映射集/获取请求到C ++类/结构的变化 [英] map set/get requests into C++ class/structure changes
问题描述
我想弄明白这里最好的方法是什么。基本上我有一个系统,我收到外部请求,以设置/获取我的模型中的值。问题是我的模型由C ++类组成,可以嵌套,而请求是简单的(键,值)对。
例如:
struct Foo {
void setX(int x);
int getX()const;
struct Boo {
void setY(float y);
float getY()const;
}:
};
如果我收到一个请求 set(y,21) code>给定的元素e,然后我需要执行的动作将不同,取决于是否foo和boo已经存在。不得不照顾每个属性的不同的可能性将写入大量的代码。
在重新发明轮子之前,我想知道是否已经有一个库或者C ++中的一个众所周知的技术,它允许以一种通用的方式将此平面操作映射到C ++结构(可以嵌套)中的变化。
Boost有此属性的属性地图。
>
get(map,key)
put(pmap,key,val)
对于价值可读的地图,您还可以获得样式访问
$pmap [key];
pmap [key] = newval; //如果不是const / readonly
您可以使用现有的属性映射适配器:
-
identity_property_map
和typed_identity_property_map
-
function_property_map
-
iterator_property_map
-
shared_array_property_map
-
associative_property_map
-
const_associative_property_map
-
vector_property_map
-
ref_property_map
-
static_property_map
-
transform_value_property_map
-
compose_property_map
或写自定义。
甚至还有 dynamic_property_map
,如下所示:
put(age,properties,fred,new_age);
put(gpa,properties,fred,new_gpa);
请注意 age
和 gpa
可以存储在任何地方(甚至可能需要一个web请求),但访问的差异被位于之间的属性界面抽象化。
我的答案中的示例:
I'm trying to figure out what is the best approach here. Basically I have a system where I receive external requests in order to set/get values in my model. The problem is that my model consists on C++ classes, which can be nested, whereas the requests are simple (key, value) pairs.
For example:
struct Foo {
void setX(int x);
int getX() const;
struct Boo {
void setY(float y);
float getY() const;
}:
};
If I receive a request that says set(y, 21)
for a given element e, then the actions I need to perform will be different depending on whether or not foo and boo already exist. Having to take care of the different possibilities for each property would end up in writing a lot of code.
Before reinventing the wheel, I was wondering if there is already a library or a well-known technique in C++ that allows mapping this flat actions into changes in C++ structures (which can be nested) in a generic way.
Thanks
Boost has Property Maps for this purpose.
The most elementary interface it exposes is
get(map, key)
put(pmap, key, val)
For lvalue/readable maps you can also get indexer style access
pmap[key];
pmap[key] = newval; // if not const/readonly
You can a existing property map adaptors:
identity_property_map
andtyped_identity_property_map
function_property_map
iterator_property_map
shared_array_property_map
associative_property_map
const_associative_property_map
vector_property_map
ref_property_map
static_property_map
transform_value_property_map
compose_property_map
or write custom ones.
There is even a dynamic_property_map
that looks like this, in practice:
put("age",properties,fred,new_age);
put("gpa",properties,fred,new_gpa);
Note that age
and gpa
could be stored anywhere (even requiring a web-request, perhaps) but the difference in access is abstracted away by the properymap interface that sits in between.
Samples from my answers:
- Is it possible to have several edge weight property maps for one graph BOOST?
- Cut set of a graph, Boost Graph Library using a BiMap and a Boost Multi Index container to store the properties
这篇关于映射集/获取请求到C ++类/结构的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!