diff --git a/jus/Service.cpp b/jus/Service.cpp index 0739aa0..3ca4623 100644 --- a/jus/Service.cpp +++ b/jus/Service.cpp @@ -36,7 +36,7 @@ template<> int64_t convertJsonTo(const ejson::Value& _value) { return int64_t(_value.toNumber().get()); } template<> int32_t convertJsonTo(const ejson::Value& _value) { - _value.display(); + //_value.display(); return int32_t(_value.toNumber().get()); } template<> int16_t convertJsonTo(const ejson::Value& _value) { @@ -58,7 +58,7 @@ template<> uint8_t convertJsonTo(const ejson::Value& _value) { return uint8_t(_value.toNumber().get()); } template<> std::string convertJsonTo(const ejson::Value& _value) { - _value.display(); + //_value.display(); return _value.toString().get(); } @@ -99,7 +99,67 @@ template<> ejson::Value convertToJson(const std::string& _value) { return ejson::String(_value); } +template<> bool convertStringTo(const std::string& _value) { + return etk::string_to_bool(_value); +} +template<> float convertStringTo(const std::string& _value) { + return etk::string_to_float(_value); +} +template<> double convertStringTo(const std::string& _value) { + return etk::string_to_double(_value); +} +template<> int64_t convertStringTo(const std::string& _value) { + return etk::string_to_int64_t(_value); +} +template<> int32_t convertStringTo(const std::string& _value) { + return etk::string_to_int32_t(_value); +} +template<> int16_t convertStringTo(const std::string& _value) { + return etk::string_to_int16_t(_value); +} +template<> int8_t convertStringTo(const std::string& _value) { + return etk::string_to_int8_t(_value); +} +template<> uint64_t convertStringTo(const std::string& _value) { + return etk::string_to_uint64_t(_value); +} +template<> uint32_t convertStringTo(const std::string& _value) { + return etk::string_to_uint32_t(_value); +} +template<> uint16_t convertStringTo(const std::string& _value) { + return etk::string_to_uint16_t(_value); +} +template<> uint8_t convertStringTo(const std::string& _value) { + return etk::string_to_uint8_t(_value); +} +template<> std::string convertStringTo(const std::string& _value) { + return _value; +} +bool CmdBase::checkCompatibility(const ParamType& _type, const ejson::Value& _params) { + if (createType() == _type) { + return _params.isBoolean(); + } + if ( createType() == _type + || createType() == _type + || createType() == _type + || createType() == _type + || createType() == _type + || createType() == _type + || createType() == _type + || createType() == _type + || createType() == _type + || createType() == _type) { + return _params.isNumber(); + } + if (createType() == _type) { + return _params.isString(); + } + return false; +} +bool CmdBase::checkCompatibility(const ParamType& _type, const std::string& _params) { + return false; +} diff --git a/jus/Service.h b/jus/Service.h index 9ef93e8..c7b97a9 100644 --- a/jus/Service.h +++ b/jus/Service.h @@ -21,6 +21,9 @@ class ParamType { const char* getName() const { return m_typeName; } + bool operator == (const ParamType& _obj) const { + return m_typeName == _obj.m_typeName; + } }; template @@ -32,13 +35,6 @@ template<> ParamType createType<_type>() {\ } class CmdBase { - public: - template - class Type2String { - public: - //std::string operator()(); - static std::string name(); - }; protected: const std::string m_name; public: @@ -60,18 +56,91 @@ class CmdBase { } public: virtual ~CmdBase() {}; - //virtual bool checkArguments(const std::vector& _params) = 0; + bool checkCompatibility(const ParamType& _type, const ejson::Value& _params); + bool checkCompatibility(const ParamType& _type, const std::string& _params); public: virtual std::string getPrototype() const = 0; - virtual ejson::Value execute(const ejson::Array& _params) = 0; + virtual ejson::Value executeJson(const ejson::Array& _params) = 0; + virtual std::string executeString(const std::vector& _params) = 0; }; +template +JUS_TYPE convertStringTo(const std::string& _value); + template JUS_TYPE convertJsonTo(const ejson::Value& _value); template ejson::Value convertToJson(const JUS_TYPE& _value); +template +class TypeList; + +template +ejson::Value executeCallJson(JUS_RETURN (*_func)(JUS_TYPES...), const ejson::Array& _params) { + #if defined(__clang__) + // clang generate a basic warning: + // warning: multiple unsequenced modifications to 'idParam' [-Wunsequenced] + int32_t idParam = 0; + return convertToJson(_func((convertJsonTo(_params[idParam++]))...)); + #elif defined(__GNUC__) || defined(__GNUG__) || defined(_MSC_VER) + int32_t idParam = m_paramCount-1; + return convertToJson(_func(convertJsonTo(_params[idParam--])...)); + #else + #error Must be implemented ... + #endif + return ejson::Null(); +} + +template +ejson::Value executeCallJson(void (*_func)(JUS_TYPES...), const ejson::Array& _params) { + ejson::Object out; + #if defined(__clang__) + // clang generate a basic warning: + // warning: multiple unsequenced modifications to 'idParam' [-Wunsequenced] + int32_t idParam = 0; + _func((convertJsonTo(_params[idParam++]))...); + #elif defined(__GNUC__) || defined(__GNUG__) || defined(_MSC_VER) + int32_t idParam = m_paramCount-1; + _func(convertJsonTo(_params[idParam--])...); + #else + #error Must be implemented ... + #endif + return ejson::Null(); +} + +template +std::string executeCallString(JUS_RETURN (*_func)(JUS_TYPES...), const std::vector& _params) { + #if defined(__clang__) + // clang generate a basic warning: + // warning: multiple unsequenced modifications to 'idParam' [-Wunsequenced] + int32_t idParam = 0; + return etk::to_string(_func((convertStringTo(_params[idParam++]))...)); + #elif defined(__GNUC__) || defined(__GNUG__) || defined(_MSC_VER) + int32_t idParam = m_paramCount-1; + return etk::to_string(_func(convertStringTo(_params[idParam--])...)); + #else + #error Must be implemented ... + #endif + return ""; +} +template +std::string executeCallString(void (*_func)(JUS_TYPES...), const std::vector& _params) { + ejson::Object out; + #if defined(__clang__) + // clang generate a basic warning: + // warning: multiple unsequenced modifications to 'idParam' [-Wunsequenced] + int32_t idParam = 0; + _func((convertStringTo(_params[idParam++]))...); + #elif defined(__GNUC__) || defined(__GNUG__) || defined(_MSC_VER) + int32_t idParam = m_paramCount-1; + _func(convertStringTo(_params[idParam--])...); + #else + #error Must be implemented ... + #endif + return ""; +} + template class TypeList: public CmdBase { protected: @@ -100,8 +169,9 @@ class TypeList: public CmdBase { ret += ");"; return ret; } - ejson::Value execute(const ejson::Array& _params) override { + ejson::Value executeJson(const ejson::Array& _params) override { ejson::Object out; + // check parameter number if (_params.size() != m_paramCount) { JUS_ERROR("Wrong number of Parameters ..."); out.add("error", ejson::String("WRONG-PARAMETER-NUMBER")); @@ -114,22 +184,45 @@ class TypeList: public CmdBase { out.add("error-help", ejson::String(help)); return out; } - // TODO : Check params ... - // Clang and Gcc does not exapnd variadic template at the same way ... - #if defined(__clang__) - // clang generate a basic warning: - // warning: multiple unsequenced modifications to 'idParam' [-Wunsequenced] - int32_t idParam = 0; - ejson::Value retVal = convertToJson(m_function(convertJsonTo(_params[idParam++])...)); - #elif defined(__GNUC__) || defined(__GNUG__) || defined(_MSC_VER) - int32_t idParam = m_paramCount-1; - ejson::Value retVal = convertToJson(m_function(convertJsonTo(_params[idParam--])...)); - #else - #error Must be implemented ... - #endif + // check parameter compatibility + for (size_t iii=0; iii& _params) override { + std::string out; + // check parameter number + if (_params.size() != m_paramCount) { + JUS_ERROR("Wrong number of Parameters ..."); + out += "error:WRONG-PARAMETER-NUMBER;"; + out += "error-help:request "; + out += etk::to_string(_params.size()); + out += " parameters and need "; + out += etk::to_string(m_paramCount); + out += " parameters. prototype function:"; + out += getPrototype(); + return out; + } + // check parameter compatibility + for (size_t iii=0; iii @@ -141,82 +234,12 @@ const ParamType TypeList::m_paramType[sizeof...(JUS_TY template const int32_t TypeList::m_paramCount = sizeof...(JUS_TYPES); -// Void special case: -template -class TypeListVoid: public CmdBase { - protected: - static const ParamType m_paramType[sizeof...(JUS_TYPES)]; - static const int32_t m_paramCount; - public: - using functionType = void (*)(JUS_TYPES...); - functionType m_function; - TypeListVoid(const std::string& _name, const std::string& _desc, functionType _fptr): - CmdBase(_name, _desc), - m_function(_fptr) { - - } - std::string getPrototype() const override { - std::string ret; - ret += createType().getName(); - ret += " "; - ret += m_name; - ret += "("; - for (size_t iii=0; iii(_params[idParam++])...); - #elif defined(__GNUC__) || defined(__GNUG__) || defined(_MSC_VER) - int32_t idParam = m_listParamType.size()-1; - m_function(convertJsonTo(_params[idParam--])...); - #else - #error Must be implemented ... - #endif - out.add("return", ejson::Null()); - return out; - } -}; -template -const ParamType TypeListVoid::m_paramType[sizeof...(JUS_TYPES)] = {createType()...}; - -template -const int32_t TypeListVoid::m_paramCount = sizeof...(JUS_TYPES); - - template CmdBase* createCmd(const std::string& _name, const std::string& _desc, JUS_RETURN (*_fffp)(JUS_TYPES...)) { return new TypeList(_name, _desc, _fffp); } -template -CmdBase* createCmd(const std::string& _name, const std::string& _desc, void (*_fffp)(JUS_TYPES...)) { - return new TypeListVoid(_name, _desc, _fffp); -} + static double mulllll(double _val1) { @@ -231,8 +254,10 @@ static void mulllll2(std::string _value, int32_t _val1) { } static std::string mulllll3(float _value, bool _val1) { + JUS_ERROR("Call with parameter : " << _value); + JUS_ERROR(" parameter : " << _val1); double _val2 = 1.0f; - return ""; + return "'il fait beau aujoud'hui ...'"; } static bool mulllll4() { @@ -306,7 +331,7 @@ namespace jus { { ejson::Array param; param.add(ejson::Number(58.5)); - ejson::Value out = tmp->execute(param); + ejson::Value out = tmp->executeJson(param); JUS_ERROR(" return: "); out.display(); } @@ -318,12 +343,14 @@ namespace jus { ejson::Array param; param.add(ejson::String("coucou")); param.add(ejson::Number(1563)); - ejson::Value out = tmp->execute(param); + ejson::Value out = tmp->executeJson(param); JUS_ERROR(" return: "); out.display(); } tmp = createCmd(_name, "desc", &mulllll3); JUS_ERROR("Signature3 : " << tmp->getPrototype()); + JUS_ERROR(" return: " << tmp->executeString(etk::split("3.5 false", ' '))); + tmp = createCmd(_name, "desc", &mulllll4); JUS_ERROR("Signature4 : " << tmp->getPrototype()); /*