简体   繁体   English

通过字段的键从任何结构或元组获取迭代器作为字符串

[英]get an iterator from any struct or tuple by key of a field as string

Do you know a library in boost, poco, ... that has a function with a signature such as : 您是否知道boost,poco,...一个具有签名功能的库,例如:

template <typename T> auto vectorize(const std::vector<std::string> & mask, T & t) -> T::iterator

which could be used like : 可以像这样使用:

struct A {double x; double y; double z;};
auto a = A();
auto it = vectorize({"x","z"}, a);
//and here doing perturbations and increments on a; ideally it is for passing it to another function

I am redesigning existing code that does such a thing (with a method for serializing inside the struct) but I have to admit our version is quite messy :/ 我正在重新设计做这种事情的现有代码(使用在结构内部进行序列化的方法),但是我不得不承认我们的版本相当混乱:/

Do you have an idea of abstraction for that and working with tuples ? 您是否有抽象的想法并使用元组? I am currently trying with an enum for mapping the field of the tuple. 我目前正在尝试使用一个枚举来映射元组的字段。

I am pretty sure that someone wrote something intelligent for this kind of reflexive container iteration problem. 我很确定有人为这种自反容器迭代问题写了一些聪明的书。 (I have looked to boost::fusion but it doest filtering by types only or I missed a page) (我已经看过boost :: fusion,但它仅按类型过滤,否则我错过了页面)

C++ doesn't have any magic ways of doing this nicely like many of the VM languages. 像许多VM语言一样,C ++没有完美的实现方法。

Usually you'd use something like boost serialization . 通常,您会使用boost序列化之类的东西。 The goal being that you just write a simple mapping function, and the rest is just configuring policies. 目标是您只编写一个简单的映射功能,其余的只是配置策略。

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

class gps_position
{
public:
    int degrees;
    int minutes;
    float seconds;
    gps_position(){};
    gps_position(int d, int m, float s) :
        degrees(d), minutes(m), seconds(s)
    {}
};

namespace boost {
namespace serialization {

template<class Archive>
void serialize(Archive & ar, gps_position & g, const unsigned int version)
{
    ar & g.degrees;
    ar & g.minutes;
    ar & g.seconds;
}

} // namespace serialization
} // namespace boost

Alternatively, you may be able to get some traction with something simple that assumes that you're always serializing doubles or write static visitors for std::tuple or boost::tuple if you're not using c++11. 另外,如果您不使用c ++ 11,则假设您始终在对double进行序列化或为std::tupleboost::tuple编写静态访问者,则可以通过一些简单的操作获得一些吸引力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM