Commit 412a0b89 authored by Tristan Matthews's avatar Tristan Matthews

* #9572: Replace utilspp singleton implementation

parent 62b4fc2e
......@@ -51,8 +51,6 @@ Short description of content of source tree
-------------------------------------------
- ringtones/ contains the different ringtones.
- stund/ is an implementation of the protocol STUN used when there is a NAT.
- utilspp/ allows to implement a singleton.
- src/ is the core of SFLphone. It contains the main.cpp, managerimpl.cpp
files, audio and gui directories, and files about signalisation SIP. Later,
it should be better, when IAX will be implemented, that a directory groups
......
......@@ -257,8 +257,6 @@ CXXFLAGS="${CXXFLAGS} -g -Wno-return-type -Wall -Wextra -Wnon-virtual-dtor -Weff
dnl What to generate
AC_CONFIG_FILES([Makefile])
AC_CONFIG_FILES([libs/Makefile \
libs/utilspp/Makefile \
libs/utilspp/singleton/Makefile \
libs/iax2/Makefile])
AC_CONFIG_FILES([src/Makefile \
......
SUBDIRS = utilspp iax2
SUBDIRS = iax2
EXTRA_DIST=pjproject
SUBDIRS = singleton
noinst_LTLIBRARIES = libutilspp.la
libutilspp_la_SOURCES = \
null_type.hpp \
singleton.hpp \
threading_single.hpp threading_single.inl
libutilspp_la_LIBADD = ./singleton/libsingleton.la
/*
* 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
#include "threading_single.hpp"
#include "singleton/singleton_holder.hpp"
noinst_LTLIBRARIES = libsingleton.la
libsingleton_la_SOURCES = \
creation_using_new.hpp creation_using_new.inl \
lifetime_default.hpp lifetime_default.inl \
singleton_holder.hpp singleton_holder.inl
/*
* 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 "creation_using_new.inl"
#endif // __CREATION_USING_NEW_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 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
/*
* 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
namespace utilspp
{
template< typename T >
class LifetimeDefault
{
public:
static void scheduleDestruction( T *obj, void (*func)() );
static void onDeadReference();
};
}
#include "lifetime_default.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 __LIFETIME_DEFAULT_INL__
#define __LIFETIME_DEFAULT_INL__
#include <cstdlib>
#include <stdexcept>
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 // __LIFETIME_DEFAULT_INL__
/*
* 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 "creation_using_new.hpp"
#include "lifetime_default.hpp"
#include "../threading_single.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 "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__
#include <cassert>
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 // __SINGLETON_HOLDER_INL__
/*
* 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_SINGLE_HPP__
#define __THREADING_SINGLE_HPP__
#include "null_type.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 "threading_single.inl"
#endif // __THREADING_SINGLE_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 __THREADING_SINGLE_INL__
#define __THREADING_SINGLE_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 // __THREADING_SINGLE_INL__
......@@ -51,7 +51,6 @@ noinst_HEADERS = \
noncopyable.h
libsflphone_la_LIBADD = \
$(top_builddir)/libs/utilspp/libutilspp.la \
$(top_builddir)/libs/iax2/libiax2.la \
$(IAX_LIB) \
./im/libim.la \
......@@ -91,6 +90,7 @@ libsflphone_la_SOURCES = conference.cpp \
voiplink.cpp \
preferences.cpp \
managerimpl.cpp \
manager.cpp \
managerimpl_registration.cpp \
eventthread.cpp \
call.cpp \
......
......@@ -33,7 +33,7 @@
#include "audio/dcblocker.h"
#include "eventthread.h"
#include "audio/samplerateconverter.h"
#include "managerimpl.h"
#include "manager.h"
#include "noncopyable.h"
#include "dbus/configurationmanager.h"
......
......@@ -37,7 +37,6 @@
#include <cc++/thread.h> // for ost::Mutex
#include <sys/time.h>
#include "manager.h"
#include "ringbuffer.h"
#include "dcblocker.h"
#include "samplerateconverter.h"
......@@ -49,6 +48,7 @@
*/
class MainBuffer;
class AudioPreference;
namespace ost {
class Time;
......
......@@ -28,8 +28,9 @@
* as that of the covered work.
*/
#include <audiostream.h>
#include "audiostream.h"
#include "pulselayer.h"
#include <stdexcept>
AudioStream::AudioStream(pa_context *c, pa_threaded_mainloop *m, const char *desc, int type, int smplrate, std::string& deviceName)
: audiostream_(0), mainloop_(m)
......
......@@ -30,11 +30,12 @@
*/
#include <algorithm> // for std::find
#include <stdexcept>
#include "audiostream.h"
#include "pulselayer.h"
#include "audio/samplerateconverter.h"
#include "audio/dcblocker.h"
#include "managerimpl.h"
#include "manager.h"
namespace {
......
......@@ -30,6 +30,7 @@
#include "networkmanager.h"
#include "../manager.h"
#include "logger.h"
const std::string NetworkManager::statesString[5] = {"unknown", "asleep",
"connecting", "connected",
......
/*
* Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
* Author : Tristan Matthews <tristan.matthews@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Additional permission under GNU GPL version 3 section 7:
*
* If you modify this program, or any covered work, by linking or
* combining it with the OpenSSL project's OpenSSL library (or a
* modified version of that library), containing parts covered by the
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
* grants you additional permission to convey the resulting work.
* Corresponding Source for a non-source form of such a combination
* shall include the source code for the parts of OpenSSL used as well
* as that of the covered work.
*/
#include "manager.h"
ManagerImpl& Manager::instance()
{
// Meyers singleton
static ManagerImpl instance_;
return instance_;
}
......@@ -29,13 +29,16 @@
* as that of the covered work.
*/
#ifndef SFLPHONE_MANAGER_H
#define SFLPHONE_MANAGER_H
#ifndef MANAGER_H_
#define MANAGER_H_
#include "utilspp/singleton.hpp"
// we could forward declare ManagerImpl BUT anyone who will call instance
// will need this include.
#include "managerimpl.h"
typedef utilspp::SingletonHolder<ManagerImpl> Manager;
namespace Manager {
ManagerImpl& instance();
}
#endif
#endif // MANAGER_H_
......@@ -32,8 +32,8 @@
* as that of the covered work.
*/
#ifndef __SFL_MANAGER_H__
#define __SFL_MANAGER_H__
#ifndef MANAGER_IMPL_H_
#define MANAGER_IMPL_H_
#include <string>
#include <vector>
......@@ -1091,4 +1091,4 @@ class ManagerImpl {
*/
History history_;
};
#endif // __MANAGER_H__
#endif // MANAGER_IMPL_H_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment