1
1
/* ***********************************************************************
2
- ECS:Python - Light-Weight C++ Wrapper For Embedding Python Into C++
3
- Copyright (c) 2012-2013 Marcus Tomlinson
2
+ ECS:Python - Light-Weight C++ Library For Embedding Python Into C++
3
+ Copyright (c) 2012-2014 Marcus Tomlinson
4
4
5
5
This file is part of EcsPython.
6
6
@@ -50,11 +50,17 @@ class Simple
50
50
return true ;
51
51
}
52
52
53
- void ShowLots ( unsigned long count, std::string message )
53
+ float ShowDouble ( double message )
54
+ {
55
+ std::cout << message;
56
+ return (float )message;
57
+ }
58
+
59
+ void ShowLots ( unsigned long count, char * message )
54
60
{
55
61
for ( unsigned int i = 0 ; i < count; i++ )
56
62
{
57
- std::cout << message. c_str () ;
63
+ std::cout << message;
58
64
}
59
65
lastMessage = message;
60
66
}
@@ -64,16 +70,43 @@ class Simple
64
70
return lastMessage;
65
71
}
66
72
73
+ void ShowPtr ( void * thiz )
74
+ {
75
+ ((Simple*)thiz)->Show (" Hey! This is coming from a pointer :)" );
76
+ }
77
+
67
78
private:
68
79
std::string lastMessage;
69
80
};
70
81
82
+ // Simple Class Factory
83
+ // ====================
84
+ class SimpleFactory
85
+ {
86
+ public:
87
+ void * NewSimple ( std::string firstMessage )
88
+ {
89
+ return new Simple ( firstMessage );
90
+ }
91
+
92
+ void DeleteSimple ( void * simple )
93
+ {
94
+ delete ( ( Simple* )simple );
95
+ }
96
+ };
97
+
71
98
// Register Classes + Methods
72
99
// ==========================
73
- ECS_REGISTER_CLASS ( Simple );
74
- ECS_REGISTER_METHOD_RETURN ( Simple, Show, bool , std::string );
75
- ECS_REGISTER_METHOD_VOID ( Simple, ShowLots, unsigned long , std::string );
76
- ECS_REGISTER_METHOD_RETURN ( Simple, GetLastMessage, std::string );
100
+ ECS_REGISTER_CLASS ( Simple )
101
+ ECS_REGISTER_METHOD_RETURN( Simple, Show, bool , std::string )
102
+ ECS_REGISTER_METHOD_RETURN( Simple, ShowDouble, float , double )
103
+ ECS_REGISTER_METHOD_VOID( Simple, ShowLots, unsigned long , char * )
104
+ ECS_REGISTER_METHOD_RETURN( Simple, GetLastMessage, std::string )
105
+ ECS_REGISTER_METHOD_VOID( Simple, ShowPtr, void * )
106
+
107
+ ECS_REGISTER_CLASS( SimpleFactory )
108
+ ECS_REGISTER_METHOD_RETURN( SimpleFactory, NewSimple, void *, std::string )
109
+ ECS_REGISTER_METHOD_VOID( SimpleFactory, DeleteSimple, void * )
77
110
78
111
// =================================================================================================
79
112
@@ -85,42 +118,66 @@ int main()
85
118
// ======================
86
119
Ecs_Init_Simple ();
87
120
Ecs_Init_Simple_Show ();
121
+ Ecs_Init_Simple_ShowDouble ();
88
122
Ecs_Init_Simple_ShowLots ();
89
123
Ecs_Init_Simple_GetLastMessage ();
124
+ Ecs_Init_Simple_ShowPtr ();
125
+
126
+ Ecs_Init_SimpleFactory ();
127
+ Ecs_Init_SimpleFactory_NewSimple ();
128
+ Ecs_Init_SimpleFactory_DeleteSimple ();
90
129
91
130
// Initialize EcsPython
92
131
// ====================
93
132
Ecs_Initialize ();
94
133
95
- // Create New Class Instance
96
- // =========================
97
- Simple* newSimple = new Simple ( " (first message)" );
98
134
99
- // Expose Class Instance To Python
100
- // ===============================
101
- Ecs_Expose_Object ( newSimple, " newSimple" );
135
+ // Create And Expose Class Instance To Python
136
+ // ==========================================
137
+ Simple newSimple ( " (first message)" );
138
+ Ecs_Expose_Object ( &newSimple, " newSimple" );
102
139
103
140
// Use Exposed Class Instance From Python
104
141
// ======================================
105
142
Ecs_Python_Cmd ( " print( newSimple.GetLastMessage() )" );
106
143
144
+ Ecs_Python_Cmd ( " newSimple.Show( 'my favorite number is ' )" );
145
+ Ecs_Python_Cmd ( " newSimple.ShowDouble( 5.9982 )" );
146
+ Ecs_Python_Cmd ( " print('')" );
147
+
148
+ Ecs_Python_Cmd ( " newSimple.ShowPtr( newSimple() )" );
149
+ Ecs_Python_Cmd ( " print('')" );
150
+
107
151
Ecs_Python_Cmd ( " state = newSimple.Show( 'hello' )" );
108
- Ecs_Python_Cmd ( " if state == True: \n\t print ( ' there,' )" );
152
+ Ecs_Python_Cmd ( " if state == True:\n\t print ( ' there,' )" );
109
153
110
154
Ecs_Python_Cmd ( " newSimple.ShowLots( 5, 'again and ' )" );
111
155
112
156
Ecs_Python_Cmd ( " newSimple.Show( 'once more.' )" );
113
157
Ecs_Python_Cmd ( " print('')" );
114
158
115
- // Use Class Instance From C++
116
- // ===========================
117
- std::cout << newSimple->GetLastMessage ().c_str ();
118
- getchar ();
159
+ // Use The Class Instance From C++
160
+ // ===============================
161
+ std::cout << " Ok, " << newSimple.GetLastMessage ().c_str () << std::endl;
162
+
163
+
164
+ // Create And Expose Factory To Python
165
+ // ===================================
166
+ SimpleFactory simpleFactory;
167
+ Ecs_Expose_Object ( &simpleFactory, " simpleFactory" );
168
+
169
+ // Create A New Class Instance From Python
170
+ // =======================================
171
+ Ecs_Python_Cmd ( " anotherSimple = Simple( simpleFactory.NewSimple( '\\ 'Allo ' ) )" );
172
+ Ecs_Python_Cmd ( " print( anotherSimple.GetLastMessage() + '\\ 'Allo!')" );
173
+ Ecs_Python_Cmd ( " simpleFactory.DeleteSimple( anotherSimple() )" );
174
+
119
175
120
176
// Finalize EcsPython
121
177
// ==================
122
178
Ecs_Finalize ();
123
179
180
+ getchar ();
124
181
return 0 ;
125
182
}
126
183
0 commit comments