[DEV] add move & copy operator in Map

This commit is contained in:
Edouard DUPIN 2017-09-26 15:44:54 +02:00
parent aa572c8d98
commit d4cc68f7e3
9 changed files with 89 additions and 2 deletions

View File

@ -165,5 +165,17 @@ namespace etk {
} }
}; };
class RuntimeError : 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.
*/
RuntimeError(const etk::String& _what, const char* _function = nullptr):
etk::Exception("RUNTIME-ERROR", _what, _function) {
}
};
} }
} }

View File

@ -36,7 +36,7 @@ namespace etk {
class FunctionPrivateLambda<ETK_TYPE_FUNCTION_FUNCTOR, ETK_TYPE_FUNCTION_RETURN(ETK_TYPE_FUNCTION_ARGS...)>: class FunctionPrivateLambda<ETK_TYPE_FUNCTION_FUNCTOR, ETK_TYPE_FUNCTION_RETURN(ETK_TYPE_FUNCTION_ARGS...)>:
public FunctionPrivate<ETK_TYPE_FUNCTION_RETURN(ETK_TYPE_FUNCTION_ARGS...)> { public FunctionPrivate<ETK_TYPE_FUNCTION_RETURN(ETK_TYPE_FUNCTION_ARGS...)> {
private: private:
ETK_TYPE_FUNCTION_FUNCTOR m_dataPointer; mutable ETK_TYPE_FUNCTION_FUNCTOR m_dataPointer;
public: public:
FunctionPrivateLambda(ETK_TYPE_FUNCTION_FUNCTOR _functor): FunctionPrivateLambda(ETK_TYPE_FUNCTION_FUNCTOR _functor):
m_dataPointer(_functor) { m_dataPointer(_functor) {

View File

@ -244,12 +244,37 @@ namespace etk {
/** /**
* @brief Constructor of the Map table. * @brief Constructor of the Map table.
* @param[in] _count Number of basic element in the table. * @param[in] _count Number of basic element in the table.
* @param[in] _ordered select an ordered map or an onordered map.
*/ */
Map(int32_t _count = 0, bool _ordered=true) : Map(int32_t _count = 0, bool _ordered=true) :
m_data(_count), m_data(_count),
m_ordered(_ordered) { m_ordered(_ordered) {
// nothing to do // nothing to do
} }
/**
* @brief Move constructor
* @param[in] _obj Other Map to move
*/
Map(Map&& _obj):
m_data(0),
m_ordered(true) {
_obj.swap(*this);
}
/**
* @brief Copy constructor
* @param[in] _obj Other Map to copy
*/
Map(const Map& _obj) :
m_data(),
m_ordered(_obj.m_ordered) {
m_data.reserve(_obj.m_data.size());
for (auto &it : _obj.m_data) {
if (it == nullptr) {
continue;
}
m_data.pushBack(new etk::Pair<ETK_MAP_TYPE_KEY, ETK_MAP_TYPE_DATA>(it->first, it->second));
}
}
void setOrdered(bool _ordered) { void setOrdered(bool _ordered) {
m_ordered = _ordered; m_ordered = _ordered;
if (m_ordered == true) { if (m_ordered == true) {
@ -262,6 +287,31 @@ namespace etk {
~Map() { ~Map() {
clear(); clear();
} }
/**
* @brief Swap two map
* @param[in] _obj Other Map to swap with
*/
void swap(Map& _obj) {
etk::swap(m_data, _obj.m_data);
etk::swap(m_ordered, _obj.m_ordered);
}
/**
* @brief Move operator
* @param[in] _obj Other Map to move
*/
Map& operator=(Map&& _obj) {
_obj.swap(*this);
return *this;
}
/**
* @brief Copy operator
* @param[in] _obj Other Map to copy
*/
Map& operator=(const Map& _obj) {
Map tmp(_obj);
tmp.swap(*this);
return *this;
}
/** /**
* @brief Remove all entry in the Map table. * @brief Remove all entry in the Map table.
* @note It does not delete pointer if your value is a pointer type... * @note It does not delete pointer if your value is a pointer type...

View File

@ -7,6 +7,7 @@
#include <etk/types.hpp> #include <etk/types.hpp>
#include <etk/move.hpp> #include <etk/move.hpp>
#include <etk/Stream.hpp>
namespace etk { namespace etk {
template<class ETK_PAIR_TYPE_1, class ETK_PAIR_TYPE_2> template<class ETK_PAIR_TYPE_1, class ETK_PAIR_TYPE_2>

View File

@ -580,6 +580,12 @@ bool etk::String::to<bool>() const {
} }
return false; 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) { void etk::sort(etk::Vector<etk::String *> &_list) {
etk::Vector<etk::String *> tmpList(_list); etk::Vector<etk::String *> tmpList(_list);
_list.clear(); _list.clear();

View File

@ -519,6 +519,8 @@ namespace etk {
inline bool operator<= (const char* _left, const String& _right) { inline bool operator<= (const char* _left, const String& _right) {
return _right <= _left; return _right <= _left;
} }
//! @not_in_doc
etk::Stream& operator <<(etk::Stream& _os, const etk::String& _obj);
/** /**
* @brief Template to declare conversion from anything in etk::String * @brief Template to declare conversion from anything in etk::String
* @param[in] _variable Variable to convert * @param[in] _variable Variable to convert

View File

@ -7,6 +7,7 @@
#include <etk/types.hpp> #include <etk/types.hpp>
//#include <etk/debug.hpp> //#include <etk/debug.hpp>
#include <etk/Stream.hpp>
namespace etk { namespace etk {
class Stream; class Stream;
@ -878,4 +879,18 @@ namespace etk {
} }
return false; 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

@ -6,6 +6,7 @@
#pragma once #pragma once
#include <etk/types.hpp> #include <etk/types.hpp>
#include <etk/Stream.hpp>
extern "C" { extern "C" {
#include <math.h> #include <math.h>

View File

@ -56,5 +56,5 @@ ETK_DECLARE_TYPE(etk::exception::OverflowError);
ETK_DECLARE_TYPE(etk::exception::UnderflowError); ETK_DECLARE_TYPE(etk::exception::UnderflowError);
ETK_DECLARE_TYPE(etk::exception::CastError); ETK_DECLARE_TYPE(etk::exception::CastError);
ETK_DECLARE_TYPE(etk::exception::AllocationError); ETK_DECLARE_TYPE(etk::exception::AllocationError);
ETK_DECLARE_TYPE(etk::exception::RuntimeError);