Files
externals/linux/include/ActivMiddleware/ActivBase/GenericMessages/Formats/Common/SmartPointerHelpers.h
T

314 lines
9.5 KiB
C++

/**
* @file include/ActivMiddleware/ActivBase/GenericMessages/Formats/Common/SmartPointerHelpers.h
*
* @brief Header for GenericMessages supported smart pointer type traits & helpers.
*/
#if (!defined ACTIV_BASE_GENERIC_MESSAGES_FORMATS_COMMON_SMART_POINTER_HELPERS_H)
#define ACTIV_BASE_GENERIC_MESSAGES_FORMATS_COMMON_SMART_POINTER_HELPERS_H
#include "ActivMiddleware/ActivBase/External.h"
#include "ActivMiddleware/CxxYZ/memory.h"
#include "ActivMiddleware/CxxYZ/type_traits.h"
#include ACTIV_BOOST_INCLUDE(move/make_unique.hpp)
#include ACTIV_BOOST_INCLUDE(smart_ptr/intrusive_ptr.hpp)
#include ACTIV_BOOST_INCLUDE(smart_ptr/intrusive_ref_counter.hpp)
#include ACTIV_BOOST_INCLUDE(smart_ptr/make_shared.hpp)
#include <memory>
//! Activ namespace.
namespace Activ
{
//! GenericMessages namespace.
namespace GenericMessages
{
namespace SmartPointerHelpers
{
namespace Detail
{
//! Is a type a supported smart pointer?
template<typename T>
class SmartPointerTraits
{
public:
typedef CxxYZ::false_type IsSupported;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T>
class SmartPointerTraitsBase
{
public:
typedef CxxYZ::true_type IsSupported;
// Note can't introspect use_count() with Boost TTI: in VS, use_count() is a member function of a base of shared_ptr
// and the introspection of has_member_function can't check bases.
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T>
class SmartPointerTraits<Boost::shared_ptr<T> > : public SmartPointerTraitsBase<Boost::shared_ptr<T> >
{
public:
typedef T ElementType;
typedef CxxYZ::true_type HasUseCount;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T>
class SmartPointerTraits<Boost::movelib::unique_ptr<T> > : public SmartPointerTraitsBase<Boost::movelib::unique_ptr<T> >
{
public:
typedef T ElementType;
typedef CxxYZ::false_type HasUseCount;
};
// ---------------------------------------------------------------------------------------------------------------------------------
#if (!defined ACTIV_BOOST_NO_CXX11_SMART_PTR)
template<typename T>
class SmartPointerTraits<std::shared_ptr<T>> : public SmartPointerTraitsBase<std::shared_ptr<T>>
{
public:
typedef T ElementType;
typedef CxxYZ::true_type HasUseCount;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T>
class SmartPointerTraits<std::unique_ptr<T>> : public SmartPointerTraitsBase<std::unique_ptr<T>>
{
public:
typedef T ElementType;
typedef CxxYZ::false_type HasUseCount;
};
#endif // (!defined ACTIV_BOOST_NO_CXX11_SMART_PTR)
// ---------------------------------------------------------------------------------------------------------------------------------
// Boost::intrusive_ptr support.
template<typename T>
class SmartPointerTraits<Boost::intrusive_ptr<T> > : public SmartPointerTraitsBase<Boost::intrusive_ptr<T> >
{
public:
typedef T ElementType;
// The pointer itself doesn't have use_count; it's intrusive in the pointee type.
typedef CxxYZ::false_type HasUseCount;
};
// ---------------------------------------------------------------------------------------------------------------------------------
// Faking Boost::optional to look like a smart pointer for our use cases.
template<typename T>
class SmartPointerTraits<Boost::optional<T> > : public SmartPointerTraitsBase<Boost::optional<T> >
{
public:
typedef T ElementType;
typedef CxxYZ::false_type HasUseCount;
};
} // namespace Detail
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
//! Is a type a supported smart pointer type?
template<typename T>
class IsSmartPointer : public Detail::SmartPointerTraits<T>::IsSupported { };
// ---------------------------------------------------------------------------------------------------------------------------------
//! Metafunction to get the pointee type from a smart pointer type.
template<typename T>
class GetElementType
{
public:
typedef typename Detail::SmartPointerTraits<T>::ElementType type;
};
// ---------------------------------------------------------------------------------------------------------------------------------
//! Does a smart pointer type have a use_count() member function?
template<typename T>
class HasUseCount : public Detail::SmartPointerTraits<T>::HasUseCount { };
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
//! Fuctor to determine if a smart pointer has unique access to the pointed object.
template<typename T, typename ENABLE = void>
class IsUnique
{
public:
ACTIV_BOOST_STATIC_ASSERT(IsSmartPointer<T>::value);
ACTIV_BOOST_STATIC_ASSERT(!HasUseCount<T>::value);
bool operator()(const T &p) const
{
return true;
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T>
class IsUnique<T, typename CxxYZ::enable_if<HasUseCount<T>::value>::type>
{
public:
bool operator()(const T &p) const
{
return (1 == p.use_count());
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T>
class IsUnique<Boost::intrusive_ptr<T> >
{
public:
bool operator()(const Boost::intrusive_ptr<T> &p) const
{
// Only supporting Boost's intrusive_ref_counter (or similar interface).
return p && (1 == p->use_count());
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
//! Remove one level of (smart) pointer from a type.
template<typename T, typename ENABLE = void>
class RemovePointer : public CxxYZ::remove_pointer<T> { };
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T>
class RemovePointer<T, typename CxxYZ::enable_if<IsSmartPointer<T>::value>::type>
{
public:
typedef typename GetElementType<T>::type type;
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
//! Fuctor to get a raw pointer from a smart pointer.
template<typename T>
class GetRawPointer
{
public:
ACTIV_BOOST_STATIC_ASSERT(IsSmartPointer<T>::value);
const typename GetElementType<T>::type *operator()(const T &p) const
{
// Hard to imagine addressof(*nullptr) isn't undefined behavior, so:
if (!!p)
return CxxYZ::addressof(*p);
else
return ACTIV_NULLPTR;
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
// Allocate a new object.
template<typename T>
class New
{
public:
ACTIV_BOOST_STATIC_ASSERT(IsSmartPointer<T>::value);
void operator()(T &p) const
{
p.reset(new typename GetElementType<T>::type());
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T>
class New<Boost::shared_ptr<T> >
{
public:
void operator()(Boost::shared_ptr<T> &p) const
{
p = Boost::make_shared<T>();
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T>
class New<Boost::movelib::unique_ptr<T> >
{
public:
void operator()(Boost::movelib::unique_ptr<T> &p) const
{
p = Boost::movelib::make_unique<T>();
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
#if (!defined ACTIV_BOOST_NO_CXX11_SMART_PTR)
template<typename T>
class New<std::shared_ptr<T>>
{
public:
void operator()(std::shared_ptr<T> &p) const
{
p = std::make_shared<T>();
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T>
class New<std::unique_ptr<T>>
{
public:
void operator()(std::unique_ptr<T> &p) const
{
p = CxxYZ::make_unique<T>();
}
};
#endif // (!defined ACTIV_BOOST_NO_CXX11_SMART_PTR)
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T>
class New<Boost::optional<T> >
{
public:
void operator()(Boost::optional<T> &p) const
{
if (!p)
p = T();
}
};
} // namespace SmartPointerHelpers
} // namespace GenericMessages
} // namespace Activ
#endif // (!defined ACTIV_BASE_GENERIC_MESSAGES_FORMATS_COMMON_SMART_POINTER_HELPERS_H)