动态和静态地访问C ++结构中的成员

时间:2010-12-02 12:51:17

标签: c++ boost boost-fusion

我想在C ++中有一个结构(或类似的东西),它将允许动态访问其成员。它应该有一个通用的getter和setter,它们将成员名称作为字符串接收,并返回某种变体类型(例如boost::variant)。

我认为可以使用boost::fusion::map实现,通过添加表示每个成员名称的字符串,以及在字符串和getter或setter函数之间构建STL映射。我不想重新发明轮子,所以我希望类似的东西已经存在。

你怎么看?我的想法会奏效吗?你知道其他方法来实现我的目标吗?

谢谢, 哈

3 个答案:

答案 0 :(得分:6)

fusion是一种方法,但为什么不将“字段”存储在由std::map键入的std::string中,其中有效负载为boost::variant ...

struct generic
{
std::map<std::string, boost::variant<foo, bar, bob, int, double> > _impl;
};

然后你可以在getter / setter中查找密钥......

哎呀,将variant包裹在optional中,你可以有可选字段!

一个更复杂的例子:

class foo
{
public:
  typedef boost::variant<int, double, float, string> f_t;
  typedef boost::optional<f_t&> return_value;
  typedef map<string, return_value> ref_map_t;

  foo() : f1(int()), f2(double()), f3(float()), f4(string()), f5(int()) 
  {
    // save the references..
    _refs["f1"] = return_value(f1);
    _refs["f2"] = return_value(f2);
    _refs["f3"] = return_value(f3);
    _refs["f4"] = return_value(f4);
    _refs["f5"] = return_value(f5);
  }

  int getf1() const { return boost::get<int>(f1); }
  double getf2() const { return boost::get<double>(f2); }
  float getf3() const { return boost::get<float>(f3); }
  string const& getf4() const { return boost::get<string>(f4); }
  int getf5() const { return boost::get<int>(f5); }

  // and setters..
  void setf1(int v) { f1 = v; }
  void setf2(double v) { f2 = v; }
  void setf3(float v) { f3 = v; }
  void setf4(std::string const& v) { f4 = v; }
  void setf5(int v) { f5 = v; }

  // key based
  return_value get(string const& key)
  {
    ref_map_t::iterator it = _refs.find(key);
    if (it != _refs.end())
      return it->second;
    return return_value();
  }

  template <typename VT>
  void set(string const& key, VT const& v)
  {
    ref_map_t::iterator it = _refs.find(key);
    if (it != _refs.end())
      *(it->second) = v;
  }

private:
  f_t f1;
  f_t f2;
  f_t f3;
  f_t f4;
  f_t f5;

  ref_map_t _refs;
};

int main(void)
{
  foo fancy;
  fancy.setf1(1);
  cout << "f1: " << fancy.getf1() << endl;

  fancy.set("f1", 10);
  cout << "f1: " << fancy.getf1() << endl;

  return 0;
}

答案 1 :(得分:1)

你在C ++中要求Reflection我认为不可用。你必须想出自己的东西。

答案 2 :(得分:1)

我为此做的是一个类似于boost :: cons的类型列表,其中包含我的成员和某种描述。然后,我通过“链式”函数调用将我的成员连续添加到“元信息”数据结构来构建此映射。整个过程看起来非常类似于在boost.python中定义一个类。如果你实际使用boost :: cons,它也应该作为boost.fusion中的一个序列,所以你可以很好地迭代你的数据。也许您可以使用boost.fusion映射来获取运行时的log(n)访问时间,但看起来它们的大小是有限的,直到可变参数模板可用。