From 7762581d5dfaa77c81bfb909853729cb049005fe Mon Sep 17 00:00:00 2001 From: jpbl <jpbl> Date: Tue, 11 Oct 2005 21:23:36 +0000 Subject: [PATCH] adding missing files --- .../utilspp/singleton/CreationStatic.hpp | 49 +++++++ .../utilspp/singleton/CreationStatic.inl | 43 ++++++ .../utilspp/singleton/CreationUsingNew.hpp | 43 ++++++ .../utilspp/singleton/CreationUsingNew.inl | 42 ++++++ .../utilspp/singleton/LifetimeDefault.hpp | 43 ++++++ .../utilspp/singleton/LifetimeDefault.inl | 42 ++++++ .../utilspp/singleton/LifetimeLibrary.hpp | 104 +++++++++++++++ .../utilspp/singleton/LifetimeLibrary.inl | 33 +++++ .../singleton/LifetimeWithLongevity.hpp | 56 ++++++++ .../singleton/LifetimeWithLongevity.inl | 56 ++++++++ .../utilspp/singleton/PrivateMembers.hpp | 93 +++++++++++++ .../utilspp/singleton/PrivateMembers.inl | 30 +++++ .../utilspp/singleton/SingletonHolder.hpp | 66 +++++++++ .../utilspp/singleton/SingletonHolder.inl | 126 ++++++++++++++++++ 14 files changed, 826 insertions(+) create mode 100644 src/gui/official/utilspp/singleton/CreationStatic.hpp create mode 100644 src/gui/official/utilspp/singleton/CreationStatic.inl create mode 100644 src/gui/official/utilspp/singleton/CreationUsingNew.hpp create mode 100644 src/gui/official/utilspp/singleton/CreationUsingNew.inl create mode 100644 src/gui/official/utilspp/singleton/LifetimeDefault.hpp create mode 100644 src/gui/official/utilspp/singleton/LifetimeDefault.inl create mode 100644 src/gui/official/utilspp/singleton/LifetimeLibrary.hpp create mode 100644 src/gui/official/utilspp/singleton/LifetimeLibrary.inl create mode 100644 src/gui/official/utilspp/singleton/LifetimeWithLongevity.hpp create mode 100644 src/gui/official/utilspp/singleton/LifetimeWithLongevity.inl create mode 100644 src/gui/official/utilspp/singleton/PrivateMembers.hpp create mode 100644 src/gui/official/utilspp/singleton/PrivateMembers.inl create mode 100644 src/gui/official/utilspp/singleton/SingletonHolder.hpp create mode 100644 src/gui/official/utilspp/singleton/SingletonHolder.inl diff --git a/src/gui/official/utilspp/singleton/CreationStatic.hpp b/src/gui/official/utilspp/singleton/CreationStatic.hpp new file mode 100644 index 0000000000..27570f5308 --- /dev/null +++ b/src/gui/official/utilspp/singleton/CreationStatic.hpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) <2002-2004> <Jean-Philippe Barrette-LaPierre> + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (cURLpp), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef CREATION_STATIC_HPP +#define CREATION_STATIC_HPP + +/** + * This class is a creation policy for the utilspp::singleton_holder. The + * policy is creating the singleton by a static memory. The constructor is + * called the first time we call the utilspp::creation_static::create() + * function. + * + * Note don't use this class because it's not complete, and at this time it's + * not REALY complyant with the lifetime policy. + */ +namespace utilspp +{ + template< typename T > + class CreationStatic + { + public: + static T* create(); + static void destroy( T* obj ); + }; +}; + +#include "CreationStatic.inl" + +#endif diff --git a/src/gui/official/utilspp/singleton/CreationStatic.inl b/src/gui/official/utilspp/singleton/CreationStatic.inl new file mode 100644 index 0000000000..9572e5eca7 --- /dev/null +++ b/src/gui/official/utilspp/singleton/CreationStatic.inl @@ -0,0 +1,43 @@ +/* + * Copyright (c) <2002-2004> <Jean-Philippe Barrette-LaPierre> + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (cURLpp), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef CREATION_STATIC_INL +#define CREATION_STATIC_INL + +template< typename T > +T* +utilspp::CreationStatic::create() +{ + static T mObj; + return new(&mObj) T; +}; + +template< typename T > +void +utilspp::CreationStatic::destroy( T* obj ) +{ + obj->~T(); +} + + +#endif \ No newline at end of file diff --git a/src/gui/official/utilspp/singleton/CreationUsingNew.hpp b/src/gui/official/utilspp/singleton/CreationUsingNew.hpp new file mode 100644 index 0000000000..5055db5b8f --- /dev/null +++ b/src/gui/official/utilspp/singleton/CreationUsingNew.hpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) <2002-2004> <Jean-Philippe Barrette-LaPierre> + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (cURLpp), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef CREATION_USING_NEW_HPP +#define CREATION_USING_NEW_HPP + +/** + * This class is a creation policy for the utilspp::singleton_holder. The + * policy is creating the singleton by a "new" call. + */ +namespace utilspp +{ + template< typename T > + struct CreationUsingNew + { + static T *create(); + static void destroy( T *obj ); + }; +}; + +#include "CreationUsingNew.inl" + +#endif diff --git a/src/gui/official/utilspp/singleton/CreationUsingNew.inl b/src/gui/official/utilspp/singleton/CreationUsingNew.inl new file mode 100644 index 0000000000..adb66ede9e --- /dev/null +++ b/src/gui/official/utilspp/singleton/CreationUsingNew.inl @@ -0,0 +1,42 @@ +/* + * Copyright (c) <2002-2004> <Jean-Philippe Barrette-LaPierre> + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (cURLpp), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef CREATION_USING_NEW_INL +#define CREATION_USING_NEW_INL + +template< typename T > +T * +utilspp::CreationUsingNew< T >::create() +{ + return new T; +} + +template< typename T > +void +utilspp::CreationUsingNew< T >::destroy( T *obj ) +{ + delete obj; +} + + +#endif diff --git a/src/gui/official/utilspp/singleton/LifetimeDefault.hpp b/src/gui/official/utilspp/singleton/LifetimeDefault.hpp new file mode 100644 index 0000000000..9c77138109 --- /dev/null +++ b/src/gui/official/utilspp/singleton/LifetimeDefault.hpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) <2002-2004> <Jean-Philippe Barrette-LaPierre> + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (cURLpp), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef LIFETIME_DEFAULT_HPP +#define LIFETIME_DEFAULT_HPP + +#include <stdexcept> +#include <cstdlib> + +namespace utilspp +{ + template< typename T > + class LifetimeDefault + { + public: + static void scheduleDestruction( T *obj, void (*func)() ); + static void onDeadReference(); + }; +}; + +#include "LifetimeDefault.inl" + +#endif diff --git a/src/gui/official/utilspp/singleton/LifetimeDefault.inl b/src/gui/official/utilspp/singleton/LifetimeDefault.inl new file mode 100644 index 0000000000..a8e63f9fc4 --- /dev/null +++ b/src/gui/official/utilspp/singleton/LifetimeDefault.inl @@ -0,0 +1,42 @@ +/* + * Copyright (c) <2002-2004> <Jean-Philippe Barrette-LaPierre> + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (cURLpp), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef LIFETIME_DEFAULT_INL +#define LIFETIME_DEFAULT_INL + +template< typename T > +void +utilspp::LifetimeDefault< T >::scheduleDestruction( T *, void (*func)() ) +{ + std::atexit(func); +} + +template< typename T > +void +utilspp::LifetimeDefault< T >::onDeadReference() +{ + throw std::logic_error( "Dead reference detected" ); +} + + +#endif diff --git a/src/gui/official/utilspp/singleton/LifetimeLibrary.hpp b/src/gui/official/utilspp/singleton/LifetimeLibrary.hpp new file mode 100644 index 0000000000..80fba716ab --- /dev/null +++ b/src/gui/official/utilspp/singleton/LifetimeLibrary.hpp @@ -0,0 +1,104 @@ +/* + * Copyright (c) <2002-2004> <Jean-Philippe Barrette-LaPierre> + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (cURLpp), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef LIFETIME_LIBRARY_HPP +#define LIFETIME_LIBRARY_HPP + +#include <cassert> + +#include "PrivateMembers.hpp" +#include "CreationUsingNew.hpp" + +namespace utilspp +{ + + template< typename T > + unsigned int getLongevity( T *p ); + + /** + * Assigns an object a longevity. Ensures ordered destructions of objects + * registered thusly during the exit sequence of the application. + */ + template< typename T, typename TDestroyer > + void setLibraryLongevity( + T *obj, + unsigned int longevity, + TDestroyer d = utilspp::PrivateMembers::Deleter< T >::deleteObject + ); + + /** + * This class is a lifetime policy for the singleton. This + * class allow you to terminate the singleton explicitly. + * You can terminate by calling: + * + * LifetimeLibrarySingleton::instance().terminate() + * + * This singleton use the utilspp::LifetimeWithLongevity policy. + */ + template< typename T > + struct LifetimeLibrary + { + static void scheduleDestruction( T *obj, void (*func)() ); + static void onDeadReference(); + }; + + class LifetimeLibraryImpl + { + public: + LifetimeLibraryImpl(); + ~LifetimeLibraryImpl(); + + void add( utilspp::PrivateMembers::LifetimeTracker *tracker ); + void terminate(); + + private: + utilspp::PrivateMembers::TrackerArray mTrackerArray; + int mNbElements; + }; + + unsigned int getLongevity( utilspp::LifetimeLibraryImpl *p ); + + typedef utilspp::SingletonHolder< + utilspp::LifetimeLibraryImpl, + utilspp::CreationUsingNew, + utilspp::LifetimeWithLongevity + > LifetimeLibrarySingleton; + + /** + * This class will ensure that + * + * LifetimeLibraryImpl::terminate() + * + * is called. + */ + template< typename T = utilspp::LifetimeLibrarySingleton > + class LifetimeLibraryGuard + { + public: + ~LifetimeLibraryGuard(); + }; +}; + +#include "LifetimeLibrary.inl" + +#endif diff --git a/src/gui/official/utilspp/singleton/LifetimeLibrary.inl b/src/gui/official/utilspp/singleton/LifetimeLibrary.inl new file mode 100644 index 0000000000..b7ab8e6d66 --- /dev/null +++ b/src/gui/official/utilspp/singleton/LifetimeLibrary.inl @@ -0,0 +1,33 @@ +template< typename T, typename TDestroyer > +void +utilspp::setLibraryLongevity( T *obj, unsigned int longevity, TDestroyer d ) +{ + using namespace utilspp::PrivateMembers; + + LifetimeTracker *p = new ConcreteLifetimeTracker< T, TDestroyer >( + obj, longevity, d); + + utilspp::LifetimeLibrarySingleton::instance().add( p ); +}; + +template< typename T > +void +utilspp::LifetimeLibrary< T >::scheduleDestruction( T *obj, void (*func)() ) +{ + utilspp::PrivateMembers::adapter<T> adapter = { func }; + utilspp::setLibraryLongevity( obj, getLongevity( obj ), adapter ); +} + +template< typename T > +void +utilspp::LifetimeLibrary< T >::onDeadReference() +{ + throw std::logic_error("Dead reference detected"); +} + +template< typename T > +utilspp::LifetimeLibraryGuard< T >::~LifetimeLibraryGuard() +{ + T::instance().terminate(); +} + diff --git a/src/gui/official/utilspp/singleton/LifetimeWithLongevity.hpp b/src/gui/official/utilspp/singleton/LifetimeWithLongevity.hpp new file mode 100644 index 0000000000..8884751264 --- /dev/null +++ b/src/gui/official/utilspp/singleton/LifetimeWithLongevity.hpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) <2002-2004> <Jean-Philippe Barrette-LaPierre> + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (cURLpp), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef LIFETIME_WITH_LONGEVITY_HPP +#define LIFETIME_WITH_LONGEVITY_HPP + +#include <cassert> + +#include "PrivateMembers.hpp" + +namespace utilspp +{ + + template< typename T > + unsigned int getLongevity( T *p ); + + /** + * Assigns an object a longevity. Ensures ordered destructions of objects + * registered thusly during the exit sequence of the application. + */ + template< typename T, typename TDestroyer > + void setLongevity(T *obj, + unsigned int longevity, + TDestroyer d = utilspp::PrivateMembers::Deleter< T >::deleteObject); + + template< typename T > + struct LifetimeWithLongevity + { + static void scheduleDestruction( T *obj, void (*func)() ); + static void onDeadReference(); + }; +}; + +#include "LifetimeWithLongevity.inl" + +#endif diff --git a/src/gui/official/utilspp/singleton/LifetimeWithLongevity.inl b/src/gui/official/utilspp/singleton/LifetimeWithLongevity.inl new file mode 100644 index 0000000000..aaaf0ebfb5 --- /dev/null +++ b/src/gui/official/utilspp/singleton/LifetimeWithLongevity.inl @@ -0,0 +1,56 @@ +template< typename T, typename TDestroyer > +void +utilspp::setLongevity( T *obj, unsigned int longevity, TDestroyer d ) +{ + using namespace utilspp::PrivateMembers; + + TrackerArray newArray = static_cast< TrackerArray >( + std::realloc(mTrackerArray, mNbElements + 1)); + if( newArray == NULL ) + { + throw std::bad_alloc(); + } + + LifetimeTracker *p = + new ConcreteLifetimeTracker< T, TDestroyer >(obj, longevity, d); + + mTrackerArray = newArray; + + TrackerArray pos = std::upper_bound( + mTrackerArray, + mTrackerArray + mNbElements, + p, + &LifetimeTracker::compare); + std::copy_backward( + pos, + mTrackerArray + mNbElements, + mTrackerArray + mNbElements + 1); + + *pos = p; + mNbElements++; + std::atexit( &atExitFunc ); +}; + +template< typename T > +void +utilspp::LifetimeWithLongevity< T >::scheduleDestruction( T *obj, void (*func)() ) +{ + utilspp::PrivateMembers::adapter<T> adapter = { func }; + utilspp::setLongevity( obj, getLongevity( obj ), adapter ); +} + +template< typename T > +void +utilspp::LifetimeWithLongevity< T >::onDeadReference() +{ + throw std::logic_error("Dead reference detected"); +} + +template< typename T > +unsigned int +utilspp::getLongevity( T * ) +{ + return 1000; +} + + diff --git a/src/gui/official/utilspp/singleton/PrivateMembers.hpp b/src/gui/official/utilspp/singleton/PrivateMembers.hpp new file mode 100644 index 0000000000..b1fb8cd22e --- /dev/null +++ b/src/gui/official/utilspp/singleton/PrivateMembers.hpp @@ -0,0 +1,93 @@ +/* + * Copyright (c) <2002-2004> <Jean-Philippe Barrette-LaPierre> + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (cURLpp), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef PRIVATE_MEMBERS_HPP +#define PRIVATE_MEMBERS_HPP + +#include <cassert> + +namespace utilspp +{ + namespace PrivateMembers + { + /** + * Helper class for utils::setLongevity + */ + class LifetimeTracker + { + public: + LifetimeTracker( unsigned int longevity ); + virtual ~LifetimeTracker(); + static bool compare( + const LifetimeTracker *l, + const LifetimeTracker *r + ); + + private: + unsigned int mLongevity; + }; + + typedef LifetimeTracker** TrackerArray; + + extern TrackerArray mTrackerArray; + extern int mNbElements; + + /** + * Helper class for Destroyer + */ + template< typename T > + struct Deleter + { + void deleteObject( T *obj ); + }; + + /** + * Concrete lifetime tracker for objects of type T + */ + template< typename T, typename TDestroyer > + class ConcreteLifetimeTracker : public LifetimeTracker + { + public: + ConcreteLifetimeTracker(T *obj, unsigned int longevity, TDestroyer d); + + ~ConcreteLifetimeTracker(); + + private: + T* mTracked; + TDestroyer mDestroyer; + }; + + void atExitFunc(); + + template <class T> + struct adapter + { + void operator()(T*); + void (*mFunc)(); + }; + }; +}; + +#include "PrivateMembers.inl" + +#endif diff --git a/src/gui/official/utilspp/singleton/PrivateMembers.inl b/src/gui/official/utilspp/singleton/PrivateMembers.inl new file mode 100644 index 0000000000..8f3609416c --- /dev/null +++ b/src/gui/official/utilspp/singleton/PrivateMembers.inl @@ -0,0 +1,30 @@ + +template< typename T > +void +utilspp::PrivateMembers::Deleter< T >::deleteObject( T *obj ) +{ + delete obj; +} + +template< typename T, typename TDestroyer > +utilspp::PrivateMembers::ConcreteLifetimeTracker< T, TDestroyer >::ConcreteLifetimeTracker( + T *obj, unsigned int longevity, TDestroyer d) +: LifetimeTracker( longevity ) +, mTracked( obj ) +, mDestroyer( d ) +{} + +template< typename T, typename TDestroyer > +utilspp::PrivateMembers::ConcreteLifetimeTracker< T, TDestroyer >::~ConcreteLifetimeTracker() +{ + mDestroyer( mTracked ); +} + + +template < typename T > +void +utilspp::PrivateMembers::adapter< T >::operator()(T*) +{ + return (*mFunc)(); +} + diff --git a/src/gui/official/utilspp/singleton/SingletonHolder.hpp b/src/gui/official/utilspp/singleton/SingletonHolder.hpp new file mode 100644 index 0000000000..8a8590a7b2 --- /dev/null +++ b/src/gui/official/utilspp/singleton/SingletonHolder.hpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) <2002-2004> <Jean-Philippe Barrette-LaPierre> + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (cURLpp), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef SINGLETON_HOLDER_HPP +#define SINGLETON_HOLDER_HPP + +#include <cassert> + +#include "CreationUsingNew.hpp" +#include "LifetimeDefault.hpp" +#include "LifetimeWithLongevity.hpp" +#include "../ThreadingSingle.hpp" + +namespace utilspp +{ + template + < class T, + template < class > class CreationPolicy = utilspp::CreationUsingNew, + template < class > class LifetimePolicy = utilspp::LifetimeDefault, + template < class > class ThreadingModel = utilspp::ThreadingSingle > + class SingletonHolder + { + public: + //the accessor method. + static T& instance(); + static void makeInstance(); + static void terminate(); + + protected: + //protected to be sure that nobody may create one by himself. + SingletonHolder(); + + private: + static void destroySingleton(); + + private: + typedef typename ThreadingModel< T * >::VolatileType InstanceType; + static InstanceType mInstance; + static bool mDestroyed; + }; + +}; + +#include "SingletonHolder.inl" + +#endif diff --git a/src/gui/official/utilspp/singleton/SingletonHolder.inl b/src/gui/official/utilspp/singleton/SingletonHolder.inl new file mode 100644 index 0000000000..84f27bea9f --- /dev/null +++ b/src/gui/official/utilspp/singleton/SingletonHolder.inl @@ -0,0 +1,126 @@ +/* + * Copyright (c) <2002-2004> <Jean-Philippe Barrette-LaPierre> + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (cURLpp), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef SINGLETON_HOLDER_INL +#define SINGLETON_HOLDER_INL + +template +< +class T, +template < class > class CreationPolicy, +template < class > class LifetimePolicy, +template < class > class ThreadingModel +> +T& +utilspp::SingletonHolder +< +T, +CreationPolicy, +LifetimePolicy, +ThreadingModel +> +::instance() +{ + if ( mInstance == NULL ) + { + makeInstance(); + } + + return ( *mInstance ); +}; + +template +< +class T, +template < class > class CreationPolicy, +template < class > class LifetimePolicy, +template < class > class ThreadingModel +> +void +utilspp::SingletonHolder +< +T, +CreationPolicy, +LifetimePolicy, +ThreadingModel +>::makeInstance() +{ + if ( mInstance == NULL ) + { + typename ThreadingModel< T >::lock guard; + (void)guard; + + if ( mInstance == NULL ) { + if ( mDestroyed ) + { + LifetimePolicy< T >::onDeadReference(); + mDestroyed = false; + } + + mInstance = CreationPolicy< T >::create(); + LifetimePolicy< T >::scheduleDestruction( mInstance, &destroySingleton ); + } + } +} + +template +< +class T, +template < class > class CreationPolicy, +template < class > class LifetimePolicy, +template < class > class ThreadingModel +> +void +utilspp::SingletonHolder +< +T, +CreationPolicy, +LifetimePolicy, +ThreadingModel +> +::destroySingleton() +{ + assert( !mDestroyed ); + CreationPolicy< T >::destroy( mInstance ); + mInstance = NULL; + mDestroyed = true; +} + +template < class T, +template < class > class C, +template < class > class L, +template < class > class M +> +typename utilspp::SingletonHolder< T, C, L, M>::InstanceType +utilspp::SingletonHolder< T, C, L, M >::mInstance; + +template +< +class T, +template < class > class C, +template < class > class L, +template < class > class M +> +bool utilspp::SingletonHolder< T, C, L, M >::mDestroyed; + +#endif -- GitLab