mirror of
https://git.jami.net/savoirfairelinux/jami-daemon.git
synced 2025-08-12 22:09:25 +08:00
*** empty log message ***
This commit is contained in:
42
utilspp/NonCopyable.hpp
Normal file
42
utilspp/NonCopyable.hpp
Normal file
@ -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 UTILSPP_NONCOPYABLE_HPP
|
||||
#define UTILSPP_NONCOPYABLE_HPP
|
||||
|
||||
|
||||
namespace utilspp
|
||||
{
|
||||
class NonCopyable
|
||||
{
|
||||
public:
|
||||
NonCopyable()
|
||||
{}
|
||||
|
||||
private:
|
||||
NonCopyable(const NonCopyable& r)
|
||||
{}
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
32
utilspp/NullType.hpp
Normal file
32
utilspp/NullType.hpp
Normal file
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* 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 UTILSPP_NULLTYPE_HPP
|
||||
#define UTILSPP_NULLTYPE_HPP
|
||||
|
||||
namespace utilspp
|
||||
{
|
||||
struct NullType;
|
||||
};
|
||||
|
||||
#endif
|
3
utilspp/Singleton.hpp
Normal file
3
utilspp/Singleton.hpp
Normal file
@ -0,0 +1,3 @@
|
||||
#include "ThreadingSingle.hpp"
|
||||
#include "singleton/SingletonHolder.hpp"
|
||||
#include "singleton/LifetimeLibrary.hpp"
|
186
utilspp/SmartPtr.hpp
Normal file
186
utilspp/SmartPtr.hpp
Normal file
@ -0,0 +1,186 @@
|
||||
/*
|
||||
* 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 UTILSPP_SMARTPTR_HPP
|
||||
#define UTILSPP_SMARTPTR_HPP
|
||||
|
||||
#include <stdexcept>
|
||||
#include "NonCopyable.hpp"
|
||||
|
||||
#define NULL_BODY_ERROR "the smart pointer contain a NULL pointer"
|
||||
|
||||
namespace utilspp
|
||||
{
|
||||
|
||||
template < typename Type = unsigned int >
|
||||
class FastCount
|
||||
{
|
||||
public:
|
||||
FastCount(Type count = 1) : mCount(count)
|
||||
{}
|
||||
|
||||
FastCount &operator++()
|
||||
{
|
||||
mCount++;
|
||||
return *this;
|
||||
}
|
||||
|
||||
FastCount &operator--()
|
||||
{
|
||||
mCount--;
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator Type()
|
||||
{
|
||||
return mCount;
|
||||
}
|
||||
|
||||
Type useCount()
|
||||
{
|
||||
return mCount;
|
||||
}
|
||||
|
||||
private:
|
||||
Type mCount;
|
||||
};
|
||||
|
||||
|
||||
template < typename ContentType, typename CountPolicy = FastCount >
|
||||
class CountingBody : public utilspp::NonCopyable
|
||||
{
|
||||
public:
|
||||
CountingBody(ContentType *body) : mBody(body)
|
||||
{}
|
||||
|
||||
void inc()
|
||||
{
|
||||
++mCount;
|
||||
}
|
||||
|
||||
void dec()
|
||||
{
|
||||
--mCount;
|
||||
if (mCount <= 0) {
|
||||
delete this;
|
||||
}
|
||||
}
|
||||
|
||||
ContentType *get()
|
||||
{
|
||||
return mBody;
|
||||
}
|
||||
|
||||
protected:
|
||||
~CountingBody()
|
||||
{
|
||||
if (mBody != NULL) {
|
||||
delete mBody;
|
||||
mBody = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
CountPolicy mCount;
|
||||
ContentType *mBody;
|
||||
};
|
||||
|
||||
|
||||
template < typename ContentType, typename CountingBodyPolicy = CountingBody>
|
||||
class SharedPtr
|
||||
{
|
||||
public:
|
||||
SharedPtr() : mContent(new CountingPolicy< ContentType >(NULL))
|
||||
{}
|
||||
|
||||
explicit SharedPtr(ContentType *content) : mContent(new CountingBodyPolicy< ContentType >(content))
|
||||
{}
|
||||
|
||||
~SharedPtr()
|
||||
{
|
||||
mContent->dec();
|
||||
}
|
||||
|
||||
SharedPtr(const SharedPtr &other) : mContent(other.mContent)
|
||||
{
|
||||
mContent->inc();
|
||||
}
|
||||
|
||||
SharedPtr& operator=(const SharedPtr &other)
|
||||
{
|
||||
if(mContent->get() != other.mContent->get()) {
|
||||
mContent->dec();
|
||||
mContent = other.mContent;
|
||||
mContent->inc();
|
||||
}
|
||||
return ( *this );
|
||||
}
|
||||
|
||||
SharedPtr& operator=(ContentType *content)
|
||||
{
|
||||
mContent--;
|
||||
mContent = new CountingBodyPolicy< ContentType >(content);
|
||||
}
|
||||
|
||||
bool operator==(const SharedPtr &other) const
|
||||
{
|
||||
return (mContent->get() == other.mContent->get());
|
||||
}
|
||||
|
||||
bool operator!=(const SharedPtr &other) const
|
||||
{
|
||||
return (mContent->get() != other.mContent->get());
|
||||
}
|
||||
|
||||
bool operator<(const SharedPtr &other) const
|
||||
{
|
||||
return (mContent->get() < other.mContent->get());
|
||||
}
|
||||
|
||||
operator ContentType*()
|
||||
{
|
||||
return mContent->get();
|
||||
}
|
||||
|
||||
ContentType& operator*()
|
||||
{
|
||||
if(mContent->get() == NULL) {
|
||||
throw std::runtime_error(NULL_BODY_ERROR);
|
||||
}
|
||||
return *mContent->get();
|
||||
}
|
||||
|
||||
ContentType* operator->()
|
||||
{
|
||||
if(mContent->get() == NULL) {
|
||||
throw std::runtime_error(NULL_BODY_ERROR);
|
||||
}
|
||||
return mContent->get();
|
||||
}
|
||||
|
||||
private:
|
||||
CountingBodyPolicy * mContent;
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
44
utilspp/ThreadingFactoryMutex.hpp
Normal file
44
utilspp/ThreadingFactoryMutex.hpp
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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 THREADING_FACTORY_MUTEX_HPP
|
||||
#define THREADING_FACTORY_MUTEX_HPP
|
||||
|
||||
namespace utilspp
|
||||
{
|
||||
template < typename T >
|
||||
struct ThreadingFactoryMutex
|
||||
{
|
||||
struct lock
|
||||
{
|
||||
lock();
|
||||
lock( const T & );
|
||||
};
|
||||
|
||||
typedef T VolatileType;
|
||||
};
|
||||
};
|
||||
|
||||
#include "ThreadingFactoryMutex.inl"
|
||||
|
||||
#endif
|
37
utilspp/ThreadingFactoryMutex.inl
Normal file
37
utilspp/ThreadingFactoryMutex.inl
Normal file
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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 THREADING_FACTORY_MUTEX_INL
|
||||
#define THREADING_FACTORY_MUTEX_INL
|
||||
|
||||
template< typename T >
|
||||
inline
|
||||
utilspp::ThreadingSingle< T >::lock::lock()
|
||||
{};
|
||||
|
||||
template< typename T >
|
||||
inline
|
||||
utilspp::ThreadingSingle< T >::lock::lock( const T & )
|
||||
{};
|
||||
|
||||
#endif
|
52
utilspp/ThreadingSingle.hpp
Normal file
52
utilspp/ThreadingSingle.hpp
Normal file
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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 SINGLE_THREADED_HPP
|
||||
#define SINGLE_THREADED_HPP
|
||||
|
||||
#include "NullType.hpp"
|
||||
|
||||
namespace utilspp
|
||||
{
|
||||
template < typename T = utilspp::NullType >
|
||||
struct ThreadingSingle
|
||||
{
|
||||
struct mutex
|
||||
{
|
||||
void lock();
|
||||
void unlock();
|
||||
};
|
||||
|
||||
struct lock
|
||||
{
|
||||
lock();
|
||||
lock( mutex &m );
|
||||
};
|
||||
|
||||
typedef T VolatileType;
|
||||
};
|
||||
};
|
||||
|
||||
#include "ThreadingSingle.inl"
|
||||
|
||||
#endif
|
50
utilspp/ThreadingSingle.inl
Normal file
50
utilspp/ThreadingSingle.inl
Normal file
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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 SINGLE_THREADED_INL
|
||||
#define SINGLE_THREADED_INL
|
||||
|
||||
template< typename T >
|
||||
inline
|
||||
utilspp::ThreadingSingle< T >::lock::lock()
|
||||
{};
|
||||
|
||||
template< typename T >
|
||||
inline
|
||||
utilspp::ThreadingSingle< T >::lock::lock(
|
||||
utilspp::ThreadingSingle< T >::mutex & )
|
||||
{}
|
||||
|
||||
template< typename T >
|
||||
inline
|
||||
void
|
||||
utilspp::ThreadingSingle< T >::mutex::lock()
|
||||
{};
|
||||
|
||||
template< typename T >
|
||||
inline
|
||||
void
|
||||
utilspp::ThreadingSingle< T >::mutex::unlock()
|
||||
{};
|
||||
|
||||
#endif
|
170
utilspp/TypeList.hpp
Normal file
170
utilspp/TypeList.hpp
Normal file
@ -0,0 +1,170 @@
|
||||
/*
|
||||
* 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 TYPE_LIST_HPP
|
||||
#define TYPE_LIST_HPP
|
||||
|
||||
#include "NullType.hpp"
|
||||
|
||||
namespace utilspp
|
||||
{
|
||||
namespace tl
|
||||
{
|
||||
template< class T, class U >
|
||||
struct TypeList
|
||||
{
|
||||
typedef T head;
|
||||
typedef U tail;
|
||||
};
|
||||
|
||||
//Calculating length of TypeLists
|
||||
template< class TList >
|
||||
struct length;
|
||||
|
||||
template<>
|
||||
struct length< NullType >
|
||||
{
|
||||
enum { value = 0 };
|
||||
};
|
||||
|
||||
template< class T, class U >
|
||||
struct length< TypeList< T, U > >
|
||||
{
|
||||
enum { value = 1 + length< U >::value };
|
||||
};
|
||||
|
||||
//Indexed access
|
||||
template< class TList, unsigned int index >
|
||||
struct TypeAt;
|
||||
|
||||
template< class THead, class TTail >
|
||||
struct TypeAt< TypeList< THead, TTail >, 0 >
|
||||
{
|
||||
typedef head result;
|
||||
}
|
||||
|
||||
template< class THead, class TTail, unsigned int i >
|
||||
struct TypeAt< TypeList< THead, TTail >, i >
|
||||
{
|
||||
typedef typename TypeAt< TTail, i - 1 >::result result;
|
||||
};
|
||||
|
||||
//Searching TypeLists
|
||||
template< class TList, class T >
|
||||
struct index_of;
|
||||
|
||||
template< class T >
|
||||
struct index_of< NullType, T >
|
||||
{
|
||||
enum { value = -1 };
|
||||
};
|
||||
|
||||
template< class TTail, class T >
|
||||
struct index_of< TypeList< T, TTail >, T >
|
||||
{
|
||||
enum { value = 0 };
|
||||
};
|
||||
|
||||
template< class THead, class TTail, class T >
|
||||
struct index_of< TypeList< THead, TTail >, T >
|
||||
{
|
||||
private:
|
||||
enum { temp = index_of< TTail, T >::value > };
|
||||
|
||||
public:
|
||||
enum { value = temp == -1 ? -1 : 1 + temp };
|
||||
};
|
||||
|
||||
//Appending to TypeLists
|
||||
template< class TList, class T >
|
||||
struct append;
|
||||
|
||||
template <>
|
||||
struct append< NullType, NullType >
|
||||
{
|
||||
typedef NullType result;
|
||||
};
|
||||
|
||||
template< class T >
|
||||
struct append< NullType, T >
|
||||
{
|
||||
typedef TYPELIST_1( T ) result;
|
||||
};
|
||||
|
||||
template< class THead, class TTail >
|
||||
struct append< NullType, NullType, TypeList< THead, TTail > >
|
||||
{
|
||||
typedef TypeList< THead, TTail > result;
|
||||
};
|
||||
|
||||
template < class THead, class TTail, class T >
|
||||
struct append< TypeList< THead, TTail >, T >
|
||||
{
|
||||
typedef TypeList< THead, typename append< TTail, T >::result >
|
||||
result;
|
||||
};
|
||||
|
||||
//Erasing a type from a TypeList
|
||||
template< class TList, class T >
|
||||
struct erase;
|
||||
|
||||
template< class T >
|
||||
struct erase< NullType, T >
|
||||
{
|
||||
typedef NullType result;
|
||||
};
|
||||
|
||||
template< class T, class TTail >
|
||||
struct erase< TypeList< T, TTail >, T >
|
||||
{
|
||||
typedef TTail result;
|
||||
};
|
||||
|
||||
template< class THead, class TTail, class T >
|
||||
struct erase< TypeList< THead, TTail >, T >
|
||||
{
|
||||
typedef TypeList< THead, typename erase< TTail, T >::result >
|
||||
result;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
#define TYPELIST_1( T1 ) ::utilspp::TypeList< T1, ::utilspp::NullType >
|
||||
#define TYPE_LIST_2( T1, T2 ) ::utilspp::TypeList< T1, TYPE_LIST_1( T2 ) >
|
||||
#define TYPE_LIST_3( T1, T2, T3 ) ::utilspp::TypeList< T1, TYPE_LIST_2( T2, T3 ) >
|
||||
#define TYPE_LIST_4( T1, T2, T3, T4 ) ::utilspp::TypeList< T1, TYPE_LIST_3( T2, T3, T4 ) >
|
||||
#define TYPE_LIST_5( T1, T2, T3, T4, T5 ) \
|
||||
::utilspp::TypeList< T1, TYPE_LIST_4( T2, T3, T4, T5 ) >
|
||||
#define TYPE_LIST_6( T1, T2, T3, T4, T5, T6 ) \
|
||||
::utilspp::TypeList< T1, TYPE_LIST_5( T2, T3, T4, T5, T6 ) >
|
||||
#define TYPE_LIST_7( T1, T2, T3, T4, T5, T6, T7 ) \
|
||||
::utilspp::TypeList< T1, TYPE_LIST_6( T2, T3, T4, T5, T6, T7 ) >
|
||||
#define TYPE_LIST_8( T1, T2, T3, T4, T5, T6, T7, T8 ) \
|
||||
::utilspp::TypeList< T1, TYPE_LIST_7( T2, T3, T4, T5, T6, T7, T8 ) >
|
||||
#define TYPE_LIST_9( T1, T2, T3, T4, T5, T6, T7, T8, T9 ) \
|
||||
::utilspp::TypeList< T1, TYPE_LIST_8( T2, T3, T4, T5, T6, T7, T8, T9 ) >
|
||||
#define TYPE_LIST_10( T1, T2, T3, T4, T5, T6, T7, T8, T9, T10 ) \
|
||||
::utilspp::TypeList< T1, TYPE_LIST_9( T2, T3, T4, T5, T6, T7, T8, T9, T10 ) >
|
||||
|
||||
#endif
|
||||
|
200
utilspp/TypeTrait.hpp
Normal file
200
utilspp/TypeTrait.hpp
Normal file
@ -0,0 +1,200 @@
|
||||
/*
|
||||
* 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 UTILSPP_TYPETRAIT_HPP
|
||||
#define UTILSPP_TYPETRAIT_HPP
|
||||
|
||||
#include "NullType.hpp"
|
||||
|
||||
namespace utilspp
|
||||
{
|
||||
template< typename T >
|
||||
class TypeTrait
|
||||
{
|
||||
private:
|
||||
template< typename U >
|
||||
struct unreference
|
||||
{
|
||||
typedef U type;
|
||||
};
|
||||
|
||||
template< typename U >
|
||||
struct unreference< U & >
|
||||
{
|
||||
typedef U type;
|
||||
};
|
||||
|
||||
template< typename U >
|
||||
struct unconst
|
||||
{
|
||||
typedef U type;
|
||||
};
|
||||
|
||||
template< typename U >
|
||||
struct unconst< const U >
|
||||
{
|
||||
typedef U type;
|
||||
};
|
||||
|
||||
public:
|
||||
typedef typename unreference< T >::type NonReference;
|
||||
typedef typename unconst< T >::type NonConst;
|
||||
typedef typename unconst< unreference< T >::type >::type NonParam;
|
||||
};
|
||||
|
||||
template< class T >
|
||||
struct PointerOnMemberFunction
|
||||
{
|
||||
typedef ::utilspp::NullType ClassType;
|
||||
typedef ::utilspp::NullType ReturnType;
|
||||
typedef ::utilspp::NullType ParamType;
|
||||
};
|
||||
|
||||
template< typename V, typename W, typename R >
|
||||
struct PointerOnMemberFunction< W(V::*)(R) >
|
||||
{
|
||||
typedef V ClassType;
|
||||
typedef W ReturnType;
|
||||
typedef R ParamType;
|
||||
};
|
||||
|
||||
template< typename T >
|
||||
struct PointerOnFunction
|
||||
{
|
||||
typedef utilspp::NullType ReturnType;
|
||||
typedef utilspp::NullType Param1Type;
|
||||
typedef utilspp::NullType Param2Type;
|
||||
typedef utilspp::NullType Param3;
|
||||
typedef utilspp::NullType Param4Type;
|
||||
typedef utilspp::NullType Param5Type;
|
||||
typedef utilspp::NullType Param6Type;
|
||||
typedef utilspp::NullType Param7Type;
|
||||
};
|
||||
|
||||
template< typename V >
|
||||
struct PointerOnFunction< V(*)() >
|
||||
{
|
||||
typedef V ReturnType;
|
||||
typedef utilspp::NullType Param1Type;
|
||||
typedef utilspp::NullType Param2Type;
|
||||
typedef utilspp::NullType Param3Type;
|
||||
typedef utilspp::NullType Param4Type;
|
||||
typedef utilspp::NullType Param5Type;
|
||||
typedef utilspp::NullType Param6Type;
|
||||
typedef utilspp::NullType Param7Type;
|
||||
};
|
||||
|
||||
template< typename V, typename W >
|
||||
struct PointerOnFunction< V(*)(W) >
|
||||
{
|
||||
typedef V ReturnType;
|
||||
typedef W Param1Type;
|
||||
typedef utilspp::NullType Param2Type;
|
||||
typedef utilspp::NullType Param3Type;
|
||||
typedef utilspp::NullType Param4Type;
|
||||
typedef utilspp::NullType Param5Type;
|
||||
typedef utilspp::NullType Param6Type;
|
||||
typedef utilspp::NullType Param7Type;
|
||||
};
|
||||
|
||||
template< typename V, typename W, typename X >
|
||||
struct PointerOnFunction< V(*)(W, X) >
|
||||
{
|
||||
typedef V ReturnType;
|
||||
typedef W Param1Type;
|
||||
typedef X Param2Type;
|
||||
typedef utilspp::NullType Param3Type;
|
||||
typedef utilspp::NullType Param4Type;
|
||||
typedef utilspp::NullType Param5Type;
|
||||
typedef utilspp::NullType Param6Type;
|
||||
typedef utilspp::NullType Param7Type;
|
||||
};
|
||||
|
||||
template< typename V, typename W, typename X, typename Y >
|
||||
struct PointerOnFunction< V(*)(W, X, Y) >
|
||||
{
|
||||
typedef V ReturnType;
|
||||
typedef W Param1Type;
|
||||
typedef X Param2Type;
|
||||
typedef Y Param3Type;
|
||||
typedef utilspp::NullType Param4Type;
|
||||
typedef utilspp::NullType Param5Type;
|
||||
typedef utilspp::NullType Param6Type;
|
||||
typedef utilspp::NullType Param7Type;
|
||||
};
|
||||
|
||||
template< typename V, typename W, typename X, typename Y, typename Z >
|
||||
struct PointerOnFunction< V(*)(W, X, Y, Z) >
|
||||
{
|
||||
typedef V ReturnType;
|
||||
typedef W Param1Type;
|
||||
typedef X Param2Type;
|
||||
typedef Y Param3Type;
|
||||
typedef Z Param4Type;
|
||||
typedef utilspp::NullType Param5Type;
|
||||
typedef utilspp::NullType Param6Type;
|
||||
typedef utilspp::NullType Param7Type;
|
||||
};
|
||||
|
||||
template< typename V, typename W, typename X, typename Y, typename Z, typename A >
|
||||
struct PointerOnFunction< V(*)(W, X, Y, Z, A) >
|
||||
{
|
||||
typedef V ReturnType;
|
||||
typedef W Param1Type;
|
||||
typedef X Param2Type;
|
||||
typedef Y Param3Type;
|
||||
typedef Z Param4Type;
|
||||
typedef A Param5Type;
|
||||
typedef utilspp::NullType Param6Type;
|
||||
typedef utilspp::NullType Param7Type;
|
||||
};
|
||||
|
||||
template< typename V, typename W, typename X, typename Y, typename Z, typename A, typename B >
|
||||
struct PointerOnFunction< V(*)(W, X, Y, Z, A, B) >
|
||||
{
|
||||
typedef V ReturnType;
|
||||
typedef W Param1Type;
|
||||
typedef X Param2Type;
|
||||
typedef Y Param3Type;
|
||||
typedef Z Param4Type;
|
||||
typedef A Param5Type;
|
||||
typedef B Param6Type;
|
||||
typedef utilspp::NullType Param7Type;
|
||||
};
|
||||
|
||||
template< typename V, typename W, typename X, typename Y, typename Z, typename A, typename B, typename C >
|
||||
struct PointerOnFunction< V(*)(W, X, Y, Z, A, B, C) >
|
||||
{
|
||||
typedef V ReturnType;
|
||||
typedef W Param1Type;
|
||||
typedef X Param2Type;
|
||||
typedef Y Param3Type;
|
||||
typedef Z Param4Type;
|
||||
typedef A Param5Type;
|
||||
typedef B Param6Type;
|
||||
typedef C Param7Type;
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
#endif
|
49
utilspp/singleton/CreationStatic.hpp
Normal file
49
utilspp/singleton/CreationStatic.hpp
Normal file
@ -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
|
43
utilspp/singleton/CreationStatic.inl
Normal file
43
utilspp/singleton/CreationStatic.inl
Normal file
@ -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
|
43
utilspp/singleton/CreationUsingNew.hpp
Normal file
43
utilspp/singleton/CreationUsingNew.hpp
Normal file
@ -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
|
42
utilspp/singleton/CreationUsingNew.inl
Normal file
42
utilspp/singleton/CreationUsingNew.inl
Normal file
@ -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
|
42
utilspp/singleton/LifetimeDefault.hpp
Normal file
42
utilspp/singleton/LifetimeDefault.hpp
Normal file
@ -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_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();
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
#endif
|
42
utilspp/singleton/LifetimeDefault.inl
Normal file
42
utilspp/singleton/LifetimeDefault.inl
Normal file
@ -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
|
104
utilspp/singleton/LifetimeLibrary.hpp
Normal file
104
utilspp/singleton/LifetimeLibrary.hpp
Normal file
@ -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
|
33
utilspp/singleton/LifetimeLibrary.inl
Normal file
33
utilspp/singleton/LifetimeLibrary.inl
Normal file
@ -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();
|
||||
}
|
||||
|
56
utilspp/singleton/LifetimeWithLongevity.hpp
Normal file
56
utilspp/singleton/LifetimeWithLongevity.hpp
Normal file
@ -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
|
56
utilspp/singleton/LifetimeWithLongevity.inl
Normal file
56
utilspp/singleton/LifetimeWithLongevity.inl
Normal file
@ -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;
|
||||
}
|
||||
|
||||
|
22
utilspp/singleton/Makefile.am
Normal file
22
utilspp/singleton/Makefile.am
Normal file
@ -0,0 +1,22 @@
|
||||
noinst_LTLIBRARIES = libsingleton.la
|
||||
|
||||
libsingleton_la_SOURCES = \
|
||||
CreationStatic.hpp \
|
||||
CreationUsingNew.hpp \
|
||||
LifetimeDefault.hpp \
|
||||
LifetimeLibrary.cpp LifetimeLibrary.hpp LifetimeLibrary.inl \
|
||||
LifetimeWithLongevity.hpp LifetimeWithLongevity.inl \
|
||||
PrivateMembers.cpp PrivateMembers.hpp PrivateMembers.inl \
|
||||
SingletonHolder.hpp SingletonHolder.inl
|
||||
|
||||
pkginclude_HEADERS = \
|
||||
CreationStatic.hpp \
|
||||
CreationUsingNew.hpp \
|
||||
LifetimeDefault.hpp \
|
||||
LifetimeLibrary.hpp LifetimeLibrary.inl \
|
||||
LifetimeWithLongevity.hpp LifetimeWithLongevity.inl \
|
||||
PrivateMembers.hpp PrivateMembers.inl \
|
||||
SingletonHolder.hpp SingletonHolder.inl
|
||||
|
||||
pkgincludedir=$(includedir)/utilspp/singleton
|
||||
|
93
utilspp/singleton/PrivateMembers.hpp
Normal file
93
utilspp/singleton/PrivateMembers.hpp
Normal file
@ -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
|
30
utilspp/singleton/PrivateMembers.inl
Normal file
30
utilspp/singleton/PrivateMembers.inl
Normal file
@ -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)();
|
||||
}
|
||||
|
66
utilspp/singleton/SingletonHolder.hpp
Normal file
66
utilspp/singleton/SingletonHolder.hpp
Normal file
@ -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
|
126
utilspp/singleton/SingletonHolder.inl
Normal file
126
utilspp/singleton/SingletonHolder.inl
Normal file
@ -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
|
@ -24,6 +24,7 @@
|
||||
#ifndef UTILSPP_NONCOPYABLE_HPP
|
||||
#define UTILSPP_NONCOPYABLE_HPP
|
||||
|
||||
|
||||
namespace utilspp
|
||||
{
|
||||
class NonCopyable
|
||||
|
Reference in New Issue
Block a user