부스트 PropertyTree는 전문화를 translator_between
으로 제공하여 사용자 정의 유형을 직렬화 할 수는 있지만 문서를 찾을 수 없으며 코드가 매우 복잡 할 수 있습니다.사용자 정의 유형을 처리하기 위해 property_tree를 확장하는 방법은 무엇입니까?
0
A
답변
1
사용자 정의 유형 CustomType
의 일반적인 패턴은 다음과 같습니다
namespace boost {
namespace property_tree {
template<>
struct translator_between<KeyType, CustomType>
{
struct type {
typedef KeyType internal_type;
typedef CustomType external_type;
boost::optional<external_type> get_value(const internal_type& str);
boost::optional<internal_type> put_value(const external_type& obj);
};
};
} // namespace property_tree
} // namespace boost
KeyType
는 ptree
및 iptree
에 대한 std::string
해야하며, 일반적으로 당신의 basic_ptree
의 첫 번째 템플릿 인수에 동일해야합니다. 그런 종류의 사람이라면 type
을 템플릿으로 만들 수 있습니다.
internal_type
과 external_type
의 두 typedef는 필수이며, 에서 ptree_utils.hpp
으로 사용됩니다.
translator_between::type
을 typedef로 만들 수 있지만 기술적으로는 필요하지 않습니다. 나는 그들이 모든 예에서 그 정의를 조금 더 예쁘게 만드는 것으로 생각한다.
get_value
및 put_value
의 인수는 반드시 const &
일 필요는 없지만 그 이유를 변경할 수는 없습니다.
특히 사용자 정의 유형에 대해 스트리밍 연산자가 오버로드 된 경우 translator_between
이라는 선언을 어디에 두어야하는지주의하십시오. 이 경우 연산자의 선언 옆에 translator_between
을 넣어야합니다.
이 문제와 관련하여 [비슷한 질문이 있습니다.] (http://stackoverflow.com/questions/9745716/change-how-boostproperty-tree-reads-translates-strings-to-bool) 예를 들어, 일반적인 설명이 아닙니다. –