[DEV] new throw implementation

This commit is contained in:
Edouard DUPIN 2018-06-22 22:47:41 +02:00
parent 0cf9706fa8
commit 9b12492ff9
7 changed files with 110 additions and 60 deletions

View File

@ -12,6 +12,7 @@
#include <echrono/Duration.hpp>
#include <etk/os/FSNode.hpp>
#include <etk/Allocator.hpp>
//#include <etk/os/FSNode.hpp>
static int32_t nbTimeInit = 0;
@ -97,7 +98,7 @@ void etest::init(int32_t _argc, const char** _argv) {
}
nbTimeInit++;
elog::init(_argc, _argv);
etk::initDefaultFolder("ewolApplNoName");
//etk::initDefaultFolder("ewolApplNoName");
ETEST_INFO("ETEST system init (BEGIN) ");
for (int32_t iii=0; iii<_argc ; ++iii) {
etk::String data = _argv[iii];

View File

@ -260,7 +260,7 @@ namespace etk {
Array(const ETK_ARRAY_TYPE_2& ... _args) {
int32_t nbElement = int32_t(sizeof...(ETK_ARRAY_TYPE_2));
if (nbElement >= m_allocated) {
throw etk::exception::InvalidArgument("Size too big ...");
ETK_THROW_EXCEPTION(etk::exception::InvalidArgument("Size too big ..."));
return;
}
pushBackN(_args...);
@ -442,7 +442,7 @@ namespace etk {
*/
void pushBack(ETK_ARRAY_TYPE&& _item) {
if (m_size == ETK_ARRAY_SIZE) {
throw etk::exception::OverflowError("try add to much data in array");
ETK_THROW_EXCEPTION(etk::exception::OverflowError("try add to much data in array"));
}
new ((char*)&m_data[m_size]) ETK_ARRAY_TYPE(etk::move(_item));
m_size += 1;
@ -453,7 +453,7 @@ namespace etk {
*/
void pushBack(const ETK_ARRAY_TYPE& _item) {
if (m_size == ETK_ARRAY_SIZE) {
throw etk::exception::OverflowError("try add to much data in array");
ETK_THROW_EXCEPTION(etk::exception::OverflowError("try add to much data in array"));
}
new ((char*)&m_data[m_size]) ETK_ARRAY_TYPE(etk::move(_item));
m_size += 1;
@ -468,7 +468,7 @@ namespace etk {
return;
}
if (m_size+_nbElement > ETK_ARRAY_SIZE) {
throw etk::exception::OverflowError("try add to much data in array");
ETK_THROW_EXCEPTION(etk::exception::OverflowError("try add to much data in array"));
}
for (size_t iii=0; iii<_nbElement; iii++) {
new ((char*)&m_data[m_size+iii]) ETK_ARRAY_TYPE(_item[iii]);
@ -573,7 +573,7 @@ namespace etk {
return;
}
if (m_size+_nbElement > ETK_ARRAY_SIZE) {
throw etk::exception::OverflowError("try add to much data in array");
ETK_THROW_EXCEPTION(etk::exception::OverflowError("try add to much data in array"));
}
if (_pos + _nbElement > m_size) {
_nbElement = m_size - _pos;
@ -687,7 +687,7 @@ namespace etk {
*/
void resize(size_t _newSize, const ETK_ARRAY_TYPE& _basicElement) {
if (_newSize > ETK_ARRAY_SIZE) {
throw etk::exception::OverflowError("try resize with larger size in array");
ETK_THROW_EXCEPTION(etk::exception::OverflowError("try resize with larger size in array"));
}
// Reallocate memory
if (_newSize > m_size) {
@ -718,7 +718,7 @@ namespace etk {
void resize(size_t _newSize) {
ETK_ARRAY_DEBUG("Resize %zu => %zu\n", m_size, _newSize);
if (_newSize > ETK_ARRAY_SIZE) {
throw etk::exception::OverflowError("try resize with larger size in array");
ETK_THROW_EXCEPTION(etk::exception::OverflowError("try resize with larger size in array"));
}
// Reallocate memory
if (_newSize > m_size) {

View File

@ -7,15 +7,13 @@
#include <etk/String.hpp>
etk::Exception::Exception():
m_type("UNKNOW"),
m_what("? ? ?"),
m_function(null) {
m_type("UNKNOW") {
}
etk::Exception::Exception(const char* _type, const etk::String& _what, const char* _function):
etk::Exception::Exception(const char* _type, const etk::String& _what):
m_type(_type),
m_what(_what),
m_function(_function) {
m_what(_what) {
}
@ -27,19 +25,48 @@ const etk::String etk::Exception::what() const {
return m_what;
}
const char* etk::Exception::where() const {
const char* etk::Exception::file() const {
return m_file;
}
size_t etk::Exception::line() const {
return m_line;
}
const char* etk::Exception::function() const {
return m_function;
}
etk::Exception& etk::Exception::setFile(const char* _value) {
m_file = _value;
return *this;
}
etk::Exception& etk::Exception::setLine(size_t _value) {
m_line = _value;
return *this;
}
etk::Exception& etk::Exception::setFunction(const char* _value) {
m_function = _value;
return *this;
}
etk::String etk::Exception::toString() const {
etk::String out = "exception{";
out += m_type;
out += ":";
out += m_what;
if (m_function != null) {
out += " in ";
out += " on ";
out += m_function;
}
if (m_file != null) {
out += " in ";
out += m_file;
out += ":";
out += m_line;
}
out += "}";
return out;
}

View File

@ -14,9 +14,11 @@ namespace etk {
*/
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
const char* m_type = null; //!< type of the exception
const etk::String m_what = "? ? ?"; //!< problem exception type
const char* m_function = null; //!< Function where the exception is generated
const char* m_file = null; //!< File where the excepton append
size_t m_line = 0; //!< Line of the exception
public:
/**
* @brief Default constructor.
@ -26,9 +28,11 @@ namespace etk {
* @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.
* @param[in] _function Function name to find faster the source of the problem.
* @param[in] _file File of the exception throw.
* @param[in] _line Line of the file throw.
*/
Exception(const char* _type, const etk::String& _what, const char* _function = null);
Exception(const char* _type, const etk::String& _what);
/**
* @brief virtualize destructor.
*/
@ -43,17 +47,44 @@ namespace etk {
* @return Descriptive string.
*/
const etk::String what() const;
/**
* @brief Get the Name of the file where the exception is generated.
* @return File string or null
*/
const char* file() const;
/**
* @brief Set the file Name.
* @param[in] _value File string or null
*/
etk::Exception& setFile(const char* _value);
/**
* @brief Get the Name of the fuction where the exception is generated.
* @return Function string or null
*/
const char* where() const;
const char* function() const;
/**
* @brief Set the function Name.
* @param[in] _value Function string or null
*/
etk::Exception& setFunction(const char* _value);
/**
* @brief Line Index in the file where thre throw append
* @return Line in the file
*/
size_t line() const;
/**
* @brief Set the file line.
* @param[in] _value File line.
*/
etk::Exception& setLine(size_t _value);
/**
* @brief Convert the class in String.
* @return generating desription of class
*/
etk::String toString() const;
};
#define ETK_THROW_EXCEPTION(ex) throw ex.setFunction(__PRETTY_FUNCTION__).setFile(__FILE__).setLine(__LINE__)
//! @brief Generic
namespace exception {
class InvalidArgument : public etk::Exception {
@ -61,10 +92,9 @@ namespace etk {
/**
* @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 = null):
etk::Exception("INVALID-ARGUMENT", _what, _function) {
InvalidArgument(const etk::String& _what):
etk::Exception("INVALID-ARGUMENT", _what) {
}
};
@ -73,10 +103,9 @@ namespace etk {
/**
* @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 = null):
etk::Exception("DOMAIN-ERROR", _what, _function) {
DomainError(const etk::String& _what):
etk::Exception("DOMAIN-ERROR", _what) {
}
};
@ -85,10 +114,9 @@ namespace etk {
/**
* @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 = null):
etk::Exception("LENGTH-ERROR", _what, _function) {
LengthError(const etk::String& _what):
etk::Exception("LENGTH-ERROR", _what) {
}
};
@ -97,10 +125,9 @@ namespace etk {
/**
* @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 = null):
etk::Exception("OUT-OF-RANGE", _what, _function) {
OutOfRange(const etk::String& _what):
etk::Exception("OUT-OF-RANGE", _what) {
}
};
@ -109,10 +136,9 @@ namespace etk {
/**
* @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 = null):
etk::Exception("RANGE-ERROR", _what, _function) {
RangeError(const etk::String& _what):
etk::Exception("RANGE-ERROR", _what) {
}
};
@ -121,10 +147,9 @@ namespace etk {
/**
* @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 = null):
etk::Exception("OVERFLOW-ERROR", _what, _function) {
OverflowError(const etk::String& _what):
etk::Exception("OVERFLOW-ERROR", _what) {
}
};
@ -133,10 +158,9 @@ namespace etk {
/**
* @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 = null):
etk::Exception("UNDERFLOW-ERROR", _what, _function) {
UnderflowError(const etk::String& _what):
etk::Exception("UNDERFLOW-ERROR", _what) {
}
};
@ -145,10 +169,9 @@ namespace etk {
/**
* @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 = null):
etk::Exception("CAST-ERROR", _what, _function) {
CastError(const etk::String& _what):
etk::Exception("CAST-ERROR", _what) {
}
};
@ -157,10 +180,9 @@ namespace etk {
/**
* @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 = null):
etk::Exception("ALLOCATION-ERROR", _what, _function) {
AllocationError(const etk::String& _what):
etk::Exception("ALLOCATION-ERROR", _what) {
}
};
@ -169,10 +191,9 @@ namespace etk {
/**
* @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.
*/
RuntimeError(const etk::String& _what, const char* _function = null):
etk::Exception("RUNTIME-ERROR", _what, _function) {
RuntimeError(const etk::String& _what):
etk::Exception("RUNTIME-ERROR", _what) {
}
};
@ -181,10 +202,9 @@ namespace etk {
/**
* @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.
*/
NullPointerError(const etk::String& _what, const char* _function = null):
etk::Exception("NULL-POINTER-ERROR", _what, _function) {
NullPointerError(const etk::String& _what):
etk::Exception("NULL-POINTER-ERROR", _what) {
}
};

View File

@ -156,7 +156,7 @@ namespace etk {
if ( m_pointerPrivate == null
&& m_local == false) {
ETK_FUNCTION_DEBUG("[%d=0X%lx] call Function (With null !!! ==> must assert ...)\n", m_pppppp, (uint64_t)this);
throw etk::exception::NullPointerError("etk::Function call empty pointer");
ETK_THROW_EXCEPTION(etk::exception::NullPointerError("etk::Function call empty pointer"));
}
ETK_FUNCTION_DEBUG("[%d=0X%lx] call Function \n", m_pppppp, (uint64_t)this);
if (m_local == true) {

View File

@ -9,6 +9,7 @@
#include <etk/Pair.hpp>
#include <etk/Vector.hpp>
#include <etk/Allocator.hpp>
#include <etk/Exception.hpp>
namespace etk {
/**
@ -403,11 +404,9 @@ namespace etk {
* @return Reference on the Element
*/
ETK_MAP_TYPE_DATA& get(const ETK_MAP_TYPE_KEY& _key) const {
static ETK_MAP_TYPE_DATA g_error;
int64_t elementId = getId(_key);
if (elementId<0) {
//TK_ERROR("try to access at an inexistent Map element : " << _key);
return g_error;
ETK_THROW_EXCEPTION(etk::exception::InvalidArgument("Can not find the key: " + etk::toString(_key)));
}
return m_data[elementId]->second;
}

View File

@ -793,9 +793,10 @@ namespace etk {
/**
* @brief Read all the data from a file
* @param[in] _path Folder/File/Pipe path of the node
* @param[in] _offset Start offset in byte in the file
* @return all the data of the file in a typed vector
*/
template<typename TTT> etk::Vector<TTT> FSNodeReadAllDataType(const etk::String& _path) {
template<typename TTT> etk::Vector<TTT> FSNodeReadAllDataType(const etk::String& _path, int32_t _offset=0) {
etk::Vector<TTT> out;
etk::FSNode node(_path);
if (node.fileOpenRead() == false) {
@ -803,10 +804,12 @@ namespace etk {
return out;
}
uint64_t nbByte = node.fileSize();
nbByte -= _offset;
out.resize(nbByte/sizeof(TTT));
if (out.size()*sizeof(TTT) != nbByte) {
//TK_WARNING("Error in reading the file missing some byte at the end ...");
}
node.fileSeek(_offset, seekNode_start);
node.fileRead(&out[0], sizeof(TTT), nbByte/sizeof(TTT));
node.fileClose();
return out;