common_util
Loading...
Searching...
No Matches
Macros
singleton.h File Reference

Define some macro definitions for singleton pattern implementations. For convenience to use, this file defined some macro definitions about singleton pattern implementations. More...

#include <mutex>

Go to the source code of this file.

Macros

#define CUTL_COPY_AND_ASSIGN(classname)
 Macro definition for constructor and copy-assignment operator.
 
#define CUTL_SINGLETON_PTR(classname)
 Macro definition for get singleton instance pointer implaementation.
 
#define CUTL_SINGLETON_PTR_DEFAULT_CTOR(classname)
 Macro definition for get singleton instance pointer implementation with default constructor.
 
#define CUTL_SINGLETON_REF(classname)
 Macro definition for get singleton instance reference implaementation.
 
#define CUTL_SINGLETON_REF_DEFAULT_CTOR(classname)
 Macro definition for get singleton instance reference implementation with default constructor.
 

Detailed Description

Define some macro definitions for singleton pattern implementations. For convenience to use, this file defined some macro definitions about singleton pattern implementations.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations.

Author
spencer.luo
Date
2024-05-13

Macro Definition Documentation

◆ CUTL_COPY_AND_ASSIGN

#define CUTL_COPY_AND_ASSIGN ( classname)
Value:
classname(const classname &) = delete; \
classname &operator=(const classname &) = delete;

Macro definition for constructor and copy-assignment operator.

◆ CUTL_SINGLETON_PTR

#define CUTL_SINGLETON_PTR ( classname)
Value:
public: \
static classname *get_instance(bool create_if_needed = true) \
{ \
static classname *obj = nullptr; \
if (!obj && create_if_needed) \
{ \
static std::once_flag flag; \
std::call_once(flag, [&] { obj = new (std::nothrow) classname(); }); \
} \
return obj; \
} \
~classname(); \
\
private: \
classname(); \
CUTL_COPY_AND_ASSIGN(classname)

Macro definition for get singleton instance pointer implaementation.

◆ CUTL_SINGLETON_PTR_DEFAULT_CTOR

#define CUTL_SINGLETON_PTR_DEFAULT_CTOR ( classname)
Value:
public: \
static classname *get_instance(bool create_if_needed = true) \
{ \
static classname *obj = nullptr; \
if (!obj && create_if_needed) \
{ \
static std::once_flag flag; \
std::call_once(flag, [&] { obj = new (std::nothrow) classname(); }); \
} \
return obj; \
} \
~classname(); \
\
private: \
classname() = default; \
CUTL_COPY_AND_ASSIGN(classname)

Macro definition for get singleton instance pointer implementation with default constructor.

◆ CUTL_SINGLETON_REF

#define CUTL_SINGLETON_REF ( classname)
Value:
public: \
static classname &get_instance(bool create_if_needed = true) \
{ \
static classname obj; \
return obj; \
} \
~classname(); \
\
private: \
classname(); \
CUTL_COPY_AND_ASSIGN(classname)

Macro definition for get singleton instance reference implaementation.

◆ CUTL_SINGLETON_REF_DEFAULT_CTOR

#define CUTL_SINGLETON_REF_DEFAULT_CTOR ( classname)
Value:
public: \
static classname &get_instance(bool create_if_needed = true) \
{ \
static classname obj; \
return obj; \
} \
~classname(); \
\
private: \
classname() = default; \
CUTL_COPY_AND_ASSIGN(classname)

Macro definition for get singleton instance reference implementation with default constructor.