A device detection library that is used natively or by 51Degrees products

Value.hpp

1/* *********************************************************************
2 * This Original Work is copyright of 51 Degrees Mobile Experts Limited.
3 * Copyright 2026 51 Degrees Mobile Experts Limited, Davidson House,
4 * Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
5 *
6 * This Original Work is licensed under the European Union Public Licence
7 * (EUPL) v.1.2 and is subject to its terms as set out below.
8 *
9 * If a copy of the EUPL was not distributed with this file, You can obtain
10 * one at https://opensource.org/licenses/EUPL-1.2.
11 *
12 * The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
13 * amended by the European Commission) shall be deemed incompatible for
14 * the purposes of the Work and the provisions of the compatibility
15 * clause in Article 5 of the EUPL shall not apply.
16 *
17 * If using the Work as, or as part of, a network application, by
18 * including the attribution notice(s) required under Article 5 of the EUPL
19 * in the end user terms of the application under an appropriate heading,
20 * such notice(s) shall fulfill the requirements of that article.
21 * ********************************************************************* */
22
23#ifndef FIFTYONE_DEGREES_VALUE_HPP
24#define FIFTYONE_DEGREES_VALUE_HPP
25
26#include <utility>
27#include "Exceptions.hpp"
28#include "results.h"
29
30
31namespace FiftyoneDegrees {
32 namespace Common {
33 FIFTYONE_DEGREES_COMMON_CXX_BEGIN
76 template <class T> class Value {
77 public:
82
87 this->value = T();
88 this->hasValueInternal = false;
89 }
90
96
102 bool hasValue() const {
103 return hasValueInternal;
104 }
105
113 return noValueReason;
114 }
115
121 const char* getNoValueMessage() const {
122 return noValueMessage;
123 }
124
131 T getValue() const {
132 if (hasValueInternal) {
133 return value;
134 }
135 else {
136 switch (noValueReason) {
138 if (noValueMessage != nullptr) {
139 throw InvalidPropertyException(noValueMessage);
140 }
141 else {
143 }
150 throw NoValuesAvailableException(noValueMessage);
151 default:
153 }
154 }
155 }
156
161 void setValue(T targetValue) {
162 this->value = std::move(targetValue);
163 hasValueInternal = true;
164 }
165
176 const char *message) {
177 this->noValueReason = reason;
178 this->noValueMessage = message;
179 }
180
186
193 T operator*() const {
194 return getValue();
195 }
196
200
201 private:
202
203 bool hasValueInternal;
204 T value;
205 const char *noValueMessage = nullptr;
208 };
209 FIFTYONE_DEGREES_COMMON_CXX_END
210 }
211}
212
213#endif
Exception thrown when a property does not exist in the data set.
Definition Exceptions.hpp:123
Exception indicating that there were no values in the results for the requested property.
Definition Exceptions.hpp:166
Exception indicating that an attempt was made to return a single value type (e.g.
Definition Exceptions.hpp:154
Value()
Default constructor.
Definition Value.hpp:86
void setValue(T targetValue)
Set the value to be contained in the Value instance.
Definition Value.hpp:161
T getValue() const
Gets the value contained in the Value instance.
Definition Value.hpp:131
const char * getNoValueMessage() const
Gets a message explaining why there is no value.
Definition Value.hpp:121
T operator*() const
Gets the value contained in the Value instance.
Definition Value.hpp:193
fiftyoneDegreesResultsNoValueReason getNoValueReason() const
Indicates the reason why valid values are not available.
Definition Value.hpp:112
bool hasValue() const
Indicates whether or not a valid value has been returned by the ResultsBase instance.
Definition Value.hpp:102
void setNoValueReason(fiftyoneDegreesResultsNoValueReason reason, const char *message)
Set the reason there is no value available.
Definition Value.hpp:174
fiftyoneDegreesResultsNoValueReason
Enum containing reasons which cause a value to not be present or valid.
Definition results.h:95
@ FIFTYONE_DEGREES_RESULTS_NO_VALUE_REASON_DIFFERENCE
The difference value is higher than the threshold, see the Pattern API.
Definition results.h:96
@ FIFTYONE_DEGREES_RESULTS_NO_VALUE_REASON_INVALID_PROPERTY
The requested property does not exist, or is not a required property.
Definition results.h:104
@ FIFTYONE_DEGREES_RESULTS_NO_VALUE_REASON_NO_RESULT_FOR_PROPERTY
There is no result which contains a value for the requested property.
Definition results.h:109
@ FIFTYONE_DEGREES_RESULTS_NO_VALUE_REASON_UNKNOWN
None of the above.
Definition results.h:131
@ FIFTYONE_DEGREES_RESULTS_NO_VALUE_REASON_NO_RESULTS
There are no results to get a value from.
Definition results.h:116
@ FIFTYONE_DEGREES_RESULTS_NO_VALUE_REASON_TOO_MANY_VALUES
There are too many values to be expressed as the requested type.
Definition results.h:119
Shared classes.
Definition Collection.hpp:31
A C++ wrapper class for transform.h conversion routines.
Definition ComponentMetaDataBuilderHash.hpp:33