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

cpp-pistache-server.helpers-header.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
{{>licenseInfo}}
/*
 * {{prefix}}Helpers.h
 *
 * This is the helper class for models and primitives
 */

#ifndef {{prefix}}Helpers_H_
#define {{prefix}}Helpers_H_

#include 
#include 
#include 
#include 
#include 
#include 

namespace {{helpersNamespace}}
{

    class ValidationException : public std::runtime_error
    {
    public:
        explicit ValidationException(const std::string& what)
            : std::runtime_error(what)
        { }
        ~ValidationException() override = default;
    };

    /// 
    /// Validate a string against the full-date definition of RFC 3339, section 5.6.
    /// 
    bool validateRfc3339_date(const std::string& str);

    /// 
    /// Validate a string against the date-time definition of RFC 3339, section 5.6.
    /// 
    bool validateRfc3339_date_time(const std::string& str);

    namespace sfinae_helpers
    {
        struct NoType {};
        template  NoType operator==(const T1&, const T2&);

        template  class EqualsOperatorAvailable
        {
        public:
            enum
            {
                value = !std::is_same< decltype(std::declval() == std::declval()), NoType >::value
            };
        };
    } // namespace sfinae_helpers


    /// 
    /// Determine if the given vector only has unique elements. T must provide the == operator.
    /// 
    template 
    bool hasOnlyUniqueItems(const std::vector& vec)
    {
        static_assert(sfinae_helpers::EqualsOperatorAvailable::value,
                      "hasOnlyUniqueItems cannot be called, passed template type does not provide == operator.");
        if (vec.size() <= 1)
        {
            return true;
        }
        // Compare every element of vec to every other element of vec.
        // This isn't an elegant way to do this, since it's O(n^2),
        // but it's the best solution working only with the == operator.
        // This could be greatly improved if our models provided a valid hash
        // and/or the < operator
        for (size_t i = 0; i < vec.size() - 1; i++)
        {
            for (size_t j = i + 1; j < vec.size(); j++)
            {
                if (vec[i] == vec[j])
                {
                    return false;
                }
            }
        }
        return true;
    }

    /// 
    /// Determine if the given vector only has unique elements. T must provide the == operator.
    /// 
    template 
    bool hasOnlyUniqueItems(const std::set& set)
    {
        return true;
    }

    std::string toStringValue(const std::string &value);
    std::string toStringValue(const int32_t value);
    std::string toStringValue(const int64_t value);
    std::string toStringValue(const bool value);
    std::string toStringValue(const float value);
    std::string toStringValue(const double value);

    bool fromStringValue(const std::string &inStr, std::string &value);
    bool fromStringValue(const std::string &inStr, int32_t &value);
    bool fromStringValue(const std::string &inStr, int64_t &value);
    bool fromStringValue(const std::string &inStr, bool &value);
    bool fromStringValue(const std::string &inStr, float &value);
    bool fromStringValue(const std::string &inStr, double &value);
    template
    bool fromStringValue(const std::vector &inStr, std::vector &value){
        try{
            for(auto & item : inStr){
                T itemValue;
                if(fromStringValue(item, itemValue)){
                    value.push_back(itemValue);
                }
            }
        }
        catch(...){
            return false;
        }
        return value.size() > 0;
    }
    template
    bool fromStringValue(const std::string &inStr, std::vector &value, char separator = ','){
        std::vector inStrings;
        std::istringstream f(inStr);
        std::string s;
        while (std::getline(f, s, separator)) {
            inStrings.push_back(s);
        }
        return fromStringValue(inStrings, value);
    }

} // namespace {{helpersNamespace}}

#endif // {{prefix}}Helpers_H_




© 2015 - 2024 Weber Informatics LLC | Privacy Policy