2013-09-23 3 views
2

Je cherche une structure qui ressemble beaucoup à boost::property_tree. Cependant, il devrait être un peu plus de type sécurisé, par exemple, je voudrais faire une exception quand je fais:Type boost boost :: property_tree équivalent

#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; 
} 

Fondamentalement, je suis à la recherche d'une mise en œuvre de cas n ° 2, comme décrit dans [34.4] How can I build a of objects of different types?. Mon conteneur doit autoriser le cas imbriqué (conteneur de conteneur).

+1

Pourquoi ce lancer? L'API doc indique qu'une exception 'ptree_bad_data' est levée si la conversion échoue. Dans ce cas, il n'y a aucun problème à convertir le 'string'" 1.2 "en un" double "1.2. Je parie essayer de convertir "abc" jette. Ce que vous pouvez faire est d'enrouler 'get <>()' et de vérifier vous-même. Vous devez définir ce qui est légal. – thokra

Répondre

1

Vous pouvez essayer d'imposer l'utilisation du type en forçant data access through a Translator. Cela signifie que vous pouvez créer une classe qui enveloppe/imite une interface property_tree, mais ajoutez des fonctionnalités d'implémentation supplémentaires pour essayer de contrôler les types.

J'ai fourni un simple de classe ptree_type_safe qui imite une partie de l'interface d'accès aux données d'un property_tree (put() et get()), mais qui permet uniquement de récupérer certains types. Si vous exécutez le code, il doit afficher un message d'erreur ptree_bad_data lorsque double val3 pt.get<double>("key1") est appelée.

#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; 
} 
Questions connexes