26 _object = std::shared_ptr<T>( obj ) ;
29 _object = std::shared_ptr<T>( obj, [](T *){ } ) ;
35 return static_cast<const T*
>(
_object.get()) ;
40 return static_cast<T*
>(
_object.get()) ;
44 inline std::shared_ptr<const T>
shared()
const {
45 return std::static_pointer_cast<
const T>(
_object) ;
50 return std::static_pointer_cast<T>(
_object);
57 inline const std::type_index &
type()
const {
73 using ExtensionMap = std::map<std::size_t, std::shared_ptr<Extension>> ;
85 return ( _extensions.find( std::type_index(
typeid(K)).hash_code() ) != _extensions.end() ) ;
88 template <
typename K,
typename T>
90 std::type_index typeidx(
typeid(K) ) ;
91 auto iter = _extensions.find( typeidx.hash_code() ) ;
92 if( iter != _extensions.end() ) {
93 MARLINMT_THROW(
"Extension of type " + std::string(typeidx.name()) +
" already present" ) ;
95 auto ext = std::make_shared<Extension>( ptr,
isOwned ) ;
96 _extensions.insert( { typeidx.hash_code(), ext } ) ;
99 template <
typename K,
typename T,
typename ...Args>
101 std::type_index typeidx(
typeid(K) ) ;
102 auto iter = _extensions.find( typeidx.hash_code() ) ;
103 if( iter != _extensions.end() ) {
104 MARLINMT_THROW(
"Extension of type " + std::string(typeidx.name()) +
" already present" ) ;
106 auto ext = std::make_shared<Extension>(
new T( args... ),
isOwned ) ;
107 _extensions.insert( { typeidx.hash_code(), ext } ) ;
108 return ext->template object<T>() ;
111 template <
typename K,
typename T>
113 std::type_index typeidx(
typeid(K) ) ;
114 auto iter = _extensions.find( typeidx.hash_code() ) ;
115 if( iter == _extensions.end() ) {
116 MARLINMT_THROW(
"Extension of type " + std::string(typeidx.name()) +
" doesn't exists" ) ;
118 return iter->second->object<T>() ;
121 template <
typename K,
typename T>
122 inline const T *
get()
const {
123 std::type_index typeidx(
typeid(K) ) ;
124 auto iter = _extensions.find( typeidx.hash_code() ) ;
125 if( iter == _extensions.end() ) {
126 MARLINMT_THROW(
"Extension of type " + std::string(typeidx.name()) +
" doesn't exists" ) ;
128 return iter->second->object<T>() ;
131 template <
typename K>
132 inline void remove() {
133 std::type_index typeidx(
typeid(K) ) ;
134 auto iter = _extensions.find( typeidx.hash_code() ) ;
135 if( iter == _extensions.end() ) {
136 MARLINMT_THROW(
"Extension of type " + std::string(typeidx.name()) +
" doesn't exists" ) ;
138 _extensions.erase( iter ) ;
Extension & operator=(const Extension &)=delete
void add(T *ptr, bool isOwned=true)
std::shared_ptr< void > _object
std::shared_ptr< T > shared()
const std::type_index & type() const
#define MARLINMT_THROW(message)
T * create(bool isOwned, Args ...args)
std::shared_ptr< const T > shared() const
Extension(T *obj, bool isOwned)
std::map< std::size_t, std::shared_ptr< Extension > > ExtensionMap