0

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}
ggs
  • 41
  • 7
  • This looks rather circular. Can you simplify the example pseudocode at all? – aschepler Aug 29 '15 at 01:16
  • I'll try.. what I'm trying to do is pre create Doer templates for bool, so members of Doer (functions & other classes with their own functions) don't have to constantly check if(true), else {}.. I should be able to derive from Doer and use virtuals, but I'm wondering if there is a better way to deal with just 2 possible run time values. – ggs Aug 29 '15 at 01:26
  • It's hard to see what you are trying to achieve. Why does the bool have to be a template parameter? "various members of doer will take different actions based on the bool value" doesn't explain it. if you are concerned about the cost of checking the bool repeatedly at run time, you might want to keep in mind that you'll benefit from branch prediction. if you make the bool a template parameter and then make a boost variant based on the two types... it's a bit circular. why not avoid the template, make something that compiles, then try to optimize. – Chris Beck Aug 29 '15 at 02:04
  • Does your program have more than one `Doer` instance? – Yakk - Adam Nevraumont Aug 29 '15 at 09:47
  • Yes.. I just updated the code. Aspects are created based on what is returned in the database.. MX Processors are on dedicated threads. Each MXProcessor uses Manager to create Aspects.. Aspects each have the same type of Doer. Doers have many objects which now need to behave differently based on the Boolean.. I would have to add if/else in many places, and there are many frequent events.. – ggs Aug 29 '15 at 14:23

1 Answers1

0

Templates are a compile time construct. You can't expect C++ would do that out of the box.

It is actually possible, using code generation, introspection (also not readily available in C++), compilation and dynamic linking. But you will have to do it yourself. It is not in the language. It won't be as trivial as a template either.

dtech
  • 47,916
  • 17
  • 112
  • 190