#include #include #include #include "Regex.h" typedef std::string Name; template Name GetVectorTypeName() { Name name = typeid(T).name(); Regex testvector("class std::vector<([^,]*)(?:[^]*)>"); std::vector results; if( testvector.Begins( name, results ) ) { name = results[1]; Regex stripclassorstruct("(?:class|struct)\\s+([^]*)"); std::vector name_matches; if( stripclassorstruct.Find( name, name_matches )) { name = name_matches[1]; } for( std::string::iterator it = name.begin(), end = name.end(); it != end; ++it ) { *it = tolower(*it); } return name; } else { return ""; } } typedef void (*CreatorFunc)(void**); typedef void (*ConstructorFunc)(void*, const std::vector& data ); void CreateString( void **obj ) { *obj = new std::string; } void DestroyString( void **obj ) { std::string* str = (std::string*)*obj; delete str; *obj = NULL; } void ConstructString( void* obj, const std::string& data ) { *(std::string*)obj = data; } // Create instance template void CreateVector( void **object ) { *object = new T; } // Vector specialised construction template void ConstructVector( void* object, const std::vector& data ) { T* vec = (T*)object; Name vector_type = GetVectorTypeName(); void *obj; CreateString(&obj); // All fields in this type should be valid objects for this vector for( std::vector::const_iterator it = data.begin(), end = data.end(); it != end; ++it ) { // Push it vec->push_back(*obj); // Get address to new instance void *newly = &vec->back(); ConstructString(newly,*it); } DestroyString(&obj); } int main( int argc, char **argv ) { CreatorFunc vcreate = CreateVector>; ConstructorFunc vcon = ConstructVector>; std::vector data; data.push_back("x"); data.push_back("y"); data.push_back("z"); void *copy_obj; vcreate(©_obj); vcon(copy_obj,data); return 0; }