You.i Engine
YiLazy_impl.h
Go to the documentation of this file.
1 // © You i Labs Inc. 2000-2020. All rights reserved.
2 #ifndef _YI_LAZY_IMPL_H
3 #define _YI_LAZY_IMPL_H
4 
5 #if !defined(_YI_LAZY_H_)
6 # error "Do not include YiLazy_impl.h directly. Please include YiLazy.h instead."
7 #endif
8 
9 #include "utility/YiLazy.h"
10 
11 template<class YI_CLASS>
13  : m_pInstance(nullptr)
14 {
15 }
16 
17 template<class YI_CLASS>
19 {
20  YI_CLASS *pInstance = m_pInstance.load();
21  if (pInstance)
22  {
23  delete pInstance;
24  m_pInstance.store(nullptr);
25  }
26 }
27 
28 template<class YI_CLASS>
29 inline const YI_CLASS *CYILazy<YI_CLASS>::Get() const
30 {
31  YI_CLASS *pInstance = m_pInstance.load();
32  if (pInstance == nullptr)
33  {
34  pInstance = new YI_CLASS();
35  YI_CLASS *pNullInstance = nullptr;
36  if (!m_pInstance.compare_exchange_strong(pNullInstance, pInstance))
37  {
38  delete pInstance;
39  pInstance = m_pInstance.load();
40  }
41  }
42  return pInstance;
43 }
44 
45 template<class YI_CLASS>
46 inline YI_CLASS *CYILazy<YI_CLASS>::Get()
47 {
48  return const_cast<YI_CLASS *>(const_cast<const CYILazy<YI_CLASS> &>(*this).Get());
49 }
50 
51 template<class YI_CLASS>
52 inline const YI_CLASS *CYILazy<YI_CLASS>::TryGet() const
53 {
54  return m_pInstance.load();
55 }
56 
57 template<class YI_CLASS>
58 inline YI_CLASS *CYILazy<YI_CLASS>::TryGet()
59 {
60  return m_pInstance.load();
61 }
62 
63 #endif /* _YI_LAZY_IMPL_H */
const YI_CLASS * TryGet() const
Definition: YiLazy_impl.h:52
~CYILazy()
Definition: YiLazy_impl.h:18
CYILazy is a thread-safe lazy initializer of a class. The instance pointer remains null until CYILazy...
Definition: YiLazy.h:47
const YI_CLASS * Get() const
Definition: YiLazy_impl.h:29
CYILazy()
Definition: YiLazy_impl.h:12