[DEV] add axception

This commit is contained in:
Edouard DUPIN 2017-09-26 09:31:47 +02:00
parent 4fe37acdf0
commit 5b0b9b7837
14 changed files with 337 additions and 41 deletions

45
etk/Exception.cpp Normal file
View File

@ -0,0 +1,45 @@
/**
* @author Edouard DUPIN
* @copyright 2017, Edouard DUPIN, all right reserved
* @license MPL-2 (see license file)
*/
#include <etk/Exception.hpp>
#include <etk/String.hpp>
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;
}

169
etk/Exception.hpp Normal file
View File

@ -0,0 +1,169 @@
/**
* @author Edouard DUPIN
* @copyright 2017, Edouard DUPIN, all right reserved
* @license MPL-2 (see license file)
*/
#pragma once
#include <etk/types.hpp>
#include <etk/String.hpp>
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) {
}
};
}
}

View File

@ -9,7 +9,6 @@
#include <etk/move.hpp>
namespace etk {
template<class ETK_PAIR_TYPE_1, class ETK_PAIR_TYPE_2>
class Pair {
public:

View File

@ -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<bool>() 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<etk::String *> &_list) {
etk::Vector<etk::String *> tmpList(_list);
_list.clear();

View File

@ -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 <class TYPE>
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

View File

@ -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);
}

View File

@ -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
*/

View File

@ -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<class ETK_VECTOR_TYPE>
etk::Stream& operator <<(etk::Stream& _os, const etk::Vector<ETK_VECTOR_TYPE>& _obj) {
_os << "{";
for (size_t iii=0; iii< _obj.size(); iii++) {
if (iii>0) {
_os << ";";
}
_os << _obj[iii];
}
_os << "}";
return _os;
}
}

View File

@ -44,3 +44,17 @@ ETK_DECLARE_TYPE(etk::UString);
ETK_DECLARE_TYPE(etk::Stream);
ETK_DECLARE_TYPE(etk::NullPtr);
#include <etk/Exception.hpp>
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);

View File

@ -156,25 +156,26 @@ namespace etk {
};
#define DEFINE_METHOD_CHECKER(RETURN_TYPE, METHOD_NAME, PARAMETERS) \
template<typename ETK_TYPE> \
struct Is_ ## METHOD_NAME ## _MemberFunctionExists { \
private: \
typedef char True; \
typedef char (&False)[2]; \
template<typename U, RETURN_TYPE (U::*)PARAMETERS = &U::METHOD_NAME> \
struct Checker { \
typedef True Type; \
}; \
template<typename U> \
static typename Checker<U>::Type Tester(const U*); \
static False Tester(...); \
public: \
enum { value = (sizeof(Tester(static_cast<const ETK_TYPE*>(0))) == sizeof(True)) }; \
}
template<typename ETK_TYPE> \
struct Is_ ## METHOD_NAME ## _MemberFunctionExists { \
private: \
typedef char True; \
typedef char (&False)[2]; \
template<typename U, RETURN_TYPE (U::*)PARAMETERS = &U::METHOD_NAME> \
struct Checker { \
typedef True Type; \
}; \
template<typename U> \
static typename Checker<U>::Type Tester(const U*); \
static False Tester(...); \
public: \
enum { value = (sizeof(Tester(static_cast<const ETK_TYPE*>(0))) == sizeof(True)) }; \
} \
// Use example:
// Is_swap_MemberFunctionExists<T>::value
DEFINE_METHOD_CHECKER(void, swap, (ETK_TYPE&));
class String;
DEFINE_METHOD_CHECKER(etk::String, toString, ());
/*
#if 0

View File

@ -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 <etk/stdTools.hpp>
#include <etk/move.hpp>
#include <etk/Stream.hpp>
//#include <etk/Stream.hpp>

View File

@ -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

View File

@ -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<uint32_t, etk::String> 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<uint32_t, etk::String> 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");
}
*/

View File

@ -161,3 +161,24 @@ TEST(TestVector, popFront) {
EXPECT_EQ(test[1], 8);
}
TEST(TestVector, initializationList_1) {
// Test contructor value
etk::Vector<int> 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<int> 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);
}