IDS peak genericC++  v1.6.2
peak_init_once.hpp
Go to the documentation of this file.
1 
10 #pragma once
11 
12 
13 #include <mutex>
14 
15 
16 namespace peak
17 {
18 namespace core
19 {
20 
21 class InitOnce
22 {
23 public:
24  InitOnce() = default;
25  virtual ~InitOnce() = default;
26  InitOnce(const InitOnce& other) = delete;
27  InitOnce& operator=(const InitOnce& other) = delete;
28  InitOnce(InitOnce&& other) = delete;
29  InitOnce& operator=(InitOnce&& other) = delete;
30 
31 protected:
32  // override the Initialize() method to lazy-load data in your derived class, then call InitializeIfNecessary() each
33  // time before accessing the lazy-load data.
34  //
35  // Note: If multiple classes in the inheritance-hierarchy use this lazy-loading mechanism, they should call
36  // the Initialize() Method of their parent class, to make sure all levels are initialized.
37  virtual void Initialize() const = 0;
38  void InitializeIfNecessary() const;
39 
40 private:
41  mutable std::once_flag m_initializedFlag;
42 };
43 
44 } /* namespace core */
45 } /* namespace peak */
46 
47 /* Implementation */
48 namespace peak
49 {
50 namespace core
51 {
52 
53 inline void InitOnce::InitializeIfNecessary() const
54 {
55  std::call_once(m_initializedFlag, [this] { Initialize(); });
56 }
57 
58 } /* namespace core */
59 } /* namespace peak */
The "peak" namespace directly contains convenience functionality like the DeviceManager as well as th...
Definition: peak_dynamic_loader.h:32