任何结构接口
Any structure interface
请给我一些有关此事的线索。预先感谢您,对不起,如果我的解释似乎有些混乱。
假设我有一些结构
struct Some
{
DataTypeA a;
DataTypeB b;
...etc.
};
我有这个接口:
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我想实现此界面,以便有可能使用这样的字符串名称访问某些字段:
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);
};
可能有一些优雅的解决方案或设计模式吗?我将非常感谢任何建议。
upd1:可能是可以使用宏观的。
写下这样的宏:
#define FIELDS( TOK )
TOK(a), TOK(b), TOK(c), TOK(d)
列出了您要反思的所有变量。在您的CPP文件中,这是因为谁想揭露这个丑陋的?
使用:
#define STR2(X) #X
#define STR(X) STR2(X)
#define TOK_NAME(X) STR(X)
StringArr SomeStruct::fieldNames() {
static char const*const names[] = {
FIELDS(TOK_NAME)
};
return { std::begin(names), std::end(names) };
}
#define TOK_GET_MAP(X)
{ STR(X), +[](SomeStruct* ptr)->AnyStruct{ return ptr->m_some.X; } }
Variant SomeStruct::getValue(const std::string& fieldName) {
using get_map = std::map< std::string, AnyStruct(*)(SomeStruct*) >;
static const get_map table = {
FIELDS(TOK_GET_MAP)
};
auto it = get_map.find(fieldName);
if (it != get_map.end()) return it->second(this);
return boost::blank;
}
或各种。
相关文章:
- 如何从实现接口的模板化类实例访问结构
- 如何使用 SWIG 接口访问 python 中的 C++ typedef'd 结构
- C/C++中数组结构和数组结构的通用接口
- 我们可以在层次结构中创建多个纯虚拟接口及其实现而不会代码爆炸吗?
- C++要在其中添加接口功能的类层次结构
- C 结构实现派生接口
- 任何结构接口
- 是否有一个更适合键值数据结构的QABSTRACTITEMMODEL类型类/接口
- 将C++接口写入动态分配的 C 结构
- 从接口层次结构获取接口
- 将泛型结构作为接口中的参数传递
- 我可以部分实现接口层次结构吗
- 如何使用谷歌测试测试实现接口的不同类/结构
- 如何为类的继承层次结构的获取者创建统一的接口
- 对 stl 数据结构使用不同的接口
- 继承层次结构中接口与实现的分离(C++新手)
- 数据结构与yaml-cpp接口的设计技巧
- 如何使用结构体作为接口从函数返回指向数组的指针
- 公开POD接口的引用计数结构
- 具有许多函数的python-c++接口的代码的python结构是什么?