From 5b0b9b78372337174c70bcb46e7860bde09c2cba Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Tue, 26 Sep 2017 09:31:47 +0200 Subject: [PATCH] [DEV] add axception --- etk/Exception.cpp | 45 ++++++++++++ etk/Exception.hpp | 169 ++++++++++++++++++++++++++++++++++++++++++++ etk/Pair.hpp | 1 - etk/String.cpp | 10 ++- etk/String.hpp | 7 +- etk/UString.cpp | 4 ++ etk/UString.hpp | 5 ++ etk/Vector.hpp | 26 +++---- etk/typeInfo.cpp | 14 ++++ etk/typeTrait.hpp | 33 ++++----- etk/types.hpp | 22 +++++- lutin_etk-base.py | 2 + test/testMap.cpp | 19 +++++ test/testVector.cpp | 21 ++++++ 14 files changed, 337 insertions(+), 41 deletions(-) create mode 100644 etk/Exception.cpp create mode 100644 etk/Exception.hpp diff --git a/etk/Exception.cpp b/etk/Exception.cpp new file mode 100644 index 0000000..b1ccb1b --- /dev/null +++ b/etk/Exception.cpp @@ -0,0 +1,45 @@ +/** + * @author Edouard DUPIN + * @copyright 2017, Edouard DUPIN, all right reserved + * @license MPL-2 (see license file) + */ +#include +#include + +etk::Exception::Exception(): + m_type("UNKNOW"), + m_what("? ? ?"), + m_function(nullptr) { + +} +etk::Exception::Exception(const char* _type, const etk::String& _what, const char* _function): + m_type(_type), + m_what(_what), + m_function(_function) { + +} + +const char* etk::Exception::which() const { + return m_type; +} + +const etk::String etk::Exception::what() const { + return m_what; +} + +const char* etk::Exception::where() const { + return m_function; +} + +etk::String etk::Exception::toString() const { + etk::String out = "exception{"; + out += m_type; + out += ":"; + out += m_what; + if (m_function != nullptr) { + out += " in "; + out += m_function; + } + out += "}"; + return out; +} diff --git a/etk/Exception.hpp b/etk/Exception.hpp new file mode 100644 index 0000000..62033fb --- /dev/null +++ b/etk/Exception.hpp @@ -0,0 +1,169 @@ +/** + * @author Edouard DUPIN + * @copyright 2017, Edouard DUPIN, all right reserved + * @license MPL-2 (see license file) + */ +#pragma once +#include +#include + + +namespace etk { + //class String; + /** + * @brief Global exception handle + */ + class Exception { + private: + const char* m_type; //!< type of the exception + const etk::String m_what; //!< problem exception type + const char* m_function; //!< Function where the exception is generated + public: + /** + * @brief Default constructor. + */ + Exception(); + /** + * @brief Generic Constructor. + * @param[in] _type Type of the exception + * @param[in] _what The explanation of the problem. + * @param[in] _function Function name to find faster the source od the problem. + */ + Exception(const char* _type, const etk::String& _what, const char* _function = nullptr); + /** + * @brief virtualize destructor. + */ + virtual ~Exception() = default; + /** + * @brief Get the type of the exception. + * @return String name of the exception. + */ + const char* which() const; + /** + * @brief Get the explanation of the problem. + * @return Descriptive string. + */ + const etk::String what() const; + /** + * @brief Get the Name of the fuction where the exception is generated. + * @return Function string or nullptr + */ + const char* where() const; + /** + * @brief Convert the class in String. + * @return generating desription of class + */ + etk::String toString() const; + }; + //! @brief Generic + namespace exception { + class InvalidArgument : public etk::Exception { + public: + /** + * @brief Contructor of an generic Exception. + * @param[in] _what The explanation of the problem. + * @param[in] _function Function name to find faster the source od the problem. + */ + InvalidArgument(const etk::String& _what, const char* _function = nullptr): + etk::Exception("INVALID-ARGUMENT", _what, _function) { + + } + }; + class DomainError : public etk::Exception { + public: + /** + * @brief Contructor of an generic Exception. + * @param[in] _what The explanation of the problem. + * @param[in] _function Function name to find faster the source od the problem. + */ + DomainError(const etk::String& _what, const char* _function = nullptr): + etk::Exception("DOMAIN-ERROR", _what, _function) { + + } + }; + class LengthError : public etk::Exception { + public: + /** + * @brief Contructor of an generic Exception. + * @param[in] _what The explanation of the problem. + * @param[in] _function Function name to find faster the source od the problem. + */ + LengthError(const etk::String& _what, const char* _function = nullptr): + etk::Exception("LENGTH-ERROR", _what, _function) { + + } + }; + class OutOfRange : public etk::Exception { + public: + /** + * @brief Contructor of an generic Exception. + * @param[in] _what The explanation of the problem. + * @param[in] _function Function name to find faster the source od the problem. + */ + OutOfRange(const etk::String& _what, const char* _function = nullptr): + etk::Exception("OUT-OF-RANGE", _what, _function) { + + } + }; + class RangeError : public etk::Exception { + public: + /** + * @brief Contructor of an generic Exception. + * @param[in] _what The explanation of the problem. + * @param[in] _function Function name to find faster the source od the problem. + */ + RangeError(const etk::String& _what, const char* _function = nullptr): + etk::Exception("RANGE-ERROR", _what, _function) { + + } + }; + class OverflowError : public etk::Exception { + public: + /** + * @brief Contructor of an generic Exception. + * @param[in] _what The explanation of the problem. + * @param[in] _function Function name to find faster the source od the problem. + */ + OverflowError(const etk::String& _what, const char* _function = nullptr): + etk::Exception("OVERFLOW-ERROR", _what, _function) { + + } + }; + class UnderflowError : public etk::Exception { + public: + /** + * @brief Contructor of an generic Exception. + * @param[in] _what The explanation of the problem. + * @param[in] _function Function name to find faster the source od the problem. + */ + UnderflowError(const etk::String& _what, const char* _function = nullptr): + etk::Exception("UNDERFLOW-ERROR", _what, _function) { + + } + }; + class CastError : public etk::Exception { + public: + /** + * @brief Contructor of an generic Exception. + * @param[in] _what The explanation of the problem. + * @param[in] _function Function name to find faster the source od the problem. + */ + CastError(const etk::String& _what, const char* _function = nullptr): + etk::Exception("CAST-ERROR", _what, _function) { + + } + }; + class AllocationError : public etk::Exception { + public: + /** + * @brief Contructor of an generic Exception. + * @param[in] _what The explanation of the problem. + * @param[in] _function Function name to find faster the source od the problem. + */ + AllocationError(const etk::String& _what, const char* _function = nullptr): + etk::Exception("ALLOCATION-ERROR", _what, _function) { + + } + }; + } +} diff --git a/etk/Pair.hpp b/etk/Pair.hpp index 9cbc228..8c5f538 100644 --- a/etk/Pair.hpp +++ b/etk/Pair.hpp @@ -9,7 +9,6 @@ #include namespace etk { - template class Pair { public: diff --git a/etk/String.cpp b/etk/String.cpp index ba77715..230ec6e 100644 --- a/etk/String.cpp +++ b/etk/String.cpp @@ -204,6 +204,10 @@ void etk::String::popBack() { } } +void etk::String::reserve(size_t _size) { + m_data.reserve(_size+1); +} + void etk::String::clear() { resize(0); } @@ -576,12 +580,6 @@ bool etk::String::to() const { } return false; } - -etk::Stream& etk::operator <<(etk::Stream& _os, const etk::String& _obj) { - _os << _obj.c_str(); - return _os; -} - void etk::sort(etk::Vector &_list) { etk::Vector tmpList(_list); _list.clear(); diff --git a/etk/String.hpp b/etk/String.hpp index 6e04c41..29437bd 100644 --- a/etk/String.hpp +++ b/etk/String.hpp @@ -351,6 +351,11 @@ namespace etk { * @brief Remove the last element of the string */ void popBack(); + /** + * @brief Force the container to have a minimum size in memory allocation + * @param[in] _size Size in byte that is requested. + */ + void reserve(size_t _size); /** * @brief Remove all element in the current string */ @@ -521,8 +526,6 @@ namespace etk { */ template etk::String toString(const TYPE& _variable); - //! @not_in_doc - etk::Stream& operator <<(etk::Stream& _os, const etk::String& _obj); /** * @brief Template to declare conversion from string to anything * @param[out] _variableRet Output value diff --git a/etk/UString.cpp b/etk/UString.cpp index 0f220e5..1e1ff6d 100644 --- a/etk/UString.cpp +++ b/etk/UString.cpp @@ -196,6 +196,10 @@ void etk::UString::popBack() { } } +void etk::UString::reserve(size_t _size) { + m_data.reserve(_size+1); +} + void etk::UString::clear() { resize(0); } diff --git a/etk/UString.hpp b/etk/UString.hpp index 9d8262b..51b6a9a 100644 --- a/etk/UString.hpp +++ b/etk/UString.hpp @@ -350,6 +350,11 @@ namespace etk { * @brief Remove the last element of the string */ void popBack(); + /** + * @brief Force the container to have a minimum size in memory allocation + * @param[in] _size Size in byte that is requested. + */ + void reserve(size_t _size); /** * @brief Remove all element in the current string */ diff --git a/etk/Vector.hpp b/etk/Vector.hpp index 9776c1b..8d2d2d8 100644 --- a/etk/Vector.hpp +++ b/etk/Vector.hpp @@ -235,6 +235,7 @@ namespace etk { m_data(nullptr), m_size(0), m_allocated(0) { + changeAllocation(int32_t(sizeof...(ETK_VECTOR_TYPE_2))); pushBackN(_args...); } /** @@ -697,6 +698,16 @@ namespace etk { } m_size = _newSize; } + /** + * @brief Force the container to have a minimum size in memory allocation + * @param[in] _size Size in byte that is requested. + */ + void reserve(size_t _size) { + if (_size <= m_allocated) { + return; + } + changeAllocation(_size); + } private: /** * @brief Change the current allocation to the correct one (depend on the current size) @@ -867,19 +878,4 @@ namespace etk { } return false; } - class Stream; - //! @not_in_doc - template - etk::Stream& operator <<(etk::Stream& _os, const etk::Vector& _obj) { - _os << "{"; - for (size_t iii=0; iii< _obj.size(); iii++) { - if (iii>0) { - _os << ";"; - } - _os << _obj[iii]; - } - _os << "}"; - return _os; - } - } diff --git a/etk/typeInfo.cpp b/etk/typeInfo.cpp index c2eddba..b3a0593 100644 --- a/etk/typeInfo.cpp +++ b/etk/typeInfo.cpp @@ -44,3 +44,17 @@ ETK_DECLARE_TYPE(etk::UString); ETK_DECLARE_TYPE(etk::Stream); ETK_DECLARE_TYPE(etk::NullPtr); +#include + +ETK_DECLARE_TYPE(etk::Exception); +ETK_DECLARE_TYPE(etk::exception::InvalidArgument); +ETK_DECLARE_TYPE(etk::exception::DomainError); +ETK_DECLARE_TYPE(etk::exception::LengthError); +ETK_DECLARE_TYPE(etk::exception::OutOfRange); +ETK_DECLARE_TYPE(etk::exception::RangeError); +ETK_DECLARE_TYPE(etk::exception::OverflowError); +ETK_DECLARE_TYPE(etk::exception::UnderflowError); +ETK_DECLARE_TYPE(etk::exception::CastError); +ETK_DECLARE_TYPE(etk::exception::AllocationError); + + diff --git a/etk/typeTrait.hpp b/etk/typeTrait.hpp index c5eabbc..1e50d35 100644 --- a/etk/typeTrait.hpp +++ b/etk/typeTrait.hpp @@ -156,25 +156,26 @@ namespace etk { }; #define DEFINE_METHOD_CHECKER(RETURN_TYPE, METHOD_NAME, PARAMETERS) \ - template \ - struct Is_ ## METHOD_NAME ## _MemberFunctionExists { \ - private: \ - typedef char True; \ - typedef char (&False)[2]; \ - template \ - struct Checker { \ - typedef True Type; \ - }; \ - template \ - static typename Checker::Type Tester(const U*); \ - static False Tester(...); \ - public: \ - enum { value = (sizeof(Tester(static_cast(0))) == sizeof(True)) }; \ - } + template \ + struct Is_ ## METHOD_NAME ## _MemberFunctionExists { \ + private: \ + typedef char True; \ + typedef char (&False)[2]; \ + template \ + struct Checker { \ + typedef True Type; \ + }; \ + template \ + static typename Checker::Type Tester(const U*); \ + static False Tester(...); \ + public: \ + enum { value = (sizeof(Tester(static_cast(0))) == sizeof(True)) }; \ + } \ // Use example: // Is_swap_MemberFunctionExists::value DEFINE_METHOD_CHECKER(void, swap, (ETK_TYPE&)); - + class String; + DEFINE_METHOD_CHECKER(etk::String, toString, ()); /* #if 0 diff --git a/etk/types.hpp b/etk/types.hpp index 57c4b9c..cc85aba 100644 --- a/etk/types.hpp +++ b/etk/types.hpp @@ -56,6 +56,26 @@ extern "C" { #define UINT64_MAX (__UINT64_C(18446744073709551615)) #endif +#ifndef FLT_EPSILON + #define FLT_EPSILON (1.192092896e-07f) +#endif +#ifndef FLT_MAX + #define FLT_MAX (3.402823466e+38f) +#endif +#ifndef FLT_MIN + #define FLT_MIN (1.175494351e-38f) +#endif + +#ifndef DBL_EPSILON + #define DBL_EPSILON (2.2204460492503131e-016) +#endif +#ifndef DBL_MAX + #define DBL_MAX (1.7976931348623158e+308) +#endif +#ifndef DBL_MIN + #define DBL_MIN (2.2250738585072014e-308) +#endif + #ifndef ETK_BUILD_LINEARMATH //! @brief If not using linear math from bullet lib, we need to define the basic element of a btScalar (float) using btScalar = float; @@ -92,4 +112,4 @@ extern "C" { #include #include -#include +//#include diff --git a/lutin_etk-base.py b/lutin_etk-base.py index 4e9e79e..6c3e140 100644 --- a/lutin_etk-base.py +++ b/lutin_etk-base.py @@ -36,6 +36,7 @@ def configure(target, my_module): 'etk/Function.cpp', 'etk/Allocator.cpp', 'etk/typeInfo.cpp', + 'etk/Exception.cpp', ]) my_module.add_header_file([ @@ -55,6 +56,7 @@ def configure(target, my_module): 'etk/Function.hpp', 'etk/NullPtr.hpp', 'etk/typeInfo.hpp', + 'etk/Exception.hpp', ]) # build in C++ mode diff --git a/test/testMap.cpp b/test/testMap.cpp index c842fcf..aacb6ad 100644 --- a/test/testMap.cpp +++ b/test/testMap.cpp @@ -29,3 +29,22 @@ TEST(TestEtkMap, add_ordered) { EXPECT_EQ(testData[1], "c 1"); EXPECT_EQ(testData.getValue(0), "c 1"); } +/* +TEST(TestEtkMap, initialization_list) { + etk::Map testData({uint32_t(12),etk::String("12")},{uint32_t(11),etk::String("11")}); + EXPECT_EQ(testData.size(), 2); + EXPECT_EQ(testData[11], "11"); + EXPECT_EQ(testData[12], "12"); + EXPECT_EQ(testData.getValue(0), "11"); + EXPECT_EQ(testData.getValue(1), "12"); +} + +TEST(TestEtkMap, initialization_list_2) { + etk::Map testData = {{12,"12"},{11,"11"}}; + EXPECT_EQ(testData.size(), 2); + EXPECT_EQ(testData[11], "11"); + EXPECT_EQ(testData[12], "12"); + EXPECT_EQ(testData.getValue(0), "11"); + EXPECT_EQ(testData.getValue(1), "12"); +} +*/ diff --git a/test/testVector.cpp b/test/testVector.cpp index 389780d..71cb3a0 100644 --- a/test/testVector.cpp +++ b/test/testVector.cpp @@ -161,3 +161,24 @@ TEST(TestVector, popFront) { EXPECT_EQ(test[1], 8); } + +TEST(TestVector, initializationList_1) { + // Test contructor value + etk::Vector test = {1, 5, 6, 8}; + EXPECT_EQ(test.size(), 4); + EXPECT_EQ(test[0], 1); + EXPECT_EQ(test[1], 5); + EXPECT_EQ(test[2], 6); + EXPECT_EQ(test[3], 8); +} + +TEST(TestVector, initializationList_2) { + // Test contructor value + etk::Vector test(1, 5, 6, 8); + EXPECT_EQ(test.size(), 4); + EXPECT_EQ(test[0], 1); + EXPECT_EQ(test[1], 5); + EXPECT_EQ(test[2], 6); + EXPECT_EQ(test[3], 8); +} +