Guest User

Untitled

a guest
Jul 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. #ifndef TALKER_FACTORY_H
  2. #define TALKER_FACTORY_H
  3. #include"Talker.h"
  4. #include"mili/mili.h"
  5. #include<string>
  6. #include <iostream>
  7.  
  8. class TalkerFactory
  9. {
  10. private:
  11.     mili::Factory<std::string, Talker> fc;
  12.     static TalkerFactory* instance;
  13.     unsigned int users;
  14.  
  15.     template <class DerivedClass>
  16.     void _register_factory(const std::string& k)
  17.     {
  18.         users++;
  19.         fc.register_factory<DerivedClass>(k);
  20.     }
  21.     Talker* _new_class(std::string& k)
  22.     {
  23.         Talker* t;
  24.         t = fc.new_class(k);
  25.         return t;
  26.     }
  27.     bool _deregister_factory()
  28.     {
  29.         users--;
  30.         return (users == 0);
  31.     }
  32. public:
  33.     TalkerFactory()
  34.     {
  35.         users = 0;
  36.     }
  37.     template<class DerivedClass>
  38.     static void register_factory(const std::string& k)
  39.     {
  40.         if (instance == NULL)
  41.             instance = new TalkerFactory;
  42.         instance->_register_factory<DerivedClass>(k);
  43.     }
  44.     static Talker* new_class(std::string& k)
  45.     {
  46.         Talker* t;
  47.         t = TalkerFactory::instance->_new_class(k);
  48.         return t;
  49.     }
  50.     static void deregister_factory()
  51.     {
  52.         if (instance->_deregister_factory())
  53.             delete instance;
  54.     }
  55.  
  56. };
  57.  
  58. template<class DerivedClass,class Key>
  59. class Registerer{
  60. public:
  61.     Registerer(const Key& k){
  62.         TalkerFactory::register_factory<DerivedClass>(k);
  63.     }
  64.     ~Registerer(){
  65.         TalkerFactory::deregister_factory();
  66.     }
  67. };
  68.  
  69. #define REGISTER_FACTORIZABLE_CLASS(DerivedClassName, keytype, key) \
  70. static Registerer<DerivedClassName,keytype> r##DerivedClassName(key)
  71.  
  72. #endif
Add Comment
Please, Sign In to add comment