TSDuck v3.40-3963
MPEG Transport Stream Toolkit
Loading...
Searching...
No Matches
tsSingleton.h File Reference

Helper for singleton definition. More...

Namespaces

namespace  ts
 TSDuck namespace, containing all TSDuck classes and functions.
 

Macros

#define TS_DECLARE_GLOBAL(constness, objtype, objname)
 Global object declaration.
 
#define TS_DECLARE_GLOBAL_WRAPPER(exportness, constness, objtype, objname)
 Support macro for TS_DECLARE_GLOBAL, do not use directly.
 
#define TS_DECLARE_SINGLETON(classname)
 Singleton class declaration.
 
#define TS_DECLARE_SINGLETON_BASE(constness, objtype)
 Support macro for TS_DECLARE_SINGLETON, do not use directly.
 
#define TS_DEFINE_GLOBAL(constness, objtype, fullobjname, initializer)
 Global object definition.
 
#define TS_DEFINE_GLOBAL_ATEXIT(constness, objtype, fullobjname, initializer, atexitfunction)
 Global object definition, with specific atexit() function.
 
#define TS_DEFINE_SINGLETON(fullclassname)
 Singleton class definition.
 
#define TS_DEFINE_SINGLETON_ATEXIT(fullclassname, atexitfunction)
 Singleton class definition, with specific atexit() function.
 
#define TS_DEFINE_SINGLETON_BASE(fullclassname, constness, objtype, initializer, atexitfunction)
 Support macro for TS_DEFINE_SINGLETON, do not use directly.
 
#define TS_STATIC_INSTANCE(constness, objtype, objname, initializer)
 Local declaration of a static object regardless of modules initialization order.
 
#define TS_STATIC_INSTANCE_ATEXIT(constness, objtype, objname, initializer, atexitfunction)
 Local declaration of a static object regardless of modules initialization order, with specific atexit() function.
 

Functions

int ts::atexit (void(*func)())
 Register a function to execute when the application exits.
 
void ts::atexit (void(*func)(void *), void *param=nullptr)
 Register a function to execute when the application exits (with a parameter).
 

Detailed Description

Helper for singleton definition.

Macro Definition Documentation

◆ TS_DECLARE_SINGLETON

#define TS_DECLARE_SINGLETON (   classname)

Singleton class declaration.

The macro TS_DECLARE_SINGLETON must be used inside the singleton class declaration.

Parameters
classnameName of the singleton class, without namespace or outer class name.

Example code:

// File: MySingle.h
namespace foo {
class MySingle {
....
#define TS_DECLARE_SINGLETON(classname)
Singleton class declaration.
Definition tsSingleton.h:79
// File: MySingle.cpp
#include "MySingle.h"
TS_DEFINE_SINGLETON(foo::MySingle);
foo::MySingle::MySingle() : ... { ... }
....
#define TS_DEFINE_SINGLETON(fullclassname)
Singleton class definition.
Definition tsSingleton.h:93

The class becomes a singleton. The default constructor is private. Use static Instance() method to get the instance of the singleton.

See also
TS_DEFINE_SINGLETON()

◆ TS_DEFINE_SINGLETON

#define TS_DEFINE_SINGLETON (   fullclassname)

Singleton class definition.

The macro TS_DEFINE_SINGLETON must be used in the implementation of a singleton class.

Parameters
fullclassnameFully qualified name of the singleton class.
See also
TS_DECLARE_SINGLETON()

◆ TS_DEFINE_SINGLETON_ATEXIT

#define TS_DEFINE_SINGLETON_ATEXIT (   fullclassname,
  atexitfunction 
)

Singleton class definition, with specific atexit() function.

Parameters
fullclassnameFully qualified name of the singleton class.
atexitfunctionThe atexit() function to use to register the destructor. Typical values are ts::atexit, std::atexit, OPENSSL_atexit.
See also
TS_DEFINE_SINGLETON()

◆ TS_DECLARE_GLOBAL

#define TS_DECLARE_GLOBAL (   constness,
  objtype,
  objname 
)

Global object declaration.

The macros TS_DECLARE_GLOBAL and TS_DEFINE_GLOBAL are used to declare a global object of a known type. This kind of object cannot be simply statically declared with something like "extern const Foo obj" if it requires a dynamic constructor. Because of the undefined initialization order in the list of modules, the object could be used before being initialized. These macros ensure that the object is dynamically created the first time it is used (thread-safe).

The actual object is wrapped into a singleton and can be accessed using the operators "*" and "->".

