Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <map>
- #include <iostream>
- #include <string>
- #include <boost/bind.hpp>
- #include <boost/function.hpp>
- #include <boost/shared_ptr.hpp>
- #include <boost/functional/factory.hpp>
- using namespace std;
- // http://www.boost.org/doc/libs/1_53_0/libs/functional/factory/doc/html/index.html#boost_functional_factory.reference.factory
- struct Base
- {
- string name;
- Base(const string &name)
- : name(name)
- { }
- virtual ~Base()
- {
- cout << "Good bye, " << name << "\n";
- }
- virtual void stuff()
- {
- cout << "Name: " << name << "\n";
- }
- };
- typedef boost::shared_ptr<Base> BasePtr;
- struct ChildA : public Base
- {
- ChildA(const string &name)
- : Base(name)
- { }
- void stuff()
- {
- Base::stuff();
- cout << "Child A\n";
- }
- };
- typedef boost::shared_ptr<ChildA> ChildAPtr;
- struct ChildB : public Base
- {
- int apples;
- ChildB(const string &name, int apples = 12)
- : Base(name), apples(apples)
- { }
- void stuff()
- {
- Base::stuff();
- cout << "Child B has " << apples << " apples\n";
- }
- };
- typedef boost::shared_ptr<ChildB> ChildBPtr;
- namespace boost {
- // Add in a simple wrapper for a factor creating shared pointers
- template<typename T>
- struct shared_factory : public factory<shared_ptr<T> >
- { };
- }
- int main(int argc, char **argv)
- {
- typedef boost::function<BasePtr (const string &name)> Factory;
- typedef map<string, Factory> FactoryMap;
- FactoryMap factoryMap;
- factoryMap["a"] = boost::shared_factory<ChildA>();
- factoryMap["b"] = boost::shared_factory<ChildB>();
- // http://stackoverflow.com/questions/1335301/using-boostbind-with-a-constructor
- factoryMap["b_overload"] = boost::bind(boost::shared_factory<ChildB>(), _1, 6);
- BasePtr test = factoryMap["a"]("Test1");
- test->stuff();
- cout << "\nNew object in same pointer (dtor)\n\n";
- test = factoryMap["b"]("Test2");
- test->stuff();
- cout << "\nOverloading bound apples to 6 (default was 12)\n\n";
- test = factoryMap["b_overload"]("Test3");
- test->stuff();
- return 0;
- }
- /*
- Name: Test1
- Child A
- New object in same pointer (dtor)
- Good bye, Test1
- Name: Test2
- Child B has 12 apples
- Overloading bound apples to 6 (default was 12)
- Good bye, Test2
- Name: Test3
- Child B has 6 apples
- Good bye, Test3
- */
Advertisement
Add Comment
Please, Sign In to add comment