2013-09-23 41 views
2

我正在寻找一个密切合作的结构boost::property_tree。然而,应当多一点类型安全的,比如我想获得一个异常时,我做的:类型安全提升:: property_tree相当于

#include <boost/property_tree/ptree.hpp> 

int main() 
{ 
    using boost::property_tree::ptree; 
    ptree pt; 
    pt.put("key1", "1.2"); // insert a string in key1 

    std::string val1 = pt.get<std::string>("key1"); // ok 

    double val3 = pt.get<double>("key1"); // ideally would throw 

    return 0; 
} 

基本上我寻找的情况下,#2的实现,因为在[34.4] How can I build a of objects of different types?描述。我的容器应该允许嵌套的情况(容器的容器)。

+1

为什么会抛出? API文档指出,如果转换失败,则抛出“ptree_bad_data”异常。在这种情况下,将字符串“1.2”转换为“double”1.2是没有问题的。我敢打赌,试图转换“ABC”投掷。你可以做的就是包装'get <>()'并且自己进行类型检查。你必须定义什么是不合法的。 – thokra

回答

1

您可以尝试执行强制使用类型data access through a Translator

意思是说,你可以创建一个包装/模仿property_tree接口的类,但添加一些额外的实现功能来控制类型。

我提供了一个简单的ptree_type_safe类,它模仿了property_tree(put()和get())的某些数据访问接口,但只允许检索某些类型。如果您运行代码,则应调用double val3 pt.get<double>("key1")时应打印ptree_bad_data错误消息。

#include<iostream> 
#include<boost/property_tree/ptree.hpp> 
#include<boost/optional.hpp> 


// Wrapper class for boost::property_tree::ptree 
class ptree_type_safe 
{ 
public: 
    // Constructor 
    ptree_type_safe() : m_Internal_tree(boost::property_tree::ptree()) {} 

    // Example function wrappers to take special measure will dealing with types 
    // put() 
    template<class Type> 
    boost::property_tree::ptree::self_type& put(const boost::property_tree::ptree::path_type& Path, const Type& Value) 
    { 
     return m_Internal_tree.put(Path, Value); 
    } 
    // get() 
    template<class Type> 
    Type get(const boost::property_tree::ptree::path_type& Path) 
    { 
     return m_Internal_tree.get<Type>(Path, force_type<Type>()); 
    } 

private: 
    boost::property_tree::ptree m_Internal_tree; // ptree 

    // force_type is a Translator that can be used to convert types 
    // and in this case, enforce calls to get() of only allowed types 
    template<typename T> 
    struct force_type 
    { 
     typedef std::string internal_type; 
     typedef T external_type; 
     boost::optional<T> get_value(const std::string& Key) 
     { 
      // This function will return the result of return_value<T>() if T is an allowed 
      // type, that is T has explicit specialization for struct is_allowed_type<T> 
      // and T has explicit specialization for the function return_value<T>(). 
      return boost::make_optional(is_allowed_type<T>::value, return_value<T>(Key)); 
     } 
     template<typename Arg_type> 
     struct is_allowed_type : std::false_type 
     { 
     }; 
     template<> 
     struct is_allowed_type<std::string> : std::true_type 
     { 
     }; 
     template<> 
     struct is_allowed_type<const char*> : std::true_type 
     { 
     }; 
     template<typename Return_type> 
     Return_type return_value(const std::string& Key) 
     { 
      // This will be called. 
      // Shouldn't matter if because get_value<ReturnType>() will throw an error. 
      // Will not compile if Return_type has no default constructor. 
      // Anyway, this should get the users attention, which is the primary goal. 
      return Return_type(); 
     } 
     template<> 
     std::string return_value<std::string>(const std::string& Key) 
     { 
      return Key; 
     } 
     template<> 
     const char* return_value<const char*>(const std::string& Key) 
     { 
      return Key.c_str(); 
     } 
    }; // force_type 
}; //ptree_type_safe 

int main() 
{ 
    using boost::property_tree::ptree; 
    //ptree pt; 
    ptree_type_safe pt; // use wrapper 
    pt.put("key1", "1.2"); // insert a string in key1 

    std::string val1 = pt.get<std::string>("key1"); // ok 

    try 
    { 
     double val3 = pt.get<double>("key1"); // ideally would throw 
    } 
    catch (boost::property_tree::ptree_bad_data& Error) 
    { 
     std::cout << Error.what() << std::endl; 
    } 
    return 0; 
}