Parameters
constnessEither "const" or empty.
objtypeFully qualified type name of the actual object. This object is wrapped into a singleton.
objnameName of the global object. Use operators "*" and "->" to access the objtype object.

Example code:

// File: MyName.h
namespace foo {
TS_DECLARE_GLOBAL(const, std::string, MyName);
#define TS_DECLARE_GLOBAL(constness, objtype, objname)
Global object declaration.
Definition tsSingleton.h:140
// File: MyName.cpp
#include "MyName.h"
TS_DEFINE_GLOBAL(const, foo::MyName, ("the name string"));
void test() { std::cout << NyName->length() << ": " << *MyName << std::endl; }
#define TS_DEFINE_GLOBAL(constness, objtype, fullobjname, initializer)
Global object definition.
Definition tsSingleton.h:155
See also
TS_DEFINE_GLOBAL()

◆ TS_DEFINE_GLOBAL

#define TS_DEFINE_GLOBAL (   constness,
  objtype,
  fullobjname,
  initializer 
)

Global object definition.

Parameters
constnessEither "const" or empty.
objtypeFully qualified type name of the actual object. This object is wrapped into a singleton.
fullobjnameFully qualified name of the global object, including namespace if necessary.
initializerThe initializer list, enclosed within parentheses, of the constructor of the static instance. Use "()" if there is no initializer.
See also
TS_DECLARE_GLOBAL()

◆ TS_DEFINE_GLOBAL_ATEXIT

#define TS_DEFINE_GLOBAL_ATEXIT (   constness,
  objtype,
  fullobjname,
  initializer,
  atexitfunction 
)

Global object definition, with specific atexit() function.

Parameters
constnessEither "const" or empty.
objtypeFully qualified type name of the actual object. This object is wrapped into a singleton.
fullobjnameFully qualified name of the global object, including namespace if necessary.
initializerThe initializer list, enclosed within parentheses, of the constructor of the static instance. Use "()" if there is no initializer.
atexitfunctionThe atexit() function to use to register the destructor. Typical values are ts::atexit, std::atexit, OPENSSL_atexit.
See also
TS_DECLARE_GLOBAL()

◆ TS_STATIC_INSTANCE

#define TS_STATIC_INSTANCE (   constness,
  objtype,
  objname,
  initializer 
)

Local declaration of a static object regardless of modules initialization order.

This macro is a variant of the TS_DECLARE_GLOBAL / TS_DEFINE_GLOBAL pattern.//! The macro TS_STATIC_INSTANCE creates a static object inside the anonymous namespace of the module where it is expanded.

Example using the std::string type: Two static objects Foo1 and Foo2 are created. Remember that Foo1 and Foo2 are not the name of objects but the names of the encapsulating classes of the objects. The first static object is initialized by the default constructor and the parameter ObjectArgs of the macro is simply the empty argument list (). The second static object is built by the constructor std::string (size_t, char) and the parameter ObjectArgs is a list of two parameters (4, '=').

TS_STATIC_INSTANCE(std::string, (), Foo1)
TS_STATIC_INSTANCE(std::string, (4, '='), Foo2)
....
std::cout << "Foo1: " << *Foo1 << std::endl;
std::cout << "Foo2: " << *Foo2 << std::endl;
#define TS_STATIC_INSTANCE(constness, objtype, objname, initializer)
Local declaration of a static object regardless of modules initialization order.
Definition tsSingleton.h:206
Parameters
constnessEither TS_VARIABLE or TS_CONST (if the object is constant and fully defined by its initializer).
objtypeFully qualified type name of the actual object. This object is wrapped into a singleton.
objnameName of the static object. Use operators "*" and "->" to access the objtype object.
initializerThe initializer list, enclosed within parentheses, of the constructor of the static instance. Use "()" if there is no initializer.
See also
TS_DECLARE_GLOBAL()

◆ TS_STATIC_INSTANCE_ATEXIT

#define TS_STATIC_INSTANCE_ATEXIT (   constness,
  objtype,
  objname,
  initializer,
  atexitfunction 
)

Local declaration of a static object regardless of modules initialization order, with specific atexit() function.

Parameters
constnessEither TS_VARIABLE or TS_CONST (if the object is constant and fully defined by its initializer).
objtypeFully qualified type name of the actual object. This object is wrapped into a singleton.
objnameName of the static object. Use operators "*" and "->" to access the objtype object.
initializerThe initializer list, enclosed within parentheses, of the constructor of the static instance. Use "()" if there is no initializer.
atexitfunctionThe atexit() function to use to register the destructor. Typical values are ts::atexit, std::atexit, OPENSSL_atexit.
See also
TS_STATIC_INSTANCE()