c++ - How can I make models developing easier -


good evening

currently i'm developing c++ mvc framework. it's education purposes right - see if can make such thing , understand , learn new things (not so) beauty language.

i've got abstract class models extended make new one. registered register singleton , loaded loader class string name.

so, right now, blank new modules written in manner:

# mymodel.hpp  class mymodel: public cinite::core::abstract::model {     public:         mymodel(cinite::core::loader &_loader): cinite::core::abstract::model(_loader){};      private:         static cinite::core::registerclass<mymodel> __cinite_reg; };  # mymodel.cpp cinite::core::registerclass<mymodel> mymodel::__cinite_reg("mymodel"); 

it's quite lot of writting, decided simplify adding macros like

#define cinite_model_construct(class) \     class(cinite::core::loader &_loader): cinite::core::abstract::model(_loader){};  #define cinite_define_registry(class) \     static cinite::core::registerclass<class> __cinite_reg;  #define cinite_register(class, name) \     cinite::core::registerclass<class> class::__cinite_reg(#name); 

so whole thing way right now:

# mymodel.hpp  class mymodel: public cinite::core::abstract::model {     public:         cinite_model_construct(mymodel);      private:         cinite_define_registry(mymodel); };  # mymodel.cpp  cinite_register(mymodel, mymodel); 

and looks more easier (at least me) write.

but there still constructor thing - wonder there possibility around (as same every class, except class name) , use default abstract::model constructor? use rather simple virtual void __init() method, no arguments, if need initialization in model , bypass whole loader-thing.

so, finally, how can use default abstract::model constructor it's arguments make blank models easier write? need loader passed model it's thing makes other possible (loading other models, drivers , other things).

and another, bonus question: do looks good, or there makes terrible in use?

for ones curiosity - registry-thing looks right now

# registry.hpp  namespace cinite {     namespace core {          template <typename t>         abstract::abstract *abstractcreator(loader &_loader) {             return new t(_loader);         }         typedef abstract::abstract* (*abstractcreatorfunc)();          class registry {             public:                 static registry& getinstance() {                     static registry instance;                     return instance;                 }                 registry(){};                 registry(registry const&)       = delete;                 void operator=(registry const&) = delete;                  const static unsigned int t_abstract   = 0;                 const static unsigned int t_model      = 1;                  ...                  static void registermodel(std::string name, abstractcreatorfunc creator);                 static abstractcreatorfunc getmodelcreator(std::string name);                  ...              private:                 std::map<std::string, abstractcreatorfunc> _models;                  ...          };          template<typename t>         struct registerclass {             registerclass(const std::string name) {                 switch (t::__regtype) {                     case registry::t_model:                         registry::registermodel(name, (abstractcreatorfunc)abstractcreator<t>);                         break;                      ...                 }              }         };     } }   # registry.cpp  using namespace cinite::core;  void registry::registermodel(std::string name, abstractcreatorfunc creator) {     registry::getinstance()._models[name] = creator; }  abstractcreatorfunc registry::getmodelcreator(std::string name) {     registry &reg = registry::getinstance();     std::map<std::string, abstractcreatorfunc>::iterator it;      = reg._models.find(name);      if (it == reg._models.end()) {         throw exception::registryexception(exception::registryexception::model_not_registered, "model not found in registry (" + name + ")");     }      return (abstractcreatorfunc)it->second; } 

by way: solution registry comes this solution (thanks @johannes-schaub-litb).

could use using keyword? example, code compiles:

#include <iostream>  class  { public:     a(int a) : ma(a) {}     virtual ~a() {}     virtual void print() { std::cout << ma << std::endl; } protected:     int ma; };  class b : public { public:     using a::a; // use constructor };  int main() {     b b(10);     b.print();     return 0; } 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -