Skip to content

Commit 1bb5e7d

Browse files
author
Marcus Tomlinson
committed
Merged 2.8
2 parents 6da20ac + 89564fd commit 1bb5e7d

File tree

14 files changed

+303
-1672
lines changed

14 files changed

+303
-1672
lines changed

CMakeLists.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
cmake_minimum_required(VERSION 2.8)
22

3-
project(EcsPython)
3+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -pthread -Wall -Wextra")
44

5-
add_definitions(
6-
-fpermissive
7-
)
5+
project(EcsPython)
86

97
add_subdirectory(example)
108

@@ -13,7 +11,6 @@ if(UNIX)
1311
endif(UNIX)
1412

1513
if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
16-
add_definitions(-DECS_EXPORT)
1714
set(PYTHON_LIBRARIES "C:/Python27/libs/python27.lib")
1815
set(PYTHON_INCLUDE_DIRS "C:/Python27/include")
1916
endif(${CMAKE_SYSTEM_NAME} MATCHES "Windows")

example/main.cpp

Lines changed: 76 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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
55
This 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+
6778
private:
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\tprint( ' 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

include/EcsPython.h

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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
55
This file is part of EcsPython.
66
@@ -32,30 +32,16 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3232
#ifndef ECSPYTHON_H
3333
#define ECSPYTHON_H
3434

35-
#ifdef _WIN32
36-
37-
#ifdef ECS_EXPORT
38-
#define DLLPORT __declspec(dllexport)
39-
#else
40-
#define DLLPORT __declspec(dllimport)
41-
#endif
42-
43-
#else
44-
45-
#define DLLPORT
46-
47-
#endif
48-
49-
#include <ecspython/EcsMacros.h>
5035
#include <dspatch/DspThread.h>
36+
#include <ecspython/EcsMacros.h>
5137

5238
struct PyMethodDef;
5339

5440
//=================================================================================================
5541
// EcsPython Globals
5642
// =================
5743

58-
struct DLLEXPORT EcsClass
44+
struct EcsClass
5945
{
6046
EcsClass( std::string newPyClassName, const std::type_info& newPyClassType )
6147
: pyClassName( newPyClassName ),
@@ -65,7 +51,7 @@ struct DLLEXPORT EcsClass
6551
const std::type_info& pyClassType;
6652
};
6753

68-
struct DLLEXPORT EcsObject
54+
struct EcsObject
6955
{
7056
EcsObject( char* newPyObject, std::string newPyClassName, std::string newPyObjectName )
7157
: pyObject( newPyObject ),
@@ -77,54 +63,54 @@ struct DLLEXPORT EcsObject
7763
std::string pyObjectName;
7864
};
7965

80-
DLLPORT extern DspMutex EcsPythonCmdMutex; // Mutex for thread-safe python calls
81-
DLLPORT extern std::vector< EcsClass* > EcsPythonClassesDict; // C++ class dictionary
82-
DLLPORT extern std::string EcsPythonClassesDef; // Python definition string for C++ classes
83-
DLLPORT extern std::vector< PyMethodDef > EcsPythonMethods; // Methods for EcsPython python module
84-
DLLPORT extern std::vector< EcsObject* > EcsExposedObjects; // C++ objects exposed to Python
66+
extern DspMutex EcsPythonCmdMutex; // Mutex for thread-safe python calls
67+
extern std::vector< EcsClass* > EcsPythonClassesDict; // C++ class dictionary
68+
extern std::string EcsPythonClassesDef; // Python definition string for C++ classes
69+
extern std::vector< PyMethodDef > EcsPythonMethods; // Methods for EcsPython python module
70+
extern std::vector< EcsObject* > EcsExposedObjects; // C++ objects exposed to Python
8571

8672
#if PY_MAJOR_VERSION >= 3
87-
DLLPORT extern struct PyModuleDef EcsPythonModule; // EcsPython python module
73+
extern struct PyModuleDef EcsPythonModule; // EcsPython python module
8874
#endif
8975

9076
//=================================================================================================
9177
// Initialize EcsPython
9278
// ====================
9379

94-
DLLEXPORT void Ecs_Initialize();
80+
void Ecs_Initialize();
9581

9682
//-------------------------------------------------------------------------------------------------
9783
// Finalize EcsPython
9884
// ==================
9985

100-
DLLEXPORT void Ecs_Finalize();
86+
void Ecs_Finalize();
10187

10288
//-------------------------------------------------------------------------------------------------
10389
// Execute Python Command
10490
// ======================
10591

106-
DLLEXPORT void Ecs_Python_Cmd( std::string pythonCmdString );
92+
void Ecs_Python_Cmd( std::string pythonCmdString );
10793

10894
//-------------------------------------------------------------------------------------------------
10995
// Execute Python File
11096
// ===================
11197

112-
DLLEXPORT void Ecs_Python_File( std::string pythonScriptPath );
98+
void Ecs_Python_File( std::string pythonScriptPath );
11399

114100
//-------------------------------------------------------------------------------------------------
115101
// Get Object Value From Python
116102
// ============================
117103

118-
DLLEXPORT std::string Ecs_Get_Value( std::string pyObjectName );
104+
std::string Ecs_Get_Value( std::string pyObjectName );
119105

120106
//-------------------------------------------------------------------------------------------------
121107
// Expose Class Instance To Python
122108
// ===============================
123109

124-
DLLEXPORT void _Ecs_Expose_Object( char* pyObject, std::string pyClassName, std::string pyObjectName );
110+
void _Ecs_Expose_Object( char* pyObject, std::string pyClassName, std::string pyObjectName );
125111

126112
template< class ObjectType >
127-
DLLEXPORT void Ecs_Expose_Object( ObjectType* object, std::string pyObjectName )
113+
void Ecs_Expose_Object( ObjectType* object, std::string pyObjectName )
128114
{
129115
for( unsigned long i = 0; i < EcsPythonClassesDict.size(); i++ )
130116
{

include/dspatch/DspThread.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/************************************************************************
22
DSPatch - Cross-Platform, Object-Oriented, Flow-Based Programming Library
3-
Copyright (c) 2012-2013 Marcus Tomlinson
3+
Copyright (c) 2012-2014 Marcus Tomlinson
44
55
This file is part of DSPatch.
66

include/dspatch/DspThreadNull.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/************************************************************************
22
DSPatch - Cross-Platform, Object-Oriented, Flow-Based Programming Library
3-
Copyright (c) 2012-2013 Marcus Tomlinson
3+
Copyright (c) 2012-2014 Marcus Tomlinson
44
55
This file is part of DSPatch.
66
@@ -55,7 +55,7 @@ class DspThread
5555
HighPriority,
5656
HighestPriority,
5757

58-
TimeCriticalPriority,
58+
TimeCriticalPriority
5959
};
6060

6161
virtual void Start( Priority priority ) {}

0 commit comments

Comments
 (0)