Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:27:52

0001 //------------------------------------------------------------------------------
0002 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
0003 // Author: Lukasz Janyst <ljanyst@cern.ch>
0004 //------------------------------------------------------------------------------
0005 // XRootD is free software: you can redistribute it and/or modify
0006 // it under the terms of the GNU Lesser General Public License as published by
0007 // the Free Software Foundation, either version 3 of the License, or
0008 // (at your option) any later version.
0009 //
0010 // XRootD is distributed in the hope that it will be useful,
0011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 // GNU General Public License for more details.
0014 //
0015 // You should have received a copy of the GNU Lesser General Public License
0016 // along with XRootD.  If not, see <http://www.gnu.org/licenses/>.
0017 //------------------------------------------------------------------------------
0018 
0019 #ifndef __XRD_CL_ANY_OBJECT_HH__
0020 #define __XRD_CL_ANY_OBJECT_HH__
0021 
0022 #include <typeinfo>
0023 #include <cstring>
0024 
0025 namespace XrdCl
0026 {
0027   //----------------------------------------------------------------------------
0028   //! Simple implementation of a type safe holder for any object pointer
0029   //! It would have been a better idea to use boost::any here but we don't
0030   //! want to depend on boost
0031   //----------------------------------------------------------------------------
0032   class AnyObject
0033   {
0034     public:
0035       //------------------------------------------------------------------------
0036       //! Constructor
0037       //------------------------------------------------------------------------
0038       AnyObject(): pHolder(0), pTypeInfo(0), pOwn( true ) {};
0039 
0040       //------------------------------------------------------------------------
0041       //! Destructor
0042       //------------------------------------------------------------------------
0043       ~AnyObject()
0044       {
0045         if( pHolder && pOwn )
0046           pHolder->Delete();
0047         delete pHolder;
0048       }
0049 
0050       //------------------------------------------------------------------------
0051       //! Grab an object
0052       //! By default the ownership of the object is taken as well, ie.
0053       //! the object will be deleted when the AnyObject holding it is deleted.
0054       //! To release an object grab a zero pointer, ie. (int *)0
0055       //!
0056       //! @param object object pointer
0057       //! @param own    take the ownership or not
0058       //------------------------------------------------------------------------
0059       template <class Type> void Set( Type object, bool own = true )
0060       {
0061         if( !object )
0062         {
0063           delete pHolder;
0064           pHolder   = 0;
0065           pTypeInfo = 0;
0066           return;
0067         }
0068 
0069         delete pHolder;
0070         pHolder   = new ConcreteHolder<Type>( object );
0071         pOwn      = own;
0072         pTypeInfo = &typeid( Type );
0073       }
0074 
0075       //------------------------------------------------------------------------
0076       //! Retrieve the object being held
0077       //------------------------------------------------------------------------
0078       template <class Type> void Get( Type &object )
0079       {
0080         if( !pHolder || (strcmp( pTypeInfo->name(), typeid( Type ).name() )) )
0081         {
0082           object = 0;
0083           return;
0084         }
0085         object = static_cast<Type>( pHolder->Get() );
0086       }
0087 
0088       //------------------------------------------------------------------------
0089       //! @return true is AnyObject holds an instance of given type
0090       //------------------------------------------------------------------------
0091       template <class Type>
0092       inline bool Has()
0093       {
0094         if( !pHolder ) return false;
0095         return strcmp( pTypeInfo->name(), typeid( Type* ).name() ) == 0;
0096       }
0097 
0098       //------------------------------------------------------------------------
0099       //! Check if we own the object being stored
0100       //------------------------------------------------------------------------
0101       bool HasOwnership() const
0102       {
0103         return pOwn;
0104       }
0105 
0106     private:
0107       //------------------------------------------------------------------------
0108       // Abstract holder object
0109       //------------------------------------------------------------------------
0110       class Holder
0111       {
0112         public:
0113           virtual ~Holder() {}
0114           virtual void Delete() = 0;
0115           virtual void *Get()   = 0;
0116       };
0117 
0118       //------------------------------------------------------------------------
0119       // Concrete holder
0120       //------------------------------------------------------------------------
0121       template<class Type>
0122       class ConcreteHolder: public Holder
0123       {
0124         public:
0125           ConcreteHolder( Type object  ): pObject( object ) {}
0126           virtual void Delete()
0127           {
0128             delete pObject;
0129           }
0130 
0131           virtual void *Get()
0132           {
0133             return (void *)pObject;
0134           }
0135 
0136         private:
0137           Type pObject;
0138       };
0139 
0140       Holder               *pHolder;
0141       const std::type_info *pTypeInfo;
0142       bool                  pOwn;
0143   };
0144 
0145   //----------------------------------------------------------------------------
0146   //! Helper function for extracting an object from AnyObject
0147   //! @param any : an instance of AnyObject
0148   //! @return    : the underlying value of type T
0149   //----------------------------------------------------------------------------
0150   template<typename T>
0151   inline T& To( AnyObject &any )
0152   {
0153     T* object;
0154     any.Get( object );
0155     return *object;
0156   }
0157 }
0158 
0159 #endif // __XRD_CL_ANY_OBJECT_HH__