eacousineau

Boost.Factory Example

Feb 10th, 2013
1,512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. #include <map>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. #include <boost/bind.hpp>
  6. #include <boost/function.hpp>
  7. #include <boost/shared_ptr.hpp>
  8. #include <boost/functional/factory.hpp>
  9.  
  10. using namespace std;
  11.  
  12. // http://www.boost.org/doc/libs/1_53_0/libs/functional/factory/doc/html/index.html#boost_functional_factory.reference.factory
  13.  
  14. struct Base
  15. {
  16.     string name;
  17.     Base(const string &name)
  18.         : name(name)
  19.     { }
  20.     virtual ~Base()
  21.     {
  22.         cout << "Good bye, " << name << "\n";
  23.     }
  24.     virtual void stuff()
  25.     {
  26.         cout << "Name: " << name << "\n";
  27.     }
  28. };
  29.  
  30. typedef boost::shared_ptr<Base> BasePtr;
  31.  
  32.  
  33. struct ChildA : public Base
  34. {
  35.     ChildA(const string &name)
  36.         : Base(name)
  37.     { }
  38.     void stuff()
  39.     {
  40.         Base::stuff();
  41.         cout << "Child A\n";
  42.     }
  43. };
  44.  
  45. typedef boost::shared_ptr<ChildA> ChildAPtr;
  46.  
  47.  
  48. struct ChildB : public Base
  49. {
  50.     int apples;
  51.     ChildB(const string &name, int apples = 12)
  52.         : Base(name), apples(apples)
  53.     { }
  54.     void stuff()
  55.     {
  56.         Base::stuff();
  57.         cout << "Child B has " << apples << " apples\n";
  58.     }
  59. };
  60.  
  61. typedef boost::shared_ptr<ChildB> ChildBPtr;
  62.  
  63. namespace boost {
  64.     // Add in a simple wrapper for a factor creating shared pointers
  65.         template<typename T>
  66.     struct shared_factory : public factory<shared_ptr<T> >
  67.     { };
  68. }
  69.  
  70. int main(int argc, char **argv)
  71. {
  72.     typedef boost::function<BasePtr (const string &name)> Factory;
  73.     typedef map<string, Factory> FactoryMap;
  74.     FactoryMap factoryMap;
  75.     factoryMap["a"] = boost::shared_factory<ChildA>();
  76.     factoryMap["b"] = boost::shared_factory<ChildB>();
  77.     // http://stackoverflow.com/questions/1335301/using-boostbind-with-a-constructor
  78.     factoryMap["b_overload"] = boost::bind(boost::shared_factory<ChildB>(), _1, 6);
  79.    
  80.     BasePtr test = factoryMap["a"]("Test1");
  81.     test->stuff();
  82.    
  83.     cout << "\nNew object in same pointer (dtor)\n\n";
  84.     test = factoryMap["b"]("Test2");
  85.     test->stuff();
  86.    
  87.     cout << "\nOverloading bound apples to 6 (default was 12)\n\n";
  88.     test = factoryMap["b_overload"]("Test3");
  89.     test->stuff();
  90.    
  91.     return 0;
  92. }
  93.  
  94. /*
  95. Name: Test1
  96. Child A
  97.  
  98. New object in same pointer (dtor)
  99.  
  100. Good bye, Test1
  101. Name: Test2
  102. Child B has 12 apples
  103.  
  104. Overloading bound apples to 6 (default was 12)
  105.  
  106. Good bye, Test2
  107. Name: Test3
  108. Child B has 6 apples
  109. Good bye, Test3
  110. */
Advertisement
Add Comment
Please, Sign In to add comment