e65fb1b64648823d0983bbc7ff3d6d03c03d4a79
[oota-llvm.git] / include / llvm / Support / ManagedStatic.h
1 //===-- llvm/Support/ManagedStatic.h - Static Global wrapper ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the ManagedStatic class and the llvm_shutdown() function.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SUPPORT_MANAGED_STATIC_H
15 #define LLVM_SUPPORT_MANAGED_STATIC_H
16
17 namespace llvm {
18
19 /// object_deleter - Helper method for ManagedStatic.
20 ///
21 template<class C>
22 void object_deleter(void *Ptr) {
23   delete (C*)Ptr;
24 }
25
26 /// ManagedStaticBase - Common base class for ManagedStatic instances.
27 class ManagedStaticBase {
28 protected:
29   // This should only be used as a static variable, which guarantees that this
30   // will be zero initialized.
31   mutable void *Ptr;
32   mutable void (*DeleterFn)(void*);
33   mutable const ManagedStaticBase *Next;
34   
35   void RegisterManagedStatic(void *ObjPtr, void (*deleter)(void*)) const;
36 public:
37   /// isConstructed - Return true if this object has not been created yet.  
38   bool isConstructed() const { return Ptr != 0; }
39   
40   void destroy() const;
41 };
42
43 /// ManagedStatic - This transparently changes the behavior of global statics to
44 /// be lazily constructed on demand (good for reducing startup times of dynamic
45 /// libraries that link in LLVM components) and for making destruction be
46 /// explicit through the llvm_shutdown() function call.
47 ///
48 template<class C>
49 class ManagedStatic : public ManagedStaticBase {
50 public:
51   
52   // Accessors.
53   C &operator*() {
54     if (!Ptr) LazyInit();
55     return *static_cast<C*>(Ptr);
56   }
57   C *operator->() {
58     if (!Ptr) LazyInit();
59     return static_cast<C*>(Ptr);
60   }
61   const C &operator*() const {
62     if (!Ptr) LazyInit();
63     return *static_cast<C*>(Ptr);
64   }
65   const C *operator->() const {
66     if (!Ptr) LazyInit();
67     return static_cast<C*>(Ptr);
68   }
69   
70 public:
71   void LazyInit() const {
72     RegisterManagedStatic(new C(), object_deleter<C>);
73   }
74 };
75
76 template<void (*CleanupFn)(void*)>
77 class ManagedCleanup : public ManagedStaticBase {
78 public:
79   void Register() { RegisterManagedStatic(0, CleanupFn); }
80 };
81
82
83 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
84 void llvm_shutdown();
85
86   
87 /// llvm_shutdown_obj - This is a simple helper class that calls
88 /// llvm_shutdown() when it is destroyed.
89 struct llvm_shutdown_obj {
90   llvm_shutdown_obj() {}
91   ~llvm_shutdown_obj() { llvm_shutdown(); }
92 };
93   
94 }
95
96 #endif