Created
October 31, 2012 13:28
-
-
Save iwadon/3987044 to your computer and use it in GitHub Desktop.
Factory関数を複数登録できるようにした。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef BASE_HH_INCLUDED | |
| #define BASE_HH_INCLUDED 1 | |
| struct Base | |
| { | |
| virtual const char *ClassName() const | |
| { | |
| return "Base"; | |
| } | |
| }; | |
| #endif // !defined(BASE_HH_INCLUDED) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef BASE_FACTORY_REGISTRY_HH_INCLUDED | |
| #define BASE_FACTORY_REGISTRY_HH_INCLUDED 1 | |
| #include <cstddef> | |
| #include "base.hh" | |
| #include <iostream> | |
| class BaseFactoryRegistry | |
| { | |
| public: | |
| BaseFactoryRegistry() | |
| : count_(0) | |
| { | |
| } | |
| void RegisterFactory(Base *(*factory)()) | |
| { | |
| factories_[count_] = factory; | |
| ++count_; | |
| } | |
| Base *NewBase(size_t n) | |
| { | |
| return (factories_[n])(); | |
| } | |
| size_t Count() const | |
| { | |
| return count_; | |
| } | |
| static BaseFactoryRegistry *Instance() | |
| { | |
| static BaseFactoryRegistry instance; | |
| return &instance; | |
| } | |
| private: | |
| Base *(*factories_[10])(); | |
| size_t count_; | |
| }; | |
| #define BASE_FACTORY_REGISTRATION(klass) \ | |
| struct BaseFactoryRegistration_##klass { \ | |
| BaseFactoryRegistration_##klass() { \ | |
| BaseFactoryRegistry::Instance()->RegisterFactory(New##klass); \ | |
| } \ | |
| static Base *New##klass() { \ | |
| return new klass; \ | |
| } \ | |
| }; \ | |
| BaseFactoryRegistration_##klass base_factory_registration_##klass | |
| #endif // !defined(BASE_FACTORY_REGISTRY_HH_INCLUDED) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "derived.hh" | |
| #include "base_factory_registry.hh" | |
| BASE_FACTORY_REGISTRATION(Derived); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef DERIVED_HH_INCLUDED | |
| #define DERIVED_HH_INCLUDED | |
| #include "base.hh" | |
| struct Derived : public Base | |
| { | |
| const char *ClassName() const | |
| { | |
| return "Derived"; | |
| } | |
| }; | |
| #endif // !defined(DERIVED_HH_INCLUDED) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "derived2.hh" | |
| #include "base_factory_registry.hh" | |
| BASE_FACTORY_REGISTRATION(Derived2); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef DERIVED2_HH_INCLUDED | |
| #define DERIVED2_HH_INCLUDED | |
| #include "base.hh" | |
| struct Derived2 : public Base | |
| { | |
| const char *ClassName() const | |
| { | |
| return "Derived2"; | |
| } | |
| }; | |
| #endif // !defined(DERIVED2_HH_INCLUDED) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "derived3.hh" | |
| #include "base_factory_registry.hh" | |
| BASE_FACTORY_REGISTRATION(Derived3); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef DERIVED3_HH_INCLUDED | |
| #define DERIVED3_HH_INCLUDED | |
| #include "base.hh" | |
| struct Derived3 : public Base | |
| { | |
| const char *ClassName() const | |
| { | |
| return "Derived3"; | |
| } | |
| }; | |
| #endif // !defined(DERIVED3_HH_INCLUDED) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "base_factory_registry.hh" | |
| #include <iostream> | |
| int main() | |
| { | |
| for (size_t i = 0; i < BaseFactoryRegistry::Instance()->Count(); ++i) { | |
| Base *base = BaseFactoryRegistry::Instance()->NewBase(i); | |
| std::cout << base->ClassName() << std::endl; | |
| } | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CXX = g++ | |
| CXXFLAGS = -Wall -Wextra -O2 -g | |
| CPPFLAGS = | |
| AR = ar | |
| LD = g++ | |
| LDFLAGS = | |
| LIBS = | |
| RM = rm -f | |
| all: ok ng | |
| clean: | |
| $(RM) ok ng | |
| $(RM) libderived.a | |
| $(RM) main.o derived.o derived2.o derived3.o | |
| ok: main.o derived.o derived2.o derived3.o | |
| $(LD) $(LDFLAGS) -o $@ main.o derived.o derived2.o derived3.o | |
| ng: main.o libderived.a | |
| $(LD) $(LDFLAGS) -o $@ main.o -L. -lderived | |
| main.o: main.cc base_factory_registry.hh | |
| $(CXX) $(CXXFLAGS) -o $@ -c $< | |
| derived.o: derived.cc base.hh derived.hh base_factory_registry.hh | |
| $(CXX) $(CXXFLAGS) -o $@ -c $< | |
| derived2.o: derived2.cc base.hh derived2.hh base_factory_registry.hh | |
| $(CXX) $(CXXFLAGS) -o $@ -c $< | |
| derived3.o: derived3.cc base.hh derived3.hh base_factory_registry.hh | |
| $(CXX) $(CXXFLAGS) -o $@ -c $< | |
| libderived.a: derived.o derived2.o derived3.o | |
| $(RM) $@; $(AR) crs $@ derived.o derived2.o derived3.o |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment