R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
Validated.h
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) 2019 GSI Helmholtzzentrum für Schwerionenforschung GmbH *
3 * Copyright (C) 2019-2025 Members of R3B Collaboration *
4 * *
5 * This software is distributed under the terms of the *
6 * GNU General Public Licence (GPL) version 3, *
7 * copied verbatim in the file "LICENSE". *
8 * *
9 * In applying this license GSI does not waive the privileges and immunities *
10 * granted to it by virtue of its status as an Intergovernmental Organization *
11 * or submit itself to any jurisdiction. *
12 ******************************************************************************/
13
14#ifndef R3BVALIDATED_H
15#define R3BVALIDATED_H
16
17//=============================================================================
18//
19// D A templatised class to represent data which can be validated, for instance
20// D for data caching.
21// D Original: https://bitbucket.org/davidcorne/mutable/
22// D Modified by Jan Mayer
23
24#include <assert.h>
25#include <utility>
26
27//=============================================================================
28template <typename T>
30{
31 public:
33 // invalid constructor
34
35 Validated(const T& data);
36 Validated(T&& data);
37 // valid constructors
38
39 bool valid() const;
40 // is this valid or not?
41
42 void invalidate();
43 // makes the class invalid again, so call in a function which will change the
44 // cached value.
45
46 void set(const T& data);
47 void set(T&& data);
48 // set the data, makes the instance valid.
49
50 T get() const;
51 // explicit conversion
52
53 const T& getRef();
54 // getter with const ref. Use this if you only want to check the value.
55
56 operator T();
57 operator T() const;
58 // implicit conversions to the template type.
59
60 private:
61 bool m_valid;
62 T m_data;
63};
64
65//=============================================================================
66//
67// D Include the source in the header file as it is a templatised class.
68//
69
70//=============================================================================
71template <typename T>
73 //
74 // D Invalid constructor
75 //
76 : m_valid(false)
77{
78}
79
80//=============================================================================
81template <typename T>
83 //
84 // D Valid constructor
85 //
86 : m_valid(true)
87 , m_data(data)
88{
89}
90
91//=============================================================================
92template <typename T>
94 //
95 // D Valid constructor
96 //
97 : m_valid(true)
98 , m_data(std::move(data))
99{
100}
101
102//=============================================================================
103template <typename T>
105//
106// D is this valid or not?
107//
108{
109 return m_valid;
110}
111
112//=============================================================================
113template <typename T>
115//
116// D Makes the class invalid again, so call when the client calls a function
117// D which will change the cached value.
118//
119{
120 m_valid = false;
121}
122
123//=============================================================================
124template <typename T>
125void Validated<T>::set(const T& data)
126//
127// D set the data.
128//
129{
130 m_data = data;
131 m_valid = true;
132}
133
134//=============================================================================
135template <typename T>
136void Validated<T>::set(T&& data)
137//
138// D set the data.
139//
140{
141 m_data = std::move(data);
142 m_valid = true;
143}
144
145//=============================================================================
146template <typename T>
148{
149 assert(valid() && "Cannot return invalid data.");
150 return m_data;
151}
152
153//=============================================================================
154template <typename T>
156{
157 assert(valid() && "Cannot return invalid data.");
158 return m_data;
159}
160
161//=============================================================================
162template <typename T>
164//
165// D implicit conversion to the template type.
166//
167{
168 assert(valid() && "Cannot return invalid data.");
169 return m_data;
170}
171
172//=============================================================================
173template <typename T>
175//
176// D implicit conversion to the template type.
177//
178{
179 // a nice way of returning a message with a standard assert.
180 assert(valid() && "Cannot return invalid data.");
181 return m_data;
182}
183
184#endif // R3BVALIDATED_H
bool valid() const
Definition Validated.h:104
void set(const T &data)
Definition Validated.h:125
void invalidate()
Definition Validated.h:114
const T & getRef()
Definition Validated.h:155
void set(T &&data)
Definition Validated.h:136
Validated(T &&data)
Definition Validated.h:93
Validated(const T &data)
Definition Validated.h:82
T get() const
Definition Validated.h:147