lundi 26 février 2018

Any structure interface

Please, give me some clues concerning this matter. Thank you in advance and sorry if my explanation seems a bit confusing.

Suppose I have some struct

struct Some
{
  DataTypeA a;
  DataTypeB b;
  ...etc.
};

and I have this interface:

class AnyStruct
{
public:
  using Variant = boost::variant<boost::blank, DataTypeA, DataTypeB, ...etc.>;
  using StringArr = std::vector<std::string>;

  virtual StringArr fieldNames() = 0;
  virtual Variant getValue(const std::string & fieldName) = 0;
};

Futher I want to implement this interface to have possibility to access Some fields using string names like this:

class SomeStruct : public AnyStruct
{
  Some m_some;

public:
  SomeStruct(const Some & some);

  /**
   * Function fieldNames returns vector {"a", "b", ...etc.}. Please, see the Some 
   * structure definition above.
   */
  virtual StringArr fieldNames();

  /**
   * 1. Function getValue with parameter "a" returns m_some.a wrapped in boost::variant
   * 2. Function getValue with parameter "b" returns m_some.b wrapped in boost::variant
   * ...etc.
   */
  virtual Variant getValue(const std::string & fieldName);
};

May be there is a some elegant solution or design pattern for this case? I will be very grateful for any advices.

Aucun commentaire:

Enregistrer un commentaire