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

Helper for singleton definition. More...

Macros

#define TS_DECLARE_SINGLETON(classname)
 Singleton class declaration.
 
#define TS_DEFINE_SINGLETON(fullclassname)
 Singleton class definition.
 
#define TS_STATIC_INSTANCE(ObjectClass, ObjectArgs, StaticInstanceClass)
 Local declaration of a static object regardless of modules initialization order.
 

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:37

The class becomes a singleton. The default contructor 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.

Example code:

// 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:75
See also
TS_DECLARE_SINGLETON()

◆ TS_STATIC_INSTANCE

#define TS_STATIC_INSTANCE (   ObjectClass,
  ObjectArgs,
  StaticInstanceClass 
)

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

This macro is a variant of the singleton pattern. Instead of being part of a user-defined class, this macro instantiates an object of an existing class. The constructor arguments are passed to the macro.

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::Instance() << std::endl;
std::cout << "Foo2: " << Foo2::Instance() << std::endl;
#define TS_STATIC_INSTANCE(ObjectClass, ObjectArgs, StaticInstanceClass)
Local declaration of a static object regardless of modules initialization order.
Definition tsSingleton.h:127
Parameters
ObjectClassThe name of the class of the static object. This must be an existing class.
ObjectArgsThe initializer list, enclosed within parentheses, of the constructor of the static instance. Use () if there is no initializer.
StaticInstanceClassThe name of the new class which encapsulates the static instance. This class is declared by the expansion of the macro.