尝试使用boost :: factory时出错

时间:2016-07-16 15:13:50

标签: c++ boost factory boost-function

我正在尝试使用boost工厂,我遇到了以下问题。 我想要做的就是让一堆派生类有一个机制,它将初始化与相应字符串匹配的派生类。 首先,我有以下名为NetWorkBlock的基类,

NetWorkBlock.h

class NetWorkBlock {

protected:

typedef boost::function<NetWorkBlock * ()> NetWorkFactory;
//definition of the function that will be used for the factory

public:

NetWorkBlock();
virtual ~NetWorkBlock();

//some basic functionalities that not related to the factory 
  and thus not mentioned

static std::map<std::string,NetWorkBlock::NetWorkFactory>& f_factory();
//static function that initializes and returns the map
};

NetWorkBlock.cpp

NetWorkBlock::NetWorkBlock() {
} //empty constructor

NetWorkBlock::~NetWorkBlock() {
} //empty deconstructor

std::map<std::string,NetWorkBlock::NetWorkFactory>& NetWorkBlock::f_factory()
{
 static std::map<std::string,NetWorkBlock::NetWorkFactory>* ans =
 new std::map<std::string,NetWorkBlock::NetWorkFactory>();
 return *ans;
} //initialization of map

继续这里是我如何定义派生类BusNetworkBlock中的东西(注意事项 期望稍后定义更多的派生类,但是在目前的时刻,我使用单个派生类来设法让事情有效:

BusNetworkBlock.h

class BusNetworkBlock {

public:

BusNetworkBlock();
virtual ~BusNetworkBlock();

//some basic functionalities that not related to the factory 
  and thus not mentioned

private:

/* Very small, "fake" class _initializer. Its only meaning is to define a
   static member _init that is initialized at the very beginning of the
   main(). Hence the constructor is called, and the constructor registers
   the BusNetworkBlock class into the (static) NetWorkBlock::f_factory. */

static class _init {
     public:
      _init() {
           NetWorkBlock::f_factory()["LoadCurve"] = boost::factory<BusNetworkBlock *>();
      }
     }_initializer;

};

BusNetworkBlock.cpp

BusNetworkBlock::BusNetworkBlock() {
} //empty constructor

BusNetworkBlock::~BusNetworkBlock() {
} //empty deconstructor

/* The definition of the bodies of the funcionalities of the class that are not mentioned here since they are not connected with the factory */

BusNetworkBlock::_init BusNetworkBlock::_initializer;

/* Ensure that the static member _initializer is initialized, so that
   BusNetworkBlock is registered into NetWorkBlock::f_factory. */

现在在名称为UCBlock的另一个单独的类中,我尝试使用工厂,以便根据我通过以下方式接收的字符串正确初始化派生的NetworkBlock类:

UCBlock.h

 class UCBlock {

  public:

    UCBlock(std::istream& inStream); //the received instream 
    virtual ~UCBlock();

//some basic functionalities that not related to the factory 
  and thus not mentioned

   NetWorkBlock * Network; /*defining a pointer of the base class NetWorkBlock
          that want to initialize properly via the factory */

};

UCBlock.cpp

 UCBlock::UCBlock( std::istream& inStream ) {

inStream >> network; //setting the string of network the corresponding factory
Network = NetWorkBlock::f_factory()[network](); // set the corresponding object via factory **here is where the problem arises**
}

当我尝试使用工厂正确初始化时出现问题 派生类的NetWorkBlock。我收到以下错误:

 terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_function_call> >'
 what():  call to empty boost::function
 Aborted (core dumped)

现在我已经尝试在网上检查这个东西,但没有设法提出解决方案,这就是为什么我真的很感激这里的任何帮助,因为我真的很喜欢这里。 (另请注意,我检查了我收到的字符串是否正确并与“LoadCurve”匹配)。

0 个答案:

没有答案