X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/46b0747592074017ff0ea4b33d4a7194235886e5..HEAD:/libs/srvconf/parsers/property.h diff --git a/libs/srvconf/parsers/property.h b/libs/srvconf/parsers/property.h index 7aa98aeb..f83e2f6e 100644 --- a/libs/srvconf/parsers/property.h +++ b/libs/srvconf/parsers/property.h @@ -18,8 +18,7 @@ * Author : Maxim Mamontov */ -#ifndef __STG_STGLIBS_SRVCONF_PROPERTY_PARSERS_H__ -#define __STG_STGLIBS_SRVCONF_PROPERTY_PARSERS_H__ +#pragma once #include "stg/common.h" @@ -29,97 +28,94 @@ namespace STG { -class BASE_PROPERTY_PARSER +struct BasePropertyParser { - public: - virtual ~BASE_PROPERTY_PARSER() {} - virtual bool Parse(const char ** attr, const std::string & attrName, const std::string & fromEncoding) = 0; + virtual ~BasePropertyParser() = default; + virtual bool Parse(const char** attr, const std::string& attrName, const std::string& fromEncoding) = 0; }; template -class PROPERTY_PARSER : public BASE_PROPERTY_PARSER +class PropertyParser : public BasePropertyParser { public: - typedef bool (* FUNC)(const char **, T &, const std::string &); - PROPERTY_PARSER(T & v, FUNC f) : value(v), func(f) {} - PROPERTY_PARSER(T & v, FUNC f, const std::string & e) : value(v), func(f), encoding(e) {} - virtual bool Parse(const char ** attr, const std::string & attrName, const std::string & /*fromEncoding*/) { return func(attr, value, attrName); } + using Func = bool (*)(const char **, T&, const std::string&); + PropertyParser(T& v, Func f) : value(v), func(f) {} + PropertyParser(T& v, Func f, const std::string& e) : value(v), func(f), encoding(e) {} + bool Parse(const char** attr, const std::string& attrName, const std::string& /*fromEncoding*/) override { return func(attr, value, attrName); } private: T & value; - FUNC func; + Func func; std::string encoding; }; template <> inline -bool PROPERTY_PARSER::Parse(const char ** attr, const std::string & attrName, const std::string & toEncoding) +bool PropertyParser::Parse(const char** attr, const std::string& attrName, const std::string& toEncoding) { -if (!encoding.empty() && !toEncoding.empty()) + if (!encoding.empty() && !toEncoding.empty()) { - std::string tmp; - if (!func(attr, tmp, attrName)) - return false; - value = IconvString(tmp, encoding, toEncoding); - return true; + std::string tmp; + if (!func(attr, tmp, attrName)) + return false; + value = IconvString(tmp, encoding, toEncoding); + return true; } -else + return func(attr, value, attrName); } -typedef std::map PROPERTY_PARSERS; +using PropertyParsers = std::map; -bool CheckValue(const char ** attr, const std::string & attrName); +bool checkValue(const char** attr, const std::string& attrName); template inline -bool GetValue(const char ** attr, T & value, const std::string & attrName) +bool getValue(const char** attr, T& value, const std::string& attrName) { -if (CheckValue(attr, attrName)) - if (str2x(attr[1], value) < 0) - return false; -return true; + if (checkValue(attr, attrName)) + if (str2x(attr[1], value) < 0) + return false; + return true; } template <> inline -bool GetValue(const char ** attr, std::string & value, const std::string & attrName) +bool getValue(const char** attr, std::string& value, const std::string& attrName) { -if (!CheckValue(attr, attrName)) - return false; -value = attr[1]; -return true; + if (!checkValue(attr, attrName)) + return false; + value = attr[1]; + return true; } template <> inline -bool GetValue(const char ** attr, double & value, const std::string & attrName) +bool getValue(const char** attr, double& value, const std::string& attrName) { -if (CheckValue(attr, attrName)) - if (strtodouble2(attr[1], value)) - return false; -return true; + if (checkValue(attr, attrName)) + if (strtodouble2(attr[1], value)) + return false; + return true; } -bool GetEncodedValue(const char ** attr, std::string & value, const std::string & attrName); +bool getEncodedValue(const char** attr, std::string& value, const std::string& attrName); -bool GetIPValue(const char ** attr, uint32_t& value, const std::string & attrName); +bool getIPValue(const char** attr, uint32_t& value, const std::string& attrName); template inline -void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER::FUNC & func = GetValue) +void addParser(PropertyParsers& parsers, const std::string& name, T& value, const typename PropertyParser::Func& func = getValue) { - parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER(value, func))); + parsers.insert(std::make_pair(ToLower(name), new PropertyParser(value, func))); } template inline -void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const std::string & toEncoding, const typename PROPERTY_PARSER::FUNC & func = GetValue) +void addParser(PropertyParsers& parsers, const std::string& name, T& value, const std::string& toEncoding, const typename PropertyParser::Func& func = getValue) { - parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER(value, func, toEncoding))); + parsers.insert(std::make_pair(ToLower(name), new PropertyParser(value, func, toEncoding))); } -bool TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr, const std::string & fromEncoding, const std::string & attrName = "value"); +bool tryParse(PropertyParsers& parsers, const std::string& name, const char** attr, const std::string& fromEncoding, const std::string& attrName = "value"); } // namespace STG - -#endif