11/* ***********************************************************************
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
44
55This file is part of EcsPython.
66
@@ -50,11 +50,17 @@ class Simple
5050 return true ;
5151 }
5252
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 )
5460 {
5561 for ( unsigned int i = 0 ; i < count; i++ )
5662 {
57- std::cout << message. c_str () ;
63+ std::cout << message;
5864 }
5965 lastMessage = message;
6066 }
@@ -64,16 +70,43 @@ class Simple
6470 return lastMessage;
6571 }
6672
73+ void ShowPtr ( void * thiz )
74+ {
75+ ((Simple*)thiz)->Show (" Hey! This is coming from a pointer :)" );
76+ }
77+
6778private:
6879 std::string lastMessage;
6980};
7081
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+
7198// Register Classes + Methods
7299// ==========================
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 * )
77110
78111// =================================================================================================
79112
@@ -85,42 +118,66 @@ int main()
85118 // ======================
86119 Ecs_Init_Simple ();
87120 Ecs_Init_Simple_Show ();
121+ Ecs_Init_Simple_ShowDouble ();
88122 Ecs_Init_Simple_ShowLots ();
89123 Ecs_Init_Simple_GetLastMessage ();
124+ Ecs_Init_Simple_ShowPtr ();
125+
126+ Ecs_Init_SimpleFactory ();
127+ Ecs_Init_SimpleFactory_NewSimple ();
128+ Ecs_Init_SimpleFactory_DeleteSimple ();
90129
91130 // Initialize EcsPython
92131 // ====================
93132 Ecs_Initialize ();
94133
95- // Create New Class Instance
96- // =========================
97- Simple* newSimple = new Simple ( " (first message)" );
98134
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" );
102139
103140 // Use Exposed Class Instance From Python
104141 // ======================================
105142 Ecs_Python_Cmd ( " print( newSimple.GetLastMessage() )" );
106143
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+
107151 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,' )" );
109153
110154 Ecs_Python_Cmd ( " newSimple.ShowLots( 5, 'again and ' )" );
111155
112156 Ecs_Python_Cmd ( " newSimple.Show( 'once more.' )" );
113157 Ecs_Python_Cmd ( " print('')" );
114158
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+
119175
120176 // Finalize EcsPython
121177 // ==================
122178 Ecs_Finalize ();
123179
180+ getchar ();
124181 return 0 ;
125182}
126183
0 commit comments