diff options
author | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:10:11 +0200 |
---|---|---|
committer | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:10:11 +0200 |
commit | c0cdb2ad99a1e2a6ade5ce76c91177a79258e669 (patch) | |
tree | 4701a015385d9a6a5a4ba99a8f1f5d400fff26b1 /shared/loki/SPCachedFactory.h | |
parent | 3.13 (diff) | |
download | FreeFileSync-c0cdb2ad99a1e2a6ade5ce76c91177a79258e669.tar.gz FreeFileSync-c0cdb2ad99a1e2a6ade5ce76c91177a79258e669.tar.bz2 FreeFileSync-c0cdb2ad99a1e2a6ade5ce76c91177a79258e669.zip |
3.14
Diffstat (limited to 'shared/loki/SPCachedFactory.h')
-rw-r--r-- | shared/loki/SPCachedFactory.h | 292 |
1 files changed, 146 insertions, 146 deletions
diff --git a/shared/loki/SPCachedFactory.h b/shared/loki/SPCachedFactory.h index 71c72644..aab37127 100644 --- a/shared/loki/SPCachedFactory.h +++ b/shared/loki/SPCachedFactory.h @@ -4,16 +4,16 @@ // // Code covered by the MIT License // -// Permission to use, copy, modify, distribute and sell this software for any -// purpose is hereby granted without fee, provided that the above copyright -// notice appear in all copies and that both that copyright notice and this +// Permission to use, copy, modify, distribute and sell this software for any +// purpose is hereby granted without fee, provided that the above copyright +// notice appear in all copies and that both that copyright notice and this // permission notice appear in supporting documentation. // // The authors make no representations about the suitability of this software // for any purpose. It is provided "as is" without express or implied warranty. // // This code DOES NOT accompany the book: -// Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design +// Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design // Patterns Applied". Copyright (c) 2001. Addison-Wesley. // //////////////////////////////////////////////////////////////////////////////// @@ -29,8 +29,8 @@ * It as been defined in a separate file because of the many introduced * dependencies (SmartPtr.h would depend on Functor.h and CachedFactory.h * would depend on SmartPtr.h). By defining another header you pay for those - * extra dependencies only if you need it. - * + * extra dependencies only if you need it. + * * This file defines FunctionStorage a new SmartPointer storage policy and * SmartPointer a new CachedFactory encapsulation policy. */ @@ -45,7 +45,7 @@ namespace Loki //////////////////////////////////////////////////////////////////////////////// /// \class FunctionStorage /// -/// \ingroup SmartPointerStorageGroup +/// \ingroup SmartPointerStorageGroup /// \brief Implementation of the StoragePolicy used by SmartPtr. /// /// This storage policy is used by SmartPointer CachedFactory's encapsulation @@ -58,146 +58,146 @@ namespace Loki /// the FunctionStorage template to know the full definition of the SmartPtr. //////////////////////////////////////////////////////////////////////////////// - template <class T> - class FunctionStorage +template <class T> +class FunctionStorage +{ +public: + /// the type of the pointee_ object + typedef T* StoredType; + /// type used to declare OwnershipPolicy type. + typedef T* InitPointerType; + /// type returned by operator-> + typedef T* PointerType; + /// type returned by operator* + typedef T& ReferenceType; + /// type of the Functor to set + typedef Functor< void , Seq< void* > > FunctorType; + + FunctionStorage() : pointee_(Default()), functor_() + {} + + // The storage policy doesn't initialize the stored pointer + // which will be initialized by the OwnershipPolicy's Clone fn + FunctionStorage(const FunctionStorage& rsh) : pointee_(0), functor_(rsh.functor_) + {} + + template <class U> + FunctionStorage(const FunctionStorage<U>& rsh) : pointee_(0), functor_(rsh.functor_) + {} + + FunctionStorage(const StoredType& p) : pointee_(p), functor_() {} + + PointerType operator->() const { return pointee_; } + + ReferenceType operator*() const { return *pointee_; } + + void Swap(FunctionStorage& rhs) + { + std::swap(pointee_, rhs.pointee_); + std::swap(functor_, rhs.functor_); + } + + /// Sets the callback function to call. You have to specify it or + /// the smartPtr will throw a bad_function_call exception. + void SetCallBackFunction(const FunctorType& functor) + { + functor_ = functor; + } + + // Accessors + template <class F> + friend typename FunctionStorage<F>::PointerType GetImpl(const FunctionStorage<F>& sp); + + template <class F> + friend const typename FunctionStorage<F>::StoredType& GetImplRef(const FunctionStorage<F>& sp); + + template <class F> + friend typename FunctionStorage<F>::StoredType& GetImplRef(FunctionStorage<F>& sp); + +protected: + // Destroys the data stored + // (Destruction might be taken over by the OwnershipPolicy) + void Destroy() + { + functor_(this); + } + + // Default value to initialize the pointer + static StoredType Default() + { return 0; } + +private: + // Data + StoredType pointee_; + FunctorType functor_; +}; + +template <class T> +inline typename FunctionStorage<T>::PointerType GetImpl(const FunctionStorage<T>& sp) +{ return sp.pointee_; } + +template <class T> +inline const typename FunctionStorage<T>::StoredType& GetImplRef(const FunctionStorage<T>& sp) +{ return sp.pointee_; } + +template <class T> +inline typename FunctionStorage<T>::StoredType& GetImplRef(FunctionStorage<T>& sp) +{ return sp.pointee_; } + +/** + * \class SmartPointer + * \ingroup EncapsulationPolicyCachedFactoryGroup + * \brief Encapsulate the object in a SmartPtr with FunctionStorage policy. + * + * The object will come back to the Cache as soon as no more SmartPtr are + * referencing this object. You can customize the SmartPointer with the standard + * SmartPtr policies (OwnershipPolicy, ConversionPolicy, CheckingPolicy, + * ConstnessPolicy) but StoragePolicy is forced to FunctionStorage. + */ +template +< +class AbstractProduct, + template <class> class OwnershipPolicy = RefCounted, + class ConversionPolicy = DisallowConversion, + template <class> class CheckingPolicy = AssertCheck, + template<class> class ConstnessPolicy = LOKI_DEFAULT_CONSTNESS + > +class SmartPointer +{ +private: + typedef SmartPtr< AbstractProduct,OwnershipPolicy, + ConversionPolicy, CheckingPolicy, + FunctionStorage, ConstnessPolicy > CallBackSP; +protected: + typedef CallBackSP ProductReturn; + SmartPointer() : fun(this, &SmartPointer::smartPointerCallbackFunction) {} + virtual ~SmartPointer() {} + + ProductReturn encapsulate(AbstractProduct* pProduct) + { + CallBackSP SP(pProduct); + SP.SetCallBackFunction(fun); + return SP; + } + + AbstractProduct* release(ProductReturn& pProduct) + { + return GetImpl(pProduct); + } + + const char* name() {return "smart pointer";} + +private: + SmartPointer& operator=(const SmartPointer&); + SmartPointer(const SmartPointer&); + void smartPointerCallbackFunction(void* pSP) { - public: - /// the type of the pointee_ object - typedef T* StoredType; - /// type used to declare OwnershipPolicy type. - typedef T* InitPointerType; - /// type returned by operator-> - typedef T* PointerType; - /// type returned by operator* - typedef T& ReferenceType; - /// type of the Functor to set - typedef Functor< void , Seq< void* > > FunctorType; - - FunctionStorage() : pointee_(Default()), functor_() - {} - - // The storage policy doesn't initialize the stored pointer - // which will be initialized by the OwnershipPolicy's Clone fn - FunctionStorage(const FunctionStorage& rsh) : pointee_(0), functor_(rsh.functor_) - {} - - template <class U> - FunctionStorage(const FunctionStorage<U>& rsh) : pointee_(0), functor_(rsh.functor_) - {} - - FunctionStorage(const StoredType& p) : pointee_(p), functor_() {} - - PointerType operator->() const { return pointee_; } - - ReferenceType operator*() const { return *pointee_; } - - void Swap(FunctionStorage& rhs) - { - std::swap(pointee_, rhs.pointee_); - std::swap(functor_, rhs.functor_); - } - - /// Sets the callback function to call. You have to specify it or - /// the smartPtr will throw a bad_function_call exception. - void SetCallBackFunction(const FunctorType &functor) - { - functor_ = functor; - } - - // Accessors - template <class F> - friend typename FunctionStorage<F>::PointerType GetImpl(const FunctionStorage<F>& sp); - - template <class F> - friend const typename FunctionStorage<F>::StoredType& GetImplRef(const FunctionStorage<F>& sp); - - template <class F> - friend typename FunctionStorage<F>::StoredType& GetImplRef(FunctionStorage<F>& sp); - - protected: - // Destroys the data stored - // (Destruction might be taken over by the OwnershipPolicy) - void Destroy() - { - functor_(this); - } - - // Default value to initialize the pointer - static StoredType Default() - { return 0; } - - private: - // Data - StoredType pointee_; - FunctorType functor_; - }; - - template <class T> - inline typename FunctionStorage<T>::PointerType GetImpl(const FunctionStorage<T>& sp) - { return sp.pointee_; } - - template <class T> - inline const typename FunctionStorage<T>::StoredType& GetImplRef(const FunctionStorage<T>& sp) - { return sp.pointee_; } - - template <class T> - inline typename FunctionStorage<T>::StoredType& GetImplRef(FunctionStorage<T>& sp) - { return sp.pointee_; } - - /** - * \class SmartPointer - * \ingroup EncapsulationPolicyCachedFactoryGroup - * \brief Encapsulate the object in a SmartPtr with FunctionStorage policy. - * - * The object will come back to the Cache as soon as no more SmartPtr are - * referencing this object. You can customize the SmartPointer with the standard - * SmartPtr policies (OwnershipPolicy, ConversionPolicy, CheckingPolicy, - * ConstnessPolicy) but StoragePolicy is forced to FunctionStorage. - */ - template - < - class AbstractProduct, - template <class> class OwnershipPolicy = RefCounted, - class ConversionPolicy = DisallowConversion, - template <class> class CheckingPolicy = AssertCheck, - template<class> class ConstnessPolicy = LOKI_DEFAULT_CONSTNESS - > - class SmartPointer - { - private: - typedef SmartPtr< AbstractProduct,OwnershipPolicy, - ConversionPolicy, CheckingPolicy, - FunctionStorage, ConstnessPolicy > CallBackSP; - protected: - typedef CallBackSP ProductReturn; - SmartPointer() : fun(this, &SmartPointer::smartPointerCallbackFunction) {} - virtual ~SmartPointer(){} - - ProductReturn encapsulate(AbstractProduct* pProduct) - { - CallBackSP SP(pProduct); - SP.SetCallBackFunction(fun); - return SP; - } - - AbstractProduct* release(ProductReturn &pProduct) - { - return GetImpl(pProduct); - } - - const char* name(){return "smart pointer";} - - private: - SmartPointer& operator=(const SmartPointer&); - SmartPointer(const SmartPointer&); - void smartPointerCallbackFunction(void* pSP) - { - CallBackSP &SP(*reinterpret_cast<CallBackSP*>(pSP)); - ReleaseObject(SP); - } - virtual void ReleaseObject(ProductReturn &object)=0; - const typename CallBackSP::FunctorType fun; - }; + CallBackSP& SP(*reinterpret_cast<CallBackSP*>(pSP)); + ReleaseObject(SP); + } + virtual void ReleaseObject(ProductReturn& object)=0; + const typename CallBackSP::FunctorType fun; +}; } // namespace Loki |