您可以使用編譯時可用的元數據來完成此操作。但是,我們必須手動完成:
template<typename Class, typename T>
struct Property {
constexpr Property(T Class::*aMember, const char* aName) : member{aMember}, name{aName} {}
using Type = T;
T Class::*member;
const char* name;
};
template<typename Class, typename T>
constexpr auto makeProperty(T Class::*member, const char* name) {
return Property<Class, T>{member, name};
}
現在我們有一個類可以容納我們期望的元數據。以下是如何使用它:
struct Dog {
constexpr static auto properties = std::make_tuple(
makeProperty(&Dog::barkType, "barkType"),
makeProperty(&Dog::color, "color")
);
private:
std::string barkType;
std::string color;
};
現在我們可以用遞歸做迭代就可以了:
template<std::size_t iteration, typename T, typename U>
void accessGetByString(T&& object, std::string name, U&& access) {
// get the property
constexpr auto property = std::get<iteration>(std::decay_t<T>::properties);
if (name == property.name) {
std::forward<U>(access)(std::forward<T>(object).*(property.member));
}
}
template<std::size_t iteration, typename T, typename U>
std::enable_if_t<(iteration > 0)>
getByStringIteration(T&& object, std::string name, U&& access) {
accessGetByString<iteration>(std::forward<T>(object), name, std::forward<U>(access));
// next iteration
getByStringIteration<iteration - 1>(std::forward<T>(object), name, std::forward<U>(access));
}
template<std::size_t iteration, typename T, typename U>
std::enable_if_t<(iteration == 0)>
getByStringIteration(T&& object, std::string name, U&& access) {
accessGetByString<iteration>(std::forward<T>(object), name, std::forward<U>(access));
}
template<typename T, typename U>
void getByString(T&& object, std::string name, U&& access) {
getByStringIteration<std::tuple_size<decltype(std::decay_t<T>::properties)>::value - 1>(
std::forward<T>(object),
name,
std::forward<U>(access)
);
}
最後,您可以使用這個工具,如:
struct MyAccess {
void operator()(int i) { cout << "got int " << i << endl; }
void operator()(double f) { cout << "got double " << f << endl; }
void operator()(std::string s) { cout << "got string " << s << endl; }
}
Dog myDog;
getByString(myDog, "color", MyAccess{});
這肯定可以通過重載lambda簡化。要了解更多關於超載拉姆達,看到這個blog post
原始代碼從答案被採取:C++ JSON Serialization
有做這樣的事情更簡單的建議。 這覆蓋P0255r0
這回答不同的問題解釋了它相當不錯:http://stackoverflow.com/a/15550632/247763 – derekerdmann
順便說一句,請不要使用' typedef struct T {}類型;'form。這不是古老的C'struct T {};''你就是需要做的。 –
很高興知道 - 謝謝!這是我第一次在12年內再次使用C++ :) –