How can I implement below? I'd like to "pre-create" 2 "Doer" template class instances for bool values. Am I stuck with virtuals? what would be most efficient way to do something like this? I'm aware of boost variant, but not sure if that's the best to use? Basically I would like to be able to have 2 "Doer" templates for bool values, as various members of doer will take different actions based on the bool value.
struct Config
{
int y;
};
template<typename DoerT>
class Aspect
{
public:
Aspect(bool b)
{
if(b) //can I create this way and pass around?
_doer<true>(this);
else
_doer<false>(this);
}
//will this work?
DoerT doer() const { return _doer;}
private:
DoerT _doer;
};
template<typename ConfigT, bool b>
class Doer
{
public:
//how to create typedef for Aspect and objects that need "b"
typedef Aspect<Doer<ConfigT,b> >* AspectT;
//also can I then specialize member functions of OtherObject
///based on b? There will be several other types here that
//will need to perform tasks differently based on the bool.
typedef OtherObject<ConfigT,b> OOT;
Doer(AspectT asp) : _asp(asp) {}
void doSomething(const Data& d)
{
}
private:
AspectT _asp;
OOT _obj;
};
//specialized members of Doer that need to behave differently based
//on bool..
template<Config,true> template<> Doer::doSomething {..}
template<Config,false> template<> Doer::doSomething {..}
template<typename DoerT>
class Manager
{
public:
typedef Aspect<DoerT>* AspectPtr;
void Load()
{
//retrieves data from database isNew returns bool
Data dbData = GetDataFromDB();
for(auto d : dbData)
{
//pass Boolean value to Aspect to create Doer templates
rows[dbData.Name]= new AspectPtr<DoerT>(d.IsNew());
}
}
AspectPtr Find(const std::string& name)
{
return rows[name];
}
private:
std::map<std::string,AspectPtr> rows;
};
class MXProcessor : Processor<Doer<Config, bool> > {...}
template<typename DoerT>
class Processor
{
public:
typedef Aspect<DoerT>* AspectPtr;
typedef OtherObject1<DoerT> obj1;
typedef OtherObject2<DoerT> obj2;
void start()
{
_mgr.Load();
}
void processData(const Data& d)
{
//lookup context row
AspectPtr asp = _ctxMgr.Find(d.Name);
asp->doer().doSomething(d);
}
private:
Manager<DoerT> _mgr;
LogMgr<DoerT> _lmgr;
};
int main() { //start MXProcessors}