xrootd
XrdClAnyObject.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
3 // Author: Lukasz Janyst <ljanyst@cern.ch>
4 //------------------------------------------------------------------------------
5 // XRootD is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // XRootD is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
17 //------------------------------------------------------------------------------
18 
19 #ifndef __XRD_CL_ANY_OBJECT_HH__
20 #define __XRD_CL_ANY_OBJECT_HH__
21 
22 #include <typeinfo>
23 #include <cstring>
24 
25 namespace XrdCl
26 {
27  //----------------------------------------------------------------------------
31  //----------------------------------------------------------------------------
32  class AnyObject
33  {
34  public:
35  //------------------------------------------------------------------------
37  //------------------------------------------------------------------------
38  AnyObject(): pHolder(0), pTypeInfo(0), pOwn( true ) {};
39 
40  //------------------------------------------------------------------------
42  //------------------------------------------------------------------------
44  {
45  if( pHolder && pOwn )
46  pHolder->Delete();
47  delete pHolder;
48  }
49 
50  //------------------------------------------------------------------------
58  //------------------------------------------------------------------------
59  template <class Type> void Set( Type object, bool own = true )
60  {
61  if( !object )
62  {
63  delete pHolder;
64  pHolder = 0;
65  pTypeInfo = 0;
66  return;
67  }
68 
69  delete pHolder;
70  pHolder = new ConcreteHolder<Type>( object );
71  pOwn = own;
72  pTypeInfo = &typeid( Type );
73  }
74 
75  //------------------------------------------------------------------------
77  //------------------------------------------------------------------------
78  template <class Type> void Get( Type &object )
79  {
80  if( !pHolder || (strcmp( pTypeInfo->name(), typeid( Type ).name() )) )
81  {
82  object = 0;
83  return;
84  }
85  object = static_cast<Type>( pHolder->Get() );
86  }
87 
88  //------------------------------------------------------------------------
90  //------------------------------------------------------------------------
91  template <class Type>
92  inline bool Has()
93  {
94  if( !pHolder ) return false;
95  return strcmp( pTypeInfo->name(), typeid( Type* ).name() ) == 0;
96  }
97 
98  //------------------------------------------------------------------------
100  //------------------------------------------------------------------------
101  bool HasOwnership() const
102  {
103  return pOwn;
104  }
105 
106  private:
107  //------------------------------------------------------------------------
108  // Abstract holder object
109  //------------------------------------------------------------------------
110  class Holder
111  {
112  public:
113  virtual ~Holder() {}
114  virtual void Delete() = 0;
115  virtual void *Get() = 0;
116  };
117 
118  //------------------------------------------------------------------------
119  // Concrete holder
120  //------------------------------------------------------------------------
121  template<class Type>
122  class ConcreteHolder: public Holder
123  {
124  public:
125  ConcreteHolder( Type object ): pObject( object ) {}
126  virtual void Delete()
127  {
128  delete pObject;
129  }
130 
131  virtual void *Get()
132  {
133  return (void *)pObject;
134  }
135 
136  private:
137  Type pObject;
138  };
139 
141  const std::type_info *pTypeInfo;
142  bool pOwn;
143  };
144 
145  //----------------------------------------------------------------------------
149  //----------------------------------------------------------------------------
150  template<typename T>
151  inline T& To( AnyObject &any )
152  {
153  T* object;
154  any.Get( object );
155  return *object;
156  }
157 }
158 
159 #endif // __XRD_CL_ANY_OBJECT_HH__
Definition: XrdClAnyObject.hh:32
virtual void * Get()=0
void Get(Type &object)
Retrieve the object being held.
Definition: XrdClAnyObject.hh:78
bool Has()
Definition: XrdClAnyObject.hh:92
Type pObject
Definition: XrdClAnyObject.hh:137
virtual void Delete()
Definition: XrdClAnyObject.hh:126
bool pOwn
Definition: XrdClAnyObject.hh:142
~AnyObject()
Destructor.
Definition: XrdClAnyObject.hh:43
Holder * pHolder
Definition: XrdClAnyObject.hh:140
const std::type_info * pTypeInfo
Definition: XrdClAnyObject.hh:141
void Set(Type object, bool own=true)
Definition: XrdClAnyObject.hh:59
Definition: XrdClAnyObject.hh:25
bool HasOwnership() const
Check if we own the object being stored.
Definition: XrdClAnyObject.hh:101
T & To(AnyObject &any)
Definition: XrdClAnyObject.hh:151
virtual ~Holder()
Definition: XrdClAnyObject.hh:113
ConcreteHolder(Type object)
Definition: XrdClAnyObject.hh:125
virtual void * Get()
Definition: XrdClAnyObject.hh:131
virtual void Delete()=0
AnyObject()
Constructor.
Definition: XrdClAnyObject.hh:38
Definition: XrdClAnyObject.hh:110
Definition: XrdClAnyObject.hh:122