WPILibC++  2020.3.2-60-g3011ebe
ManagedStatic.h
1 //===-- llvm/Support/ManagedStatic.h - Static Global wrapper ----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the ManagedStatic class and the wpi_shutdown() function.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef WPIUTIL_WPI_MANAGEDSTATIC_H
15 #define WPIUTIL_WPI_MANAGEDSTATIC_H
16 
17 #include <atomic>
18 #include <cstddef>
19 
20 namespace wpi {
21 
23 template <class C> struct object_creator {
24  static void *call() { return new C(); }
25 };
26 
29 template <typename T> struct object_deleter {
30  static void call(void *Ptr) { delete (T *)Ptr; }
31 };
32 template <typename T, size_t N> struct object_deleter<T[N]> {
33  static void call(void *Ptr) { delete[](T *)Ptr; }
34 };
35 
38 protected:
39  // This should only be used as a static variable, which guarantees that this
40  // will be zero initialized.
41  mutable std::atomic<void *> Ptr;
42  mutable void (*DeleterFn)(void*);
43  mutable const ManagedStaticBase *Next;
44 
45  void RegisterManagedStatic(void *(*creator)(), void (*deleter)(void*)) const;
46  void RegisterManagedStatic(void *created, void (*deleter)(void*)) const;
47 
48 public:
50  bool isConstructed() const { return Ptr != nullptr; }
51 
52  void destroy() const;
53 };
54 
60 template <class C, class Creator = object_creator<C>,
61  class Deleter = object_deleter<C>>
63 public:
64  ManagedStatic() = default;
65 
66  ManagedStatic(C* created, void(*deleter)(void*)) {
67  RegisterManagedStatic(created, deleter);
68  }
69 
70  // Accessors.
71  C &operator*() {
72  void *Tmp = Ptr.load(std::memory_order_acquire);
73  if (!Tmp)
74  RegisterManagedStatic(Creator::call, Deleter::call);
75 
76  return *static_cast<C *>(Ptr.load(std::memory_order_relaxed));
77  }
78 
79  C *operator->() { return &**this; }
80 
81  const C &operator*() const {
82  void *Tmp = Ptr.load(std::memory_order_acquire);
83  if (!Tmp)
84  RegisterManagedStatic(Creator::call, Deleter::call);
85 
86  return *static_cast<C *>(Ptr.load(std::memory_order_relaxed));
87  }
88 
89  const C *operator->() const { return &**this; }
90 };
91 
93 void wpi_shutdown();
94 
98  wpi_shutdown_obj() = default;
100 };
101 
102 } // end namespace wpi
103 
104 #endif // WPIUTIL_WPI_MANAGEDSTATIC_H
wpi::ManagedStaticBase::isConstructed
bool isConstructed() const
isConstructed - Return true if this object has not been created yet.
Definition: ManagedStatic.h:50
wpi::wpi_shutdown_obj
wpi_shutdown_obj - This is a simple helper class that calls wpi_shutdown() when it is destroyed.
Definition: ManagedStatic.h:97
wpi::wpi_shutdown
void wpi_shutdown()
wpi_shutdown - Deallocate and destroy all ManagedStatic variables.
wpi
WPILib C++ utilities (wpiutil) namespace.
Definition: Endian.h:31
wpi::ManagedStaticBase
ManagedStaticBase - Common base class for ManagedStatic instances.
Definition: ManagedStatic.h:37
wpi::ManagedStatic
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:62
wpi::object_creator
object_creator - Helper method for ManagedStatic.
Definition: ManagedStatic.h:23
wpi::object_deleter
object_deleter - Helper method for ManagedStatic.
Definition: ManagedStatic.h:29