// eventpp library // Copyright (C) 2018 Wang Qi (wqking) // Github: https://github.com/wqking/eventpp // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0 // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef ANYDATA_H_320827729782 #define ANYDATA_H_320827729782 #include #include #include #include #include namespace eventpp { namespace anydata_internal_ { template void funcDeleteObject(void * object) { delete static_cast(object); } template void funcFreeObject(void * object) { static_cast(object)->~T(); } template auto doFuncMoveConstruct(void * object, void * buffer) -> typename std::enable_if::value>::type { new (buffer) T(std::move(*(T *)object)); } template auto doFuncMoveConstruct(void * object, void * buffer) -> typename std::enable_if::value>::type { static_assert(std::is_move_constructible::value, "AnyData: object must be either copy or move constructible"); } template void funcMoveConstruct(void * object, void * buffer) { doFuncMoveConstruct(object, buffer); } struct AnyDataFunctions { void (*free)(void *); void (*moveConstruct)(void *, void *); }; template const AnyDataFunctions * doGetAnyDataFunctions() { static const AnyDataFunctions functions { &funcFreeObject, &funcMoveConstruct }; return &functions; } template struct RemoveCvRef { using Type = typename std::remove_cv::type>::type; }; template const AnyDataFunctions * getAnyDataFunctions() { using U = typename RemoveCvRef::Type; return doGetAnyDataFunctions(); } template struct MaxSizeOf; template struct MaxSizeOf { static constexpr std::size_t otherSize = MaxSizeOf::value; static constexpr std::size_t tSize = sizeof(T); static constexpr std::size_t value = tSize > otherSize ? tSize : otherSize; }; template struct MaxSizeOf { static constexpr std::size_t value = sizeof(T); }; class LargeData { public: template explicit LargeData(T && object) : data(), deleter() { using U = typename RemoveCvRef::Type; deleter = &funcDeleteObject; data = new U(std::forward(object)); } ~LargeData() { if(data != nullptr) { assert(deleter != nullptr); deleter(data); } } LargeData(LargeData && other) : data(), deleter() { std::swap(data, other.data); std::swap(deleter, other.deleter); } LargeData(const LargeData & other) = delete; LargeData & operator = (const LargeData & other) = delete; LargeData & operator = (LargeData && other) = delete; const void * getAddress() const { return data; } template bool isType() const { using U = typename RemoveCvRef::Type; return deleter == &funcDeleteObject; } private: void * data; void (*deleter)(void *); }; } //namespace anydata_internal_ template class AnyData { private: using LargeData = anydata_internal_::LargeData; static constexpr std::size_t maxSize = maxSize_ < sizeof(LargeData) ? sizeof(LargeData) : maxSize_; static_assert(maxSize > 0, "AnyData: maxSize must be greater than 0"); public: ~AnyData() { if(functions != nullptr) { functions->free(buffer.data()); } } template AnyData(T && object, typename std::enable_if<(sizeof(typename anydata_internal_::RemoveCvRef::Type) <= maxSize)>::type * = 0) : functions(anydata_internal_::getAnyDataFunctions()), buffer() { using U = typename std::remove_reference::type; new (buffer.data()) U(std::forward(object)); } template AnyData(T && object, typename std::enable_if<(sizeof(typename anydata_internal_::RemoveCvRef::Type) > maxSize)>::type * = 0) : functions(anydata_internal_::getAnyDataFunctions()), buffer() { new (buffer.data()) LargeData(std::forward(object)); } AnyData(AnyData && other) : functions(other.functions), buffer() { if(functions != nullptr) { functions->moveConstruct(other.buffer.data(), buffer.data()); } } AnyData(const AnyData & other) = delete; AnyData(AnyData & other) = delete; AnyData & operator = (const AnyData & other) = delete; AnyData & operator = (AnyData && other) = delete; template const T & get() const { return *(T *)getAddress(); } const void * getAddress() const { assert(functions != nullptr); if(! isLargerData()) { return buffer.data(); } else { return ((const LargeData *)buffer.data())->getAddress(); } } template bool isType() const { if(! isLargerData()) { return anydata_internal_::getAnyDataFunctions() == functions; } else { return ((const LargeData *)buffer.data())->isType(); } } template operator T & () const { return *(T *)getAddress(); } template operator T * () const { return (T *)getAddress(); } private: bool isLargerData() const { return functions == anydata_internal_::getAnyDataFunctions(); } private: const anydata_internal_::AnyDataFunctions * functions; std::array buffer; }; template constexpr std::size_t maxSizeOf() { return anydata_internal_::MaxSizeOf::value; } } //namespace eventpp #endif