nlib
Singleton.h
Go to the documentation of this file.
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_SINGLETON_H_
17 #define INCLUDE_NN_NLIB_SINGLETON_H_
18 
19 #include "nn/nlib/Config.h"
20 
21 NLIB_NAMESPACE_BEGIN
22 
23 template <class T>
25  public:
26  static T& Instance() {
27  if (!flag_) {
29  nlib_once(&once_flag_, Init);
30  }
31  T* ptr = reinterpret_cast<T*>(&memory_[0]);
32  return *ptr;
33  }
34  static T& GetInstance() { return Instance(); }
35 
36  private:
37  static void Init();
38  static int32_t flag_;
39  static nlib_onceflag once_flag_;
40  // Memory leak would be mis-detected if dynamically allocated.
41  NLIB_ALIGNAS(8) static unsigned char memory_[sizeof(T)];
42 
43  private:
44  Singleton() {}
46 };
47 
48 template <class T>
49 void Singleton<T>::Init() {
50  new (memory_) T();
52 }
53 
54 template <class T>
56 template <class T>
57 int32_t Singleton<T>::flag_ = 0;
58 template <class T>
59 
60 #ifndef NLIB_DOXYGEN
61 #if !defined(NN_PLATFORM_CTR)
62 NLIB_ALIGNAS(8) unsigned char Singleton<T>::memory_[sizeof(T)];
63 #else
64 unsigned char Singleton<T>::memory_[sizeof(T)];
65 #endif
66 #endif
67 
68 NLIB_NAMESPACE_END
69 
70 #endif // INCLUDE_NN_NLIB_SINGLETON_H_
int32_t nlib_atomic_load32(const int32_t *ptr, int memorder)
Loads a value in an atomic operation. Its behavior is similar to the one for __atomic_load_n() of gcc...
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:163
#define NLIB_ATOMIC_RELEASE
Similar to __ATOMIC_RELEASE of gcc or std::memory_order_release of C++11.
errno_t nlib_once(nlib_onceflag *flag, nlib_oncefunc func)
Ensures that func is executed only one time at most.
Singleton initialized by the object when first used.
Definition: Singleton.h:24
#define NLIB_ATOMIC_ACQUIRE
Similar to __ATOMIC_ACQUIRE of gcc or std::memory_order_acquire of C++11.
A file that contains the configuration information for each development environment.
#define NLIB_ALIGNAS(x)
Defines alignas(x) or the equivalent.
Definition: Config.h:239
#define NLIB_ONCE_INIT
The value for statically initializing nlib_onceflag.
Definition: Platform.h:1105
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:229
struct nlib_onceflag_ nlib_onceflag
The structure to use with nlib_once.
Definition: Platform.h:1104
static T & Instance()
Gets the singleton instance of T.
Definition: Singleton.h:26
void nlib_atomic_store32(int32_t *ptr, int32_t val, int memorder)
Stores a value in an atomic operation. Its behavior is similar to the one for __atomic_store_n() of g...