diff --git a/include/aidge/utils/Any.hpp b/include/aidge/utils/Any.hpp index bbf83eecfa2a6d2af5a89258802986c25a65c9a6..d7ff22de23763e399d23fd7abb9e28a7a85aff5c 100644 --- a/include/aidge/utils/Any.hpp +++ b/include/aidge/utils/Any.hpp @@ -15,12 +15,13 @@ #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 <new> class _any { private: + /// @brief Operation to perform on the object. enum _Op { _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy }; union _Arg { @@ -28,10 +29,15 @@ private: _any* _M_any; }; + /// @brief Stored data without type information. void* _M_data; + + /// @brief Member function to perform type-related computations on stored data. void (*_M_manager)(_Op, const _any*, _Arg*); public: + /// @brief Class to centralize functions and type information in a memory efficient way. + /// @tparam Tp Decayed stored type. template <typename Tp> struct Manager { static void manage(_Op which, const _any* __any, _Arg* __arg) { @@ -66,8 +72,11 @@ private: using _Decay_if_not_any = std::enable_if_t<!std::is_same<_VTp, _any>::value, _VTp>; public: + /// @brief Default constructor _any() noexcept : _M_manager(nullptr) { } + /// @brief Copy constructor + /// @param __other _any(const _any& __other) { if (!__other._M_manager) @@ -80,6 +89,8 @@ public: } } + /// @brief Move constructor + /// @param __other _any(_any&& __other) { if (!__other._M_manager) @@ -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> explicit _any(T&& value) : _M_manager(&Manager<VT>::manage), _M_data(new VT{std::forward<T>(value)}) {} - ~_any() { if(_M_manager) { @@ -107,6 +121,8 @@ public: } } + /// @brief Access type id of the value currently stored + /// @return const std::type_info& type() const { if (!_M_manager) @@ -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> inline _ValueType any_cast(const _any& __any) { diff --git a/include/aidge/utils/CParameter.hpp b/include/aidge/utils/CParameter.hpp index c13d9ca74e7558b397030de4f285805c9210fb0c..75c2b57ac179300ec3efbb1195c434e0534b62f3 100644 --- a/include/aidge/utils/CParameter.hpp +++ b/include/aidge/utils/CParameter.hpp @@ -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_object<_Up>::value); 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)); } throw std::bad_cast(); @@ -101,7 +101,7 @@ private: */ 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 = {}; };