Skip to content
Snippets Groups Projects
Commit 1bfd7150 authored by jpbl's avatar jpbl
Browse files

remved

parent 66c8355a
No related branches found
No related tags found
No related merge requests found
/*
* 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 creation_using_new
{
static T *create();
static void destroy( T *obj );
};
};
template< typename T >
T *
utilspp::creation_using_new< T >::create()
{
return new T;
}
template< typename T >
void
utilspp::creation_using_new< T >::destroy( T *obj )
{
delete obj;
}
#endif
/*
* 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 lifetime_default
{
public:
static void schedule_destruction( T *obj, void (*func)() );
static void on_dead_reference();
};
};
template< typename T >
void
utilspp::lifetime_default< T >::schedule_destruction( T *, void (*func)() )
{
std::atexit(func);
}
template< typename T >
void
utilspp::lifetime_default< T >::on_dead_reference()
{
throw std::logic_error( "Dead reference detected" );
}
#endif
/*
* 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 "private_members.hpp"
namespace utilspp
{
template< typename T >
unsigned int get_longevity( 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 T_destroyer >
void set_longevity(
T *obj,
unsigned int longevity,
T_destroyer d = utilspp::private_members::deleter< T >::delete_object
);
template< typename T >
struct lifetime_with_longevity
{
static void schedule_destruction( T *obj, void (*func)() );
static void on_dead_reference();
};
};
#include "lifetime_with_longevity.inl"
#endif
template< typename T, typename T_destroyer >
void
utilspp::set_longevity( T *obj, unsigned int longevity, T_destroyer d )
{
using namespace utilspp::private_members;
tracker_array new_array = static_cast< tracker_array >(
std::realloc(
m_tracker_array,
m_nb_elements + 1
)
);
if( new_array == NULL )
{
throw std::bad_alloc();
}
lifetime_tracker *p = new concrete_lifetime_tracker< T, T_destroyer >(
obj,
longevity,
d
);
m_tracker_array = new_array;
tracker_array pos = std::upper_bound(
m_tracker_array,
m_tracker_array + m_nb_elements,
p,
&lifetime_tracker::compare
);
std::copy_backward(
pos,
m_tracker_array + m_nb_elements,
m_tracker_array + m_nb_elements + 1
);
*pos = p;
m_nb_elements++;
std::atexit( &at_exit_func );
};
template< typename T >
void
utilspp::lifetime_with_longevity< T >::schedule_destruction( T *obj, void (*func)() )
{
utilspp::private_members::adapter<T> adapter = { func };
utilspp::set_longevity( obj, get_longevity( obj ), adapter );
}
template< typename T >
void
utilspp::lifetime_with_longevity< T >::on_dead_reference()
{
throw std::logic_error("Dead reference detected");
}
template< typename T >
unsigned int
utilspp::get_longevity( T * )
{
return 1000;
}
/*
* 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 NULL_TYPE_HPP
#define NULL_TYPE_HPP
namespace utilspp
{
class null_type;
};
#endif
/*
* 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 private_members
{
/**
* Helper class for utils::set_longevity
*/
class lifetime_tracker
{
public:
lifetime_tracker( unsigned int longevity );
virtual ~lifetime_tracker();
static bool compare(
const lifetime_tracker *l,
const lifetime_tracker *r
);
private:
unsigned int m_longevity;
};
typedef lifetime_tracker** tracker_array;
extern tracker_array m_tracker_array;
extern int m_nb_elements;
/**
* Helper class for Destroyer
*/
template< typename T >
struct deleter
{
void delete_object( T *obj );
};
/**
* Concrete lifetime tracker for objects of type T
*/
template< typename T, typename T_destroyer >
class concrete_lifetime_tracker : public lifetime_tracker
{
public:
concrete_lifetime_tracker(
T *obj,
unsigned int longevity,
T_destroyer d
);
~concrete_lifetime_tracker();
private:
T* m_tracked;
T_destroyer m_destroyer;
};
void at_exit_func();
template <class T>
struct adapter
{
void operator()(T*);
void (*m_func)();
};
};
};
#include "private_members.inl"
#endif
template< typename T >
void
utilspp::private_members::deleter< T >::delete_object( T *obj )
{
delete obj;
}
template< typename T, typename T_destroyer >
utilspp::private_members::concrete_lifetime_tracker< T, T_destroyer >::concrete_lifetime_tracker(
T *obj,
unsigned int longevity,
T_destroyer d
)
: lifetime_tracker( longevity )
, m_tracked( obj )
, m_destroyer( d )
{}
template< typename T, typename T_destroyer >
utilspp::private_members::concrete_lifetime_tracker< T, T_destroyer >::~concrete_lifetime_tracker()
{
m_destroyer( m_tracked );
}
template < typename T >
void
utilspp::private_members::adapter< T >::operator()(T*)
{
return (*m_func)();
}
#include "singleton_holder.hpp"
#include "lifetime_library.hpp"
/*
* 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 "creation_using_new.hpp"
#include "lifetime_default.hpp"
#include "threading_single.hpp"
namespace utilspp
{
template
<
class T,
template < class > class T_creation_policy = utilspp::creation_using_new,
template < class > class T_lifetime_policy = utilspp::lifetime_default,
template < class > class T_threading_model = utilspp::threading_single
>
class singleton_holder
{
public:
//the accessor method.
static T& instance();
static void make_instance();
protected:
//protected to be sure that nobody may create one by himself.
singleton_holder();
private:
static void destroy_singleton();
private:
typedef typename T_threading_model< T * >::volatile_type instance_type;
static instance_type m_instance;
static bool m_destroyed;
};
}
#include "singleton_holder.inl"
#endif
/*
* 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 T_creation_policy,
template < class > class T_lifetime_policy,
template < class > class T_threading_model
>
T&
utilspp::singleton_holder
<
T,
T_creation_policy,
T_lifetime_policy,
T_threading_model
>
::instance()
{
if ( m_instance == NULL )
{
make_instance();
}
return ( *m_instance );
};
template
<
class T,
template < class > class T_creation_policy,
template < class > class T_lifetime_policy,
template < class > class T_threading_model
>
void
utilspp::singleton_holder
<
T,
T_creation_policy,
T_lifetime_policy,
T_threading_model
>::make_instance()
{
typename T_threading_model< T >::lock guard;
(void)guard;
if ( m_instance == NULL )
{
if ( m_destroyed )
{
T_lifetime_policy< T >::on_dead_reference();
m_destroyed = false;
}
m_instance = T_creation_policy< T >::create();
T_lifetime_policy< T >::schedule_destruction( m_instance, &destroy_singleton );
}
}
template
<
class T,
template < class > class T_creation_policy,
template < class > class T_lifetime_policy,
template < class > class T_threading_model
>
void
utilspp::singleton_holder
<
T,
T_creation_policy,
T_lifetime_policy,
T_threading_model
>
::destroy_singleton()
{
assert( !m_destroyed );
T_creation_policy< T >::destroy( m_instance );
m_instance = NULL;
m_destroyed = true;
}
template < class T,
template < class > class C,
template < class > class L,
template < class > class M
>
typename utilspp::singleton_holder< T, C, L, M>::instance_type
utilspp::singleton_holder< T, C, L, M >::m_instance;
template
<
class T,
template < class > class C,
template < class > class L,
template < class > class M
>
bool utilspp::singleton_holder< T, C, L, M >::m_destroyed;
#endif
/*
* 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 "null_type.hpp"
namespace utilspp
{
template < typename T = utilspp::null_type >
struct threading_single
{
struct mutex
{
void lock();
void unlock();
};
struct lock
{
lock();
lock( mutex &m );
};
typedef T volatile_type;
};
};
template< typename T >
inline
utilspp::threading_single< T >::lock::lock()
{};
template< typename T >
inline
utilspp::threading_single< T >::lock::lock(
utilspp::threading_single< T >::mutex & )
{}
template< typename T >
inline
void
utilspp::threading_single< T >::mutex::lock()
{};
template< typename T >
inline
void
utilspp::threading_single< T >::mutex::unlock()
{};
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment