Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string>
- #include<vector>
- #include "Regex.h"
- typedef std::string Name;
- template <typename T>
- Name GetVectorTypeName() {
- Name name = typeid(T).name();
- Regex testvector("class std::vector<([^,]*)(?:[^]*)>");
- std::vector<std::string> results;
- if( testvector.Begins( name, results ) ) {
- name = results[1];
- Regex stripclassorstruct("(?:class|struct)\\s+([^]*)");
- std::vector<std::string> 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<std::string>& 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 <typename T> void CreateVector( void **object ) {
- *object = new T;
- }
- // Vector specialised construction
- template <typename T> void ConstructVector( void* object, const std::vector<std::string>& data ) {
- T* vec = (T*)object;
- Name vector_type = GetVectorTypeName<T>();
- void *obj;
- CreateString(&obj);
- // All fields in this type should be valid objects for this vector
- for( std::vector<std::string>::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<std::vector<std::string>>;
- ConstructorFunc vcon = ConstructVector<std::vector<std::string>>;
- std::vector<std::string> data;
- data.push_back("x");
- data.push_back("y");
- data.push_back("z");
- void *copy_obj;
- vcreate(©_obj);
- vcon(copy_obj,data);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement