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
//=============================================================================
28
template
<
typename
T>
29
class
Validated
30
{
31
public
:
32
Validated
();
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
//=============================================================================
71
template
<
typename
T>
72
Validated<T>::Validated
()
73
//
74
// D Invalid constructor
75
//
76
: m_valid(false)
77
{
78
}
79
80
//=============================================================================
81
template
<
typename
T>
82
Validated<T>::Validated
(
const
T& data)
83
//
84
// D Valid constructor
85
//
86
: m_valid(true)
87
, m_data(data)
88
{
89
}
90
91
//=============================================================================
92
template
<
typename
T>
93
Validated<T>::Validated
(T&& data)
94
//
95
// D Valid constructor
96
//
97
: m_valid(true)
98
, m_data(std::move(data))
99
{
100
}
101
102
//=============================================================================
103
template
<
typename
T>
104
bool
Validated<T>::valid
() const
105
//
106
// D is this valid or not?
107
//
108
{
109
return
m_valid;
110
}
111
112
//=============================================================================
113
template
<
typename
T>
114
void
Validated<T>::invalidate
()
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
//=============================================================================
124
template
<
typename
T>
125
void
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
//=============================================================================
135
template
<
typename
T>
136
void
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
//=============================================================================
146
template
<
typename
T>
147
T
Validated<T>::get
()
const
148
{
149
assert(
valid
() &&
"Cannot return invalid data."
);
150
return
m_data;
151
}
152
153
//=============================================================================
154
template
<
typename
T>
155
const
T&
Validated<T>::getRef
()
156
{
157
assert(
valid
() &&
"Cannot return invalid data."
);
158
return
m_data;
159
}
160
161
//=============================================================================
162
template
<
typename
T>
163
Validated<T>::operator
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
//=============================================================================
173
template
<
typename
T>
174
Validated<T>::operator
T() const
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
Validated::valid
bool valid() const
Definition
Validated.h:104
Validated::set
void set(const T &data)
Definition
Validated.h:125
Validated::Validated
Validated()
Definition
Validated.h:72
Validated::invalidate
void invalidate()
Definition
Validated.h:114
Validated::getRef
const T & getRef()
Definition
Validated.h:155
Validated::set
void set(T &&data)
Definition
Validated.h:136
Validated::Validated
Validated(T &&data)
Definition
Validated.h:93
Validated::Validated
Validated(const T &data)
Definition
Validated.h:82
Validated::get
T get() const
Definition
Validated.h:147
neuland
shared
Validated.h
Generated by
1.13.2