From 53bb23e1c2942933e372263c4219217fd3d0b49c Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Fri, 4 Mar 2016 22:56:56 +0100 Subject: [PATCH] [DEV] try to create variant type for auto parameter set ... --- eproperty/List.h | 106 +++++--------------------- eproperty/Property.h | 2 + eproperty/PropertyType.h | 153 ++++++++++++++++++++++++++++++++++++++ eproperty/Range.h | 106 ++++---------------------- eproperty/Value.h | 113 +++------------------------- eproperty/Variant.cpp | 14 ++++ eproperty/Variant.h | 28 +++++++ eproperty/VariantBase.cpp | 0 eproperty/VariantBase.h | 25 +++++++ lutin_eproperty-test.py | 39 ++++++++++ lutin_eproperty.py | 6 +- test/main.cpp | 32 ++++++++ test/test_list.cpp | 0 test/test_range.cpp | 0 test/test_value.cpp | 0 15 files changed, 341 insertions(+), 283 deletions(-) create mode 100644 eproperty/PropertyType.h create mode 100644 eproperty/Variant.cpp create mode 100644 eproperty/Variant.h create mode 100644 eproperty/VariantBase.cpp create mode 100644 eproperty/VariantBase.h create mode 100644 lutin_eproperty-test.py create mode 100644 test/main.cpp create mode 100644 test/test_list.cpp create mode 100644 test/test_range.cpp create mode 100644 test/test_value.cpp diff --git a/eproperty/List.h b/eproperty/List.h index e73361f..f60c7b2 100644 --- a/eproperty/List.h +++ b/eproperty/List.h @@ -7,17 +7,13 @@ */ #pragma once -#include -#include -#include +#include #include #include namespace eproperty { - template class List : public Property { + template class List : public PropertyType { private: - TYPE m_value; //!< Element value ==> can be directly used. - TYPE m_default; //!< Default value. std::map m_list; //!< pointer on the list of all elements. public: /** @@ -34,12 +30,8 @@ namespace eproperty { const TYPE& _defaultValue, const std::string& _description="", void (CLASS_TYPE::*_setObs)()=nullptr) : - Property(_owner, _name), - m_value(_defaultValue), - m_default(_defaultValue) { - if (_setObs != nullptr) { - setObserver([=](){(*_owner.*_setObs)();}); - } + eproperty::PropertyType(_owner, _name, _defaultValue, _description, _setObs) { + }; /** * @brief Destructor. @@ -53,29 +45,15 @@ namespace eproperty { } m_list.insert(std::make_pair(_name, _value)); } - // herited methode - virtual std::string getPropertyType() const { + std::string getPropertyType() const override { return "eproperty::List"; } - // herited methode - virtual std::string getType() const { - return typeid(TYPE).name(); - } - // herited methode - virtual std::string getString() const { - return getElement(m_value); - } - // herited methode - virtual std::string getDefault() const { - return getElement(m_default); - } - // herited methode - virtual void setString(const std::string& _newVal) { + void setString(const std::string& _newVal) override { auto it = m_list.find(_newVal); if (it != m_list.end()) { - if (it->second != m_value) { - m_value = it->second; - notifyChange(); + if (it->second != eproperty::PropertyType::m_value) { + eproperty::PropertyType::m_value = it->second; + eproperty::PropertyType::notifyChange(); } return; } @@ -84,94 +62,44 @@ namespace eproperty { EPROPERTY_VERBOSE(" element : " << it.first); } } - // herited methode - virtual std::string getInfo() const { - std::string list = "List default=" + getElement(m_default) + " in : ["; + std::string getInfo() const override { + std::string list = "List default=" + getValueSpecific(eproperty::PropertyType::m_default) + " in : ["; for (auto &it : m_list) { list += it.first + "/"; } return list + "]"; } - // herited methode - virtual bool isDefault() const { - return m_value == m_default; - } - // herited methode - virtual void setDefault() { - set(m_default); - } - void setDefaultValue(const TYPE& _value) { - m_default = _value; - } - /** - * @brief Get the value of the current parameter. - * @return the Reference value - */ - const inline TYPE& get() const { - return m_value; - }; /** * @brief Set the value of the current parameter. * @param[in] _newVal New value of the parameter. (not set if out of range) */ - void set(TYPE _newVal) { - if (_newVal == m_value) { + void set(const TYPE& _newVal) override { + if (_newVal == eproperty::PropertyType::m_value) { return; } for (auto &it : m_list) { if (it.second == _newVal) { - m_value = it.second; - notifyChange(); + eproperty::PropertyType::m_value = it.second; + eproperty::PropertyType::notifyChange(); return; } } EPROPERTY_WARNING("paramList value=??? is not un the list ... ==> no change"); } - /** - * @brief Set the value of the current parameter (no check (for internal set with no check). - * @note For performence, this function must be inline - * @note Only use by the owner of the property (can not be check on compile time for now ...) - * TODO: Do it better ... compile check - * @param[in] newVal New value to set - */ - inline void setDirect(const TYPE& _newVal) { - m_value = _newVal; - }; - /** - * @brief Get the value of the current parameter (no check (for internal set with no check). - * @note For performence, this function must be inline - * @note Only use by the owner of the property (can not be check on compile time for now ...) - * TODO: Do it better ... compile check - * @return a reference on the value - */ - TYPE& getDirect() { - return m_value; - } private: /** * @brief Get the element description from real Value. * @param[in] _intValue value that might be converted in string. * @return the description string coresponding to this ID. */ - std::string getElement(TYPE _intValue) const { + std::string getValueSpecific(const TYPE& _valueRequested) const override { for (auto &it : m_list) { - if (it.second == _intValue) { + if (it.second == _valueRequested) { return it.first; } } return "???"; } - public: - const List& operator= (const TYPE& _newVal) = delete; - operator const TYPE&() const { - return m_value; - } - const TYPE& operator *() const noexcept { - return m_value; - } - const TYPE* operator->() const noexcept { - return &m_value; - } }; template std::ostream& operator <<(std::ostream& _os, const eproperty::List& _obj) { _os << _obj.get(); diff --git a/eproperty/Property.h b/eproperty/Property.h index e775923..c611eac 100644 --- a/eproperty/Property.h +++ b/eproperty/Property.h @@ -8,6 +8,7 @@ #pragma once #include +#include #include #include #include @@ -75,6 +76,7 @@ namespace eproperty { * @brief Reset the value to the default value. */ virtual void setDefault() = 0; + virtual void setVariant(eproperty::VariantBase* _variantValue) = 0; public: template bool operator== (const TYPE& _obj) const = delete; diff --git a/eproperty/PropertyType.h b/eproperty/PropertyType.h new file mode 100644 index 0000000..318fe32 --- /dev/null +++ b/eproperty/PropertyType.h @@ -0,0 +1,153 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2016, Edouard DUPIN, all right reserved + * + * @license APACHE v2.0 (see license file) + */ +#pragma once + +#include +#include +#include +#include + + +namespace eproperty { + template class VariantSpecify : public eproperty::VariantBase { + private: + TYPE m_value; + public: + VariantSpecify() : + eproperty::VariantBase(uint64_t(typeid(TYPE))), + m_value() { + + } + VariantSpecify(TYPE&& _value) : + eproperty::VariantBase(uint64_t(typeid(TYPE))), + m_value(_value) { + + } + const TYPE& get() { + return m_value; + } + }; + template class PropertyType : public Property { + protected: + TYPE m_value; //!< Current value. + TYPE m_default; //!< Default value. + public: + /** + * @brief Create a parameter with a specific type. + * @param[in] _owner Owner of the parameter. + * @param[in] _name Static name of the parameter. + * @param[in] _defaultValue Default value of the parameter. + * @param[in] _description description of the parameter. + * @param[in] _setObs function of the class that opserve the change of the value + */ + template + PropertyType(CLASS_TYPE* _owner, + const std::string& _name, + const TYPE& _defaultValue, + const std::string& _description = "", + void (CLASS_TYPE::*_setObs)()=nullptr) : + Property(_owner, _name), + m_value(_defaultValue), + m_default(_defaultValue) { + if (_setObs != nullptr) { + setObserver([=](){(*_owner.*_setObs)();}); + } + } + /** + * @brief Destructor. + */ + virtual ~PropertyType() = default; + std::string getPropertyType() const override { + return "eproperty::Value"; + } + std::string getType() const override { + return typeid(TYPE).name(); + } + std::string getString() const override { + return getValueSpecific(m_value); + } + std::string getDefault() const override { + return getValueSpecific(m_default); + } + std::string getInfo() const override { + return getType() + " default=" + getDefault(); + } + bool isDefault() const override { + return m_value == m_default; + } + void setDefault() override { + set(m_default); + } + public: + /** + * @brief Get the value of the current parameter. + * @note For performence, this function must be inline + * @return the Reference value + */ + const inline TYPE& get() const { + return m_value; + }; + /** + * @brief Set a new value for this parameter + * @param[in] newVal New value to set (set the nearest value if range is set) + */ + virtual void set(const TYPE& _newVal) { + if (_newVal != m_value) { + m_value = _newVal; + notifyChange(); + } + } + void setVariant(eproperty::VariantBase* _variantValue) override { + eproperty::VariantSpecify* value = dynamic_cast*>(_variantValue); + if (value == nullptr) { + EPROPERTY_ERROR("Can not set property : '" << getName() << "' wrong variant type ..."); + return; + } + // TODO : Check range ... + setDirect(value->get()); + } + /** + * @brief Set the value of the current parameter (no check (for internal set with no check). + * @note For performence, this function must be inline + * @note Only use by the owner of the property (can not be check on compile time for now ...) + * TODO: Do it better ... compile check + * @param[in] newVal New value to set + */ + inline void setDirect(const TYPE& _newVal) { + m_value = _newVal; + } + /** + * @brief Get the value of the current parameter (no check (for internal set with no check). + * @note For performence, this function must be inline + * @note Only use by the owner of the property (can not be check on compile time for now ...) + * TODO: Do it better ... compile check + * @return a reference on the value + */ + TYPE& getDirect() { + return m_value; + } + virtual std::string getValueSpecific(const TYPE& _valueRequested) const = 0; + public: + operator const TYPE&() const { + return m_value; + } + const TYPE& operator *() const noexcept { + return m_value; + } + const TYPE* operator->() const noexcept { + return &m_value; + } + const PropertyType& operator= (const TYPE& _newVal) = delete; + }; + + template std::ostream& operator <<(std::ostream& _os, const eproperty::PropertyType& _obj) { + _os << _obj.get(); + return _os; + } +} + diff --git a/eproperty/Range.h b/eproperty/Range.h index 9f1247e..4fb4760 100644 --- a/eproperty/Range.h +++ b/eproperty/Range.h @@ -8,16 +8,14 @@ #pragma once #include -#include +#include #include namespace eproperty { - template class Range : public Property { + template class Range : public Value { private: - TYPE m_value; //!< Current value. TYPE m_min; //!< Minimum value. TYPE m_max; //!< Maximum value. - TYPE m_default; //!< Default value. public: /** * @brief Create a parameter with a specific type. @@ -37,122 +35,48 @@ namespace eproperty { const TYPE& _max, const std::string& _description = "", void (CLASS_TYPE::*_setObs)()=nullptr) : - Property(_owner, _name), - m_value(_defaultValue), + eproperty::Value(_owner, _name, _defaultValue, _description, _setObs), m_min(_min), - m_max(_max), - m_default(_defaultValue) { + m_max(_max) { if (m_min > m_max) { //EPROPERTY_CRITICAL("min > max..."); } - if (_setObs != nullptr) { - setObserver([=](){(*_owner.*_setObs)();}); - } }; /** * @brief Destructor. */ virtual ~Range() = default; - // herited methode - virtual std::string getPropertyType() const { + std::string getPropertyType() const override { return "eproperty::Range"; } - // herited methode - virtual std::string getType() const { - return typeid(TYPE).name(); - } - // herited methode - virtual std::string getString() const { - return getValueSpecific(m_value); - }; - // herited methode - virtual std::string getDefault() const { - return getValueSpecific(m_default); - }; - // herited methode - virtual void setString(const std::string& _newVal) { + void setString(const std::string& _newVal) override { TYPE val; // when you want to set an element in parameter you will implement the function template std::from_string etk::from_string(val, _newVal); set(val); } - // herited methode - virtual std::string getInfo() const { - return getType() + " default=" + getDefault(); - } - // herited methode - virtual bool isDefault() const { - return m_value == m_default; - } - // herited methode - virtual void setDefault() { - set(m_default); + std::string getInfo() const override { + return eproperty::Value::getType() + " default=" + eproperty::Value::getDefault(); } public: - /** - * @brief Get the value of the current parameter. - * @return The reference value - */ - const inline TYPE& get() const { - return m_value; - }; /** * @brief Set a new value for this parameter * @param[in] newVal New value to set (set the nearest value if range is set) */ - void set(const TYPE& _newVal) { + void set(const TYPE& _newVal) override { if (m_min == m_max) { - if (_newVal != m_value) { - m_value = _newVal; - notifyChange(); + if (_newVal != eproperty::Value::m_value) { + eproperty::Value::m_value = _newVal; + eproperty::Value::notifyChange(); } } else { TYPE newVal = std::avg(m_min, _newVal, m_max); - if (newVal != m_value) { - m_value = newVal; - notifyChange(); + if (newVal != eproperty::Value::m_value) { + eproperty::Value::m_value = newVal; + eproperty::Value::notifyChange(); } } } - /** - * @brief Set the value of the current parameter (no check (for internal set with no check). - * @note For performence, this function must be inline - * @note Only use by the owner of the property (can not be check on compile time for now ...) - * TODO: Do it better ... compile check - * @param[in] newVal New value to set - */ - inline void setDirect(const TYPE& _newVal) { - m_value = _newVal; - }; - /** - * @brief Get the value of the current parameter (no check (for internal set with no check). - * @note For performence, this function must be inline - * @note Only use by the owner of the property (can not be check on compile time for now ...) - * TODO: Do it better ... compile check - * @return a reference on the value - */ - TYPE& getDirect() { - return m_value; - } - private: - /** - * @brief Get the string of the specify value. - * @return convetion of the velue in string. - */ - std::string getValueSpecific(const TYPE& _valueRequested) const { - return etk::to_string(_valueRequested); - } - public: - const Range& operator= (const TYPE& _newVal) = delete; - operator const TYPE&() const { - return m_value; - } - const TYPE& operator*() const noexcept { - return m_value; - } - const TYPE* operator->() const noexcept { - return &m_value; - } }; template std::ostream& operator <<(std::ostream& _os, const eproperty::Range& _obj) { diff --git a/eproperty/Value.h b/eproperty/Value.h index 97d8f5c..69faf92 100644 --- a/eproperty/Value.h +++ b/eproperty/Value.h @@ -7,15 +7,11 @@ */ #pragma once -#include -#include +#include namespace eproperty { - template class Value : public Property { - private: - TYPE m_value; //!< Current value. - TYPE m_default; //!< Default value. + template class Value : public PropertyType { public: /** * @brief Create a parameter with a specific type. @@ -31,110 +27,23 @@ namespace eproperty { const TYPE& _defaultValue, const std::string& _description = "", void (CLASS_TYPE::*_setObs)()=nullptr) : - Property(_owner, _name), - m_value(_defaultValue), - m_default(_defaultValue) { - if (_setObs != nullptr) { - setObserver([=](){(*_owner.*_setObs)();}); - } + eproperty::PropertyType(_owner, _name, _defaultValue, _description, _setObs) { + } - /** - * @brief Destructor. - */ - virtual ~Value() = default; - // herited methode - virtual std::string getPropertyType() const { - return "eproperty::Value"; - } - // herited methode - virtual std::string getType() const { - return typeid(TYPE).name(); - } - // herited methode - virtual std::string getString() const { - return getValueSpecific(m_value); - } - // herited methode - virtual std::string getDefault() const { - return getValueSpecific(m_default); - } - // herited methode - virtual void setString(const std::string& _newVal) { - // when you want to set an element in parameter you will implement the function template std::from_string - etk::from_string(m_value, _newVal); - // TODO : Do it better ... - notifyChange(); - } - // herited methode - virtual std::string getInfo() const { - return getType() + " default=" + getDefault(); - } - // herited methode - virtual bool isDefault() const { - return m_value == m_default; - } - // herited methode - virtual void setDefault() { - set(m_default); - } - public: - /** - * @brief Get the value of the current parameter. - * @note For performence, this function must be inline - * @return the Reference value - */ - const inline TYPE& get() const { - return m_value; - }; - /** - * @brief Set a new value for this parameter - * @param[in] newVal New value to set (set the nearest value if range is set) - */ - void set(const TYPE& _newVal) { - if (_newVal != m_value) { - m_value = _newVal; - notifyChange(); - } - } - /** - * @brief Set the value of the current parameter (no check (for internal set with no check). - * @note For performence, this function must be inline - * @note Only use by the owner of the property (can not be check on compile time for now ...) - * TODO: Do it better ... compile check - * @param[in] newVal New value to set - */ - inline void setDirect(const TYPE& _newVal) { - m_value = _newVal; - } - /** - * @brief Get the value of the current parameter (no check (for internal set with no check). - * @note For performence, this function must be inline - * @note Only use by the owner of the property (can not be check on compile time for now ...) - * TODO: Do it better ... compile check - * @return a reference on the value - */ - TYPE& getDirect() { - return m_value; - } - private: + protected: /** * @brief Get the string of the specify value. * @return convetion of the velue in string. */ - std::string getValueSpecific(const TYPE& _valueRequested) const { + std::string getValueSpecific(const TYPE& _valueRequested) const override { return etk::to_string(_valueRequested); } - public: - operator const TYPE&() const { - return m_value; + void setString(const std::string& _newVal) override { + // when you want to set an element in parameter you will implement the function template std::from_string + etk::from_string(eproperty::PropertyType::m_value, _newVal); + // TODO : Do it better ... + eproperty::PropertyType::notifyChange(); } - const TYPE& operator *() const noexcept { - return m_value; - } - const TYPE* operator->() const noexcept { - return &m_value; - } - const Value& operator= (const TYPE& _newVal) = delete; }; template std::ostream& operator <<(std::ostream& _os, const eproperty::Value& _obj) { diff --git a/eproperty/Variant.cpp b/eproperty/Variant.cpp new file mode 100644 index 0000000..53a920b --- /dev/null +++ b/eproperty/Variant.cpp @@ -0,0 +1,14 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2016, Edouard DUPIN, all right reserved + * + * @license APACHE v2.0 (see license file) + */ + +#include +#include + +eproperty::Variant::~Variant() { + +} diff --git a/eproperty/Variant.h b/eproperty/Variant.h new file mode 100644 index 0000000..d08d4b5 --- /dev/null +++ b/eproperty/Variant.h @@ -0,0 +1,28 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2016, Edouard DUPIN, all right reserved + * + * @license APACHE v2.0 (see license file) + */ +#pragma once + +#include +#include +#include +#include + + +namespace eproperty { + class Variant { + private: + std::unique_ptr m_value; + public: + template + Variant(TYPE&& _value): + m_value() { + eproperty::VariantSpecify* _val = new eproperty::VariantSpecify(); + } + ~Variant(); + }; +} diff --git a/eproperty/VariantBase.cpp b/eproperty/VariantBase.cpp new file mode 100644 index 0000000..e69de29 diff --git a/eproperty/VariantBase.h b/eproperty/VariantBase.h new file mode 100644 index 0000000..1fcd1b1 --- /dev/null +++ b/eproperty/VariantBase.h @@ -0,0 +1,25 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2016, Edouard DUPIN, all right reserved + * + * @license APACHE v2.0 (see license file) + */ +#pragma once + +#include +#include + +namespace eproperty { + class VariantBase { + protected: + uint64_t m_type; + public: + VariantBase(const uint64_t& _type) : + m_type(_type) { + + } + virtual ~VariantBase() = default; + }; +} + diff --git a/lutin_eproperty-test.py b/lutin_eproperty-test.py new file mode 100644 index 0000000..ce4e315 --- /dev/null +++ b/lutin_eproperty-test.py @@ -0,0 +1,39 @@ +#!/usr/bin/python +import lutin.module as module +import lutin.tools as tools +import datetime + + +def get_type(): + return "BINARY" + +def get_sub_type(): + return "TEST" + +def get_desc(): + return "e-property test software" + +def get_licence(): + return "APACHE-2" + +def get_compagny_type(): + return "com" + +def get_compagny_name(): + return "atria-soft" + +def get_maintainer(): + return ["Mr DUPIN Edouard "] + +def create(target, module_name): + my_module = module.Module(__file__, module_name, get_type()) + my_module.add_src_file([ + 'test/main.cpp', + 'test/declareSignals.cpp', + 'test/test_list.cpp', + 'test/test_range.cpp', + 'test/test_value.cpp' + ]) + my_module.add_module_depend(['eproperty', 'gtest', 'test-debug']) + return my_module + diff --git a/lutin_eproperty.py b/lutin_eproperty.py index 1a7850d..a437162 100644 --- a/lutin_eproperty.py +++ b/lutin_eproperty.py @@ -33,14 +33,18 @@ def create(target, module_name): 'eproperty/debug.cpp', 'eproperty/Property.cpp', 'eproperty/Interface.cpp', + 'eproperty/Variant.cpp', ]) my_module.add_header_file([ 'eproperty/debug.h', 'eproperty/Value.h', 'eproperty/Interface.h', 'eproperty/Property.h', + 'eproperty/PropertyType.h', 'eproperty/Range.h', - 'eproperty/List.h' + 'eproperty/List.h', + 'eproperty/Variant.h', + 'eproperty/VariantBase.h' ]) my_module.add_module_depend(['etk']) my_module.add_path(tools.get_current_path(__file__)) diff --git a/test/main.cpp b/test/main.cpp new file mode 100644 index 0000000..d2ecb42 --- /dev/null +++ b/test/main.cpp @@ -0,0 +1,32 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2016, Edouard DUPIN, all right reserved + * + * @license APACHE v2.0 (see license file) + */ +#define NAME "Empty" + +#include +#include +#include +#include + +#undef __class__ +#define __class__ "eproperty-test" + +int main(int _argc, const char *_argv[]) { + ::testing::InitGoogleTest(&_argc, const_cast(_argv)); + etk::init(_argc, _argv); + for (int32_t iii=0; iii<_argc ; ++iii) { + std::string data = _argv[iii]; + if ( data == "-h" + || data == "--help") { + TEST_PRINT("eproperty-test - help : "); + TEST_PRINT(" " << _argv[0] << " [options]"); + TEST_PRINT(" No optiions ..."); + return -1; + } + } + return RUN_ALL_TESTS(); +} diff --git a/test/test_list.cpp b/test/test_list.cpp new file mode 100644 index 0000000..e69de29 diff --git a/test/test_range.cpp b/test/test_range.cpp new file mode 100644 index 0000000..e69de29 diff --git a/test/test_value.cpp b/test/test_value.cpp new file mode 100644 index 0000000..e69de29