All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cpp-restbed-server.model-helpers-header.mustache Maven / Gradle / Ivy

There is a newer version: 7.6.0
Show newest version
{{>licenseInfo}}

#ifndef OPENAPI_MODELS_HELPER_H_
#define OPENAPI_MODELS_HELPER_H_

{{{defaultInclude}}}

#include 
#include 
#include 
#include 

{{#modelNamespaceDeclarations}}
namespace {{this}} {
{{/modelNamespaceDeclarations}}

template
boost::property_tree::ptree toPt(const T& val) {
    boost::property_tree::ptree pt;
    pt.put_value(val);
    return pt;
}

template 
boost::property_tree::ptree toPt(const std::vector & vec) {
    boost::property_tree::ptree pt;
    for (auto &childEntry : vec) {
        boost::property_tree::ptree childNode = toPt(childEntry);
        pt.push_back(std::make_pair("", childNode));
    }
    return pt;
}

template 
boost::property_tree::ptree toPt(const std::map & map) {
    boost::property_tree::ptree pt;
    for (auto &childEntry : map) {
        boost::property_tree::ptree childNode = toPt(childEntry.second);
        pt.push_back(boost::property_tree::ptree::value_type(childEntry.first, childNode));
    }
    return pt;
}

template 
boost::property_tree::ptree toPt(const std::set & set) {
    boost::property_tree::ptree pt;
    for (auto &childEntry : set) {
        boost::property_tree::ptree childNode = toPt(childEntry);
        pt.push_back(std::make_pair("", childNode));
    }
    return pt;
}


template
struct is_vector
{
    static constexpr bool value = false;
};

template
struct is_vector>
{
    static constexpr bool value = true;
};

template
struct is_map
{
    static constexpr bool value = false;
};

template
struct is_map>
{
    static constexpr bool value = true;
};

template
struct is_set
{
    static constexpr bool value = false;
};

template
struct is_set>
{
    static constexpr bool value = true;
};


template
std::enable_if_t::value && !is_map::value && !is_set::value, T>
fromPt(const boost::property_tree::ptree& pt) {
    return pt.get_value();
}

template
std::enable_if_t::value,T>
fromPt(const boost::property_tree::ptree& pt) {
    T vec;
    for (const auto &child: pt) {
        typename T::value_type childElement = fromPt(child.second);
        vec.emplace_back(childElement);
    }
    return vec;
}

template 
std::enable_if_t::value,T>
fromPt(const boost::property_tree::ptree &pt) {
    T map;
    for (const auto &child: pt) {
        using ChildType = typename T::mapped_type;
        ChildType childElement = fromPt(child.second);
        map.insert(std::make_pair(child.first, childElement));
    }
    return map;
}

template
std::enable_if_t::value,T>
fromPt(const boost::property_tree::ptree& pt) {
    T set;
    for (const auto &child: pt) {
        typename T::value_type childElement = fromPt(child.second);
        set.insert(childElement);
    }
    return set;
}

{{#modelNamespaceDeclarations}}
}
{{/modelNamespaceDeclarations}}

#endif /* OPENAPI_MODELS_HELPER_H_ */




© 2015 - 2024 Weber Informatics LLC | Privacy Policy