samedi 2 avril 2016

Override class member values without changing actual implemetation

I have class like following :

class SignalProcessor
{
  //....

  bool memberBool;
  uint32_t memberUint32;

  int memberArray[10];
  //...
  float memberfloat;
};

SignalProcessor implementation is for a control system, with some complex algorithm.

Now I have to override some of those member values with some other values coming from another application for diagnostic/debugging purposes.

One way would be to dirty the entire implementation with bunch of #ifdef or if... else.. every where in the code.

So I was curious if I could do anything only within the SignalProcessor header to override those values, something like following.

SignalOverride< bool, true > memberBool;
SignalOverride< uint32_t, false> memberUint32;
SignalOverride< int, true> memberArray[10];

// SignalOverride 
template <typename T, bool overrideOn >
class  SignalOverride
{
   T operator=(T value )
   {
      if( !overrideOn )
      {
         m_signal = value;
      }
      else
      {
         /* m_signal =  ???
          Not entirely sure how to assign values here from other  application.
         */
      }
      return *this;
   }

   operator T () const
   {
      return m_signal ;
   }   

   // ...
   private:
    T m_signal;
}

The other application has same signal names in couple of C structs like following, which somehow I would have to use if override option is true.

struct Values1
{
  bool memberBool;
  uint32_t memberUint32;   
};

struct Values2
{
  int memberArray[10];   
};

I am open to any other design/solutions, except I don't want to go through each place in several source files and do if...else...

I can only use C++98 ( poor me )

Aucun commentaire:

Enregistrer un commentaire