Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef TALKER_FACTORY_H
- #define TALKER_FACTORY_H
- #include"Talker.h"
- #include"mili/mili.h"
- #include<string>
- #include <iostream>
- class TalkerFactory
- {
- private:
- mili::Factory<std::string, Talker> fc;
- static TalkerFactory* instance;
- unsigned int users;
- template <class DerivedClass>
- void _register_factory(const std::string& k)
- {
- users++;
- fc.register_factory<DerivedClass>(k);
- }
- Talker* _new_class(std::string& k)
- {
- Talker* t;
- t = fc.new_class(k);
- return t;
- }
- bool _deregister_factory()
- {
- users--;
- return (users == 0);
- }
- public:
- TalkerFactory()
- {
- users = 0;
- }
- template<class DerivedClass>
- static void register_factory(const std::string& k)
- {
- if (instance == NULL)
- instance = new TalkerFactory;
- instance->_register_factory<DerivedClass>(k);
- }
- static Talker* new_class(std::string& k)
- {
- Talker* t;
- t = TalkerFactory::instance->_new_class(k);
- return t;
- }
- static void deregister_factory()
- {
- if (instance->_deregister_factory())
- delete instance;
- }
- };
- template<class DerivedClass,class Key>
- class Registerer{
- public:
- Registerer(const Key& k){
- TalkerFactory::register_factory<DerivedClass>(k);
- }
- ~Registerer(){
- TalkerFactory::deregister_factory();
- }
- };
- #define REGISTER_FACTORIZABLE_CLASS(DerivedClassName, keytype, key) \
- static Registerer<DerivedClassName,keytype> r##DerivedClassName(key)
- #endif
Add Comment
Please, Sign In to add comment