Passing arguments in constructor with boost factor

2019-05-23 07:32发布

问题:

I am fairly new with the whole factory implementation and as a result maybe my question will sound wrong and not very well defined. So, with few words I want to have a boost factoty in order to initialize a banch fo derived classes and so far I have managed to do so for classes with empty constructors. Let me present to my current boost factory implementation for two small classes:

Base.h:

#ifndef BASE_H_
#define BASE_H_

#include <vector>
#include <map>
#include "boost/function.hpp"

class base {

protected:
    typedef boost::function <base *()> basefactory;

public:
      base();
      virtual ~base();

     int a;

     static std::map<std::string,base::basefactory>& b_factory();

  };

#endif /* BASE_H_ */

Base.cpp:

#include "base.h"

base::base() {
   // TODO Auto-generated constructor stub
}

base::~base() {
  // TODO Auto-generated destructor stub
}

static std::map<std::string,base::basefactory>& base::b_factory()
{
  static std::map<std::string,base::basefactory>* ans =
  new std::map<std::string,base::basefactory>();
  return *ans;
}

Derived.h :

#ifndef DERIVED_H_
#define DERIVED_H_

#include "boost/function.hpp"
#include "boost/functional/factory.hpp"
#include <iostream>

#include "base.h"

class derived : public base {
     public:
         derived();
         virtual ~derived();

          int b;

          static class _init {
            public:
               _init() {
                       base::b_factory()["derived"] = boost::factory<derived*>();
                       }
           }_initializer;

 };

 #endif /* DERIVED_H_ */

Derived.cpp:

#include "derived.h"

derived::derived() {
    // TODO Auto-generated constructor stub
}

derived::~derived() {
    // TODO Auto-generated destructor stub
}

derived::_init derived::_initializer;

So, the above desplayed code works just fine for empty constructor of the classes, but I am very unsure how to modify the code in case the base and derived class constructors need to receive arguments. More specifically let's say that I want to have the base constructor:

base(int alpha) { 
a = alpha;
}

And also the derived constructor:

derived(int alpha, int beta) : base( alpha ) // in order to pass the argument alpha to the base class
{ 
    b = beta;
}

So, as said above I am really unsure what modifications I need to do in order to manage to have the above boost factory implementation working for my code. I know there are some posts in here in elsewhere online for parametrized constructors but they didn't manage to make me properly understand how to do it myself and that's why I made this post her. Any kind of help/suggestion would be much appreciated!

回答1:

If you want a factory which takes 2 arguments, you may do:

std::map<std::string, boost::function<base* (int, int)>> factories;
factories["derived"] = boost::bind(boost::factory<derived*>(), _1, _2);

std::unique_ptr<base> b{factories.at("derived")(42, 52)};

If you want to fix arguments, you may do

std::map<std::string, boost::function<base* ()>> factories;
factories["derived"] = boost::bind(boost::factory<derived*>(), 42, 52);
std::unique_ptr<base> b{factories.at("derived")()};

Demo