The Pedigree Project  0.1
LazyEvaluate.h
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef KERNEL_UTILITIES_LAZYEVALUATE_H
21 #define KERNEL_UTILITIES_LAZYEVALUATE_H
22 
23 #include "pedigree/kernel/compiler.h"
24 #include "pedigree/kernel/utilities/utility.h" // for pedigree_std::move
25 
39 template <class T, class M, T *(*create)(const M &), void (*destroy)(T *)>
41 {
42  public:
43  // Default constructor builds a version that can never be evaluated.
44  LazyEvaluate() : m_Metadata(), m_Field(nullptr), m_Ok(false)
45  {
46  }
47  // Lazy variant (only evaluates on access)
48  LazyEvaluate(const M &metadata)
49  : m_Metadata(metadata), m_Field(nullptr), m_Ok(true)
50  {
51  }
52  LazyEvaluate(M &&metadata)
53  : m_Metadata(pedigree_std::move(metadata)), m_Field(nullptr), m_Ok(true)
54  {
55  }
56  // Explicit variants (if the result of evaluation is known already)
57  LazyEvaluate(T *value) : m_Metadata(), m_Field(value), m_Ok(true)
58  {
59  }
60  LazyEvaluate(T *value, const M &metadata)
61  : m_Metadata(metadata), m_Field(value), m_Ok(true)
62  {
63  }
64  LazyEvaluate(T *value, M &&metadata)
65  : m_Metadata(pedigree_std::move(metadata)), m_Field(value), m_Ok(true)
66  {
67  }
68  virtual ~LazyEvaluate()
69  {
70  reset();
71  }
72 
73  bool active() const
74  {
75  return m_Ok && (m_Field != nullptr);
76  }
77 
78  void reset()
79  {
80  if (active())
81  {
82  destroy(m_Field);
83  m_Field = nullptr;
84  }
85  }
86 
87  T *get()
88  {
89  if (active())
90  {
91  return m_Field;
92  }
93  else if (m_Ok)
94  {
95  m_Field = create(m_Metadata);
96  }
97 
98  return m_Field;
99  }
100 
101  T *operator->()
102  {
103  return get();
104  }
105 
106  T &operator*()
107  {
108  return *get();
109  }
110 
111  operator bool() const
112  {
113  // !ok = default constructed
114  return m_Ok;
115  }
116 
117  operator T *()
118  {
119  return get();
120  }
121 
122  private:
123  NOT_COPYABLE_OR_ASSIGNABLE(LazyEvaluate);
124 
125  M m_Metadata;
126  T *m_Field;
127  bool m_Ok;
128 };
129 
130 #endif // KERNEL_UTILITIES_LAZYEVALUATE_H