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  bool HasOwnership() const
92  {
93  return pOwn;
94  }
95 
96  private:
97  //------------------------------------------------------------------------
98  // Abstract holder object
99  //------------------------------------------------------------------------
100  class Holder
101  {
102  public:
103  virtual ~Holder() {}
104  virtual void Delete() = 0;
105  virtual void *Get() = 0;
106  };
107 
108  //------------------------------------------------------------------------
109  // Concrete holder
110  //------------------------------------------------------------------------
111  template<class Type>
112  class ConcreteHolder: public Holder
113  {
114  public:
115  ConcreteHolder( Type object ): pObject( object ) {}
116  virtual void Delete()
117  {
118  delete pObject;
119  }
120 
121  virtual void *Get()
122  {
123  return (void *)pObject;
124  }
125 
126  private:
127  Type pObject;
128  };
129 
131  const std::type_info *pTypeInfo;
132  bool pOwn;
133  };
134 }
135 
136 #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
Type pObject
Definition: XrdClAnyObject.hh:127
virtual void Delete()
Definition: XrdClAnyObject.hh:116
bool pOwn
Definition: XrdClAnyObject.hh:132
~AnyObject()
Destructor.
Definition: XrdClAnyObject.hh:43
Holder * pHolder
Definition: XrdClAnyObject.hh:130
const std::type_info * pTypeInfo
Definition: XrdClAnyObject.hh:131
void Set(Type object, bool own=true)
Definition: XrdClAnyObject.hh:59
virtual ~Holder()
Definition: XrdClAnyObject.hh:103
ConcreteHolder(Type object)
Definition: XrdClAnyObject.hh:115
virtual void * Get()
Definition: XrdClAnyObject.hh:121
bool HasOwnership() const
Check if we own the object being stored.
Definition: XrdClAnyObject.hh:91
virtual void Delete()=0
AnyObject()
Constructor.
Definition: XrdClAnyObject.hh:38
Definition: XrdClAnyObject.hh:100
Definition: XrdClAnyObject.hh:112