将设置/获取请求映射到 C++ 类/结构更改

map set/get requests into C++ class/structure changes(将设置/获取请求映射到 C++ 类/结构更改)
本文介绍了将设置/获取请求映射到 C++ 类/结构更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试找出最好的方法.基本上我有一个系统,我可以在其中接收外部请求,以便在我的模型中设置/获取值.问题是我的模型由可以嵌套的 C++ 类组成,而请求是简单的(键、值)对.

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.

例如:

struct Foo {
    void setX(int x);
    int getX() const;

    struct Boo {
        void setY(float y);
        float getY() const;
    }:
};

如果我收到一个对给定元素 e 说 set(y, 21) 的请求,那么我需要执行的操作将根据 foo 和 boo 是否已经存在而有所不同.必须考虑每个属性的不同可能性最终会编写大量代码.

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.

在重新发明轮子之前,我想知道 C++ 中是否已经有一个库或一项众所周知的技术,允许以通用方式将这种平面操作映射到 C++ 结构(可以嵌套)中的更改.

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.

谢谢

推荐答案

Boost 具有用于此目的的属性映射.

Boost has Property Maps for this purpose.

  • http://www.boost.org/doc/libs/1_57_0/libs/property_map/doc/property_map.html

它暴露的最基本的接口是

The most elementary interface it exposes is

get(map, key)
put(pmap, key, val)

对于左值/可读映射,您还可以获得 indexer 样式访问

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_maptyped_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
  • identity_property_map and 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 在实践中看起来像这样:

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);

请注意,agegpa 可以存储在任何地方(甚至可能需要网络请求),但是访问的差异被位于介于两者之间.

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.

我的回答示例:

  • 一个图 BOOST 是否可以有多个边权重属性图?
  • 图的割集,Boost Graph Library 使用 BiMap 和 Boost Multi Index 容器来存储属性
  • 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++ 类/结构更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Bring window to front -gt; raise(),show(),activateWindow() don’t work(把窗户放在前面 -raise(),show(),activateWindow() 不起作用)
How to get a list video capture devices NAMES (web cameras) using Qt (crossplatform)? (C++)(如何使用 Qt(跨平台)获取列表视频捕获设备名称(网络摄像机)?(C++))
How to compile Qt as static(如何将 Qt 编译为静态)
C++ over Qt : Controlling transparency of Labels and Buttons(C++ over Qt:控制标签和按钮的透明度)
How to know when a new USB storage device is connected in Qt?(Qt如何知道新的USB存储设备何时连接?)
What is an event loop in Qt?(Qt 中的事件循环是什么?)