Skip to main content
GameDev.net gamedev.net
🔒 Locked

Dynamically creating (almost) arbitrary classes?

Started by Xgkkp Aug 16, 2006 at 6:11 AM 2 replies 1.3k views
Original Post
Xgkkp
Xgkkp
I am starting to redesign a project that I did last year to make it more flexible - my question is partly tied to scripts, however it is more of a general problem so I thought it would be most at home here (however using a scripting engine as an example) The problem I have is thus: I want to be able to specify objects to 'add' in the script file, for example, I want to be able to do this:
add "BField" { 
     ...
     add "linearfield" {
          ...
     }
}
add "particle"  { ...
The problem comes that I want the implementation to be vaguely flexible, that is, not hard-coded - to be able to pass the scripting code a list of "Class names" as strings (to match the parsed input) and some information that will let me create them dynamically - I assume all the objects would have to have to be inherited from the same base class, and that would be involved, but the only way I can think of is to pass a pointer to the objects constructor, but wouldn't that require it to be a static function? (And I'm sure that would cause other problems, if at all possible?) Any suggestions or insight would be greatly appreciated, I have tried searching but to be quite honest I'm not sure of the words I should be searching for, 'Dynamic class creation in c++ from scripts' and various permutations of that haven't yielded much information.
Demus79
Demus79
Use the class factory. Does exactly what you need.

[edit]
You can create a macro for example to create all required static creation functions.

Cheers
Xgkkp
Xgkkp
Ahh yes that is exactly what I need - thanks, though I am kicking myself for not being able to think of a concept so simple
Zahlman
Zahlman
For *real* flexibility, consider something like (all totally off the top of my head):

typedef std::set<std::string> ScriptObjectPrototype;class Value {  std::string literal_representation;  template <typename T>  Value& operator=(const T& t) {    try {      literal_representation = boost::lexical_cast<std::string>(t);    } catch (boost::bad_lexical_cast& error) {      literal_representation = "";    }  }  template <typename T>  operator T() {    try {      return boost::lexical_cast<T>(literal_representation);    } catch (boost::bad_lexical_cast& error) {      return T();    }  }};class ScriptObject {  ScriptObjectPrototype* type;  std::vector<Value> attribute_values;  ScriptObject(ScriptObjectPrototype& type): type(&type),     attribute_values(type->size()) {}  Value& operator[](const std::string& field) {    std::set::iterator it = type->find(field);    if (it == type->end()) { throw my_exception_of_some_sort_or_other(); }    return attribute_values[std::distance(type->begin(), it)];  }  const Value& operator[](const std::string& field) const {    std::set::iterator it = type->find(field);    if (it == type->end()) { throw my_exception_of_some_sort_or_other(); }    return attribute_values[std::distance(type->begin(), it)];  }};

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.