[PREV] < SGEngine's C++ Modules || 2010-08-24 14:25:02 > [NEXT]
SGEngine's primary experimental approach is that of everything being a dynamic loadable module.

For the most part, DLLs tend to use C due to C++'s name mangling shenanigans making it hard to link against.

However, C doesn't provide the const safety, nor the structure, which C++ affords - nor the extensions given via STL... so how would we work around this?

Well, we can create our C++ classes as normal, and provide two C-wrappers; one to create a new instance of our class, and another to destroy it.

It's important that you expose a create and a destroy wrapper within the Library and that it creates and destroys any instance created. Otherwise, all sorts of madness can ensue due to newing and deleting memory across DLL boundaries which you shouldn't really have access to.

  1.  
  2. class SimpleClass
  3. {
  4.     public:
  5.         SimpleClass();
  6.         ~SimpleClass();
  7.  
  8.         void simpleFunction(const std::string& myString);
  9.  
  10. };
  11.  
  12. extern "C" SimpleClass* create() {
  13.     return new SimpleClass;
  14. }
  15.  
  16. extern "C" void destroy(SimpleClass* pClass) {
  17.     delete pClass;
  18. }
  19.  


If the above class is then pulled in via a library, loaded via whichever your OS supports ( LoadLibrary/dlopen ) you would then pull out the function pointers to the create and destroy functions we've externed ( and are therefore not subject to the C++ name mangling. )

An instance of the class can then be created via calling the create wrapper we've just linked in.
Once we're finished with it, we pass the Class pointer back to the destroy function, and it gets cleaned up.

This sounds like a mad way of doing things, but it's essentially a manual method of calling the Constructor and Destructor, as there's nothing saying your create/destroy functions cannot take parameters to pass through. It just requires a bit of resource management so you don't create or destroy too many instances - but then that should be a given for creating any pointers.

SGEngine does go a step further in that all it's library modules are derived from SGELIB.
Therefore, virtual methods and so on are not a problem assuming you have the above wrapper for create and destroy; as destroying a pointer of the wrong type, is a very bad thing indeed!
[PREV] < 0 Comments || Add Comment > [NEXT]

Categories

Code Tutorial
Engine Dev
Honours Project
Dev Stuff
General Stuff
Everything

Previous Blogs

Current Blog
SGEngine's C++ Modules
Code Highlighting Test
Starting Engine Development
Let's see if this still works!
Ooo an Update!
And it dies... again...
Fixed Blog?
Fixing The Blog
Milestone Two - The Biggie
Handheld Fun!
Let there be SOUND!
It's Alive!!
The Mid Milestone
Merging Trees
Spam!
Milestone One
Bitten by the Python
To Embed or Extend...
Progress Update
Feeling better!
Illness
It's a VM, but it's not!
Homebrew for the masses!
A Blog Update ( and some code mangling too! )
Engine Fun
Still Alive
New Front Site!
Site Programming
Hello World!


Blog and Code by Steven Campbell
Blog opinions are my own, as different as they may be to yours.
Comments are other people's opinions though.

Valid XHTML 1.0 Strict Valid CSS!