kodi
GlobalsHandling.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include <memory>
12 
86 namespace xbmcutil
87 {
105  template <class T> class GlobalsSingleton
106  {
113  template <class K> class Deleter
114  {
115  public:
116  K* guarded;
117  ~Deleter() { if (guarded) delete guarded; }
118  };
119 
130  static Deleter<std::shared_ptr<T> > instance;
131 
135  static T* quick;
136  public:
137 
142  inline static std::shared_ptr<T> getInstance()
143  {
144  if (!instance.guarded)
145  {
146  if (!quick)
147  quick = new T;
148  instance.guarded = new std::shared_ptr<T>(quick);
149  }
150  return *(instance.guarded);
151  }
152 
160  inline static T* getQuick()
161  {
162  if (!quick)
163  quick = new T;
164 
165  return quick;
166  }
167 
168  };
169 
170  template <class T> typename GlobalsSingleton<T>::template Deleter<std::shared_ptr<T> > GlobalsSingleton<T>::instance;
171  template <class T> T* GlobalsSingleton<T>::quick;
172 
179  class InitFlag { public: explicit InitFlag(bool& flag) { flag = true; } };
180 }
181 
193 #define XBMC_GLOBAL_REF(classname,g_variable) \
194  static std::shared_ptr<classname> g_variable##Ref(xbmcutil::GlobalsSingleton<classname>::getInstance())
195 
202 #define XBMC_GLOBAL_USE(classname) (*(xbmcutil::GlobalsSingleton<classname>::getQuick()))
static std::shared_ptr< T > getInstance()
Retrieve an instance of the singleton using a shared pointer for reference counting.
Definition: GlobalsHandling.h:142
This is another bit of hackery that will act as a flag for whether or not a global/static has been in...
Definition: GlobalsHandling.h:179
Definition: addon_base.h:267
This file contains the pattern for moving "globals" from the BSS Segment to the heap.
Definition: GlobalsHandling.h:86
static T * getQuick()
This is for quick access when using form (2) of the pattern.
Definition: GlobalsHandling.h:160
This class is an implementation detail of the macros defined below and is NOT meant to be used as a g...
Definition: GlobalsHandling.h:105