Skip to content
Snippets Groups Projects
Commit 04f81b7f authored by Maxence Naud's avatar Maxence Naud
Browse files

[Doc] for _any class

parent 42a7741a
No related branches found
No related tags found
1 merge request!15Remove CParameter memory leak
Pipeline #30956 failed
...@@ -15,12 +15,13 @@ ...@@ -15,12 +15,13 @@
#include <typeinfo> // typeid #include <typeinfo> // typeid
#include <type_traits> // enable_if_t, decay_t, is_same, is_copy_constructible, remove_cv, remove_reference #include <type_traits> // std::enable_if_t, std::decay_t, std::is_same, std::is_copy_constructible, std::remove_cv, std::remove_reference
#include <assert.h> #include <assert.h>
#include <new> #include <new>
class _any { class _any {
private: private:
/// @brief Operation to perform on the object.
enum _Op { _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy }; enum _Op { _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy };
union _Arg { union _Arg {
...@@ -28,10 +29,15 @@ private: ...@@ -28,10 +29,15 @@ private:
_any* _M_any; _any* _M_any;
}; };
/// @brief Stored data without type information.
void* _M_data; void* _M_data;
/// @brief Member function to perform type-related computations on stored data.
void (*_M_manager)(_Op, const _any*, _Arg*); void (*_M_manager)(_Op, const _any*, _Arg*);
public: public:
/// @brief Class to centralize functions and type information in a memory efficient way.
/// @tparam Tp Decayed stored type.
template <typename Tp> template <typename Tp>
struct Manager { struct Manager {
static void manage(_Op which, const _any* __any, _Arg* __arg) { static void manage(_Op which, const _any* __any, _Arg* __arg) {
...@@ -66,8 +72,11 @@ private: ...@@ -66,8 +72,11 @@ private:
using _Decay_if_not_any = std::enable_if_t<!std::is_same<_VTp, _any>::value, _VTp>; using _Decay_if_not_any = std::enable_if_t<!std::is_same<_VTp, _any>::value, _VTp>;
public: public:
/// @brief Default constructor
_any() noexcept : _M_manager(nullptr) { } _any() noexcept : _M_manager(nullptr) { }
/// @brief Copy constructor
/// @param __other
_any(const _any& __other) _any(const _any& __other)
{ {
if (!__other._M_manager) if (!__other._M_manager)
...@@ -80,6 +89,8 @@ public: ...@@ -80,6 +89,8 @@ public:
} }
} }
/// @brief Move constructor
/// @param __other
_any(_any&& __other) _any(_any&& __other)
{ {
if (!__other._M_manager) if (!__other._M_manager)
...@@ -92,13 +103,16 @@ public: ...@@ -92,13 +103,16 @@ public:
} }
} }
/// @brief By-value constructor.
/// @tparam T Data type.
/// @tparam VT Decayed data type.
/// @param value
template<typename T, typename VT = _Decay_if_not_any<T>, std::enable_if_t<std::is_copy_constructible<VT>::value, bool> = true> template<typename T, typename VT = _Decay_if_not_any<T>, std::enable_if_t<std::is_copy_constructible<VT>::value, bool> = true>
explicit _any(T&& value) explicit _any(T&& value)
: _M_manager(&Manager<VT>::manage), : _M_manager(&Manager<VT>::manage),
_M_data(new VT{std::forward<T>(value)}) _M_data(new VT{std::forward<T>(value)})
{} {}
~_any() ~_any()
{ {
if(_M_manager) { if(_M_manager) {
...@@ -107,6 +121,8 @@ public: ...@@ -107,6 +121,8 @@ public:
} }
} }
/// @brief Access type id of the value currently stored
/// @return
const std::type_info& type() const const std::type_info& type() const
{ {
if (!_M_manager) if (!_M_manager)
...@@ -117,6 +133,10 @@ public: ...@@ -117,6 +133,10 @@ public:
} }
}; };
/// @brief Access value stored in the object converted in the template type if possible.
/// @tparam _ValueType
/// @param __any
/// @return Stored value.
template<typename _ValueType> template<typename _ValueType>
inline _ValueType any_cast(const _any& __any) inline _ValueType any_cast(const _any& __any)
{ {
......
...@@ -31,7 +31,7 @@ private: ...@@ -31,7 +31,7 @@ private:
assert((std::is_constructible<_ValueType, const _Up&>::value && "Template argument must be constructible from a const value.")); assert((std::is_constructible<_ValueType, const _Up&>::value && "Template argument must be constructible from a const value."));
assert(std::is_object<_Up>::value); assert(std::is_object<_Up>::value);
assert(__any.type() == typeid(_Up)); assert(__any.type() == typeid(_Up));
if (_any::Manager<_Up>::access(&__any)) { if (_any::Manager<_Up>::access(&__any)) { // assess if _any object is empty
return *static_cast<_ValueType*>(_any::Manager<_Up>::access(&__any)); return *static_cast<_ValueType*>(_any::Manager<_Up>::access(&__any));
} }
throw std::bad_cast(); throw std::bad_cast();
...@@ -101,7 +101,7 @@ private: ...@@ -101,7 +101,7 @@ private:
*/ */
std::map<std::string, std::string> m_Types; std::map<std::string, std::string> m_Types;
///\brief All parameters values concatenated in raw binary form. ///\brief All raw pointers to parameters values concatenated. Use custom any class compatible with C++14.
std::vector<_any> m_Buffer = {}; std::vector<_any> m_Buffer = {};
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment