\r\n

51Degrees Device Detection C/C++  4.4

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 2023 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 "Exceptions.hpp"
27 #include "results.h"
28 
29 
30 namespace FiftyoneDegrees {
31  namespace Common {
74  template <class T> class Value {
75  public:
84  Value() {
85  this->value = T();
86  this->hasValueInternal = false;
87  }
88 
100  bool hasValue() {
101  return hasValueInternal;
102  }
103 
111  return noValueReason;
112  }
113 
119  const char* getNoValueMessage() {
120  return noValueMessage;
121  }
122 
129  T getValue() {
130  if (hasValueInternal) {
131  return value;
132  }
133  else {
134  switch (noValueReason) {
136  if (noValueMessage != nullptr) {
137  throw InvalidPropertyException(noValueMessage);
138  }
139  else {
140  throw InvalidPropertyException();
141  }
143  throw TooManyValuesException();
148  throw NoValuesAvailableException(noValueMessage);
149  default:
151  }
152  }
153  }
154 
159  void setValue(T targetValue) {
160  this->value = targetValue;
161  hasValueInternal = true;
162  }
163 
174  const char *message) {
175  this->noValueReason = reason;
176  this->noValueMessage = message;
177  }
178 
191  T operator*() {
192  return getValue();
193  }
194 
199  private:
200 
201  bool hasValueInternal;
202  T value;
203  const char *noValueMessage = nullptr;
206  };
207  }
208 }
209 
210 #endif
Encapsulates a value returned an instance of ResultsBase for a specified property.
Definition: Value.hpp:74
Exception thrown when a property does not exist in the data set.
Definition: Exceptions.hpp:122
T getValue()
Gets the value contained in the Value instance.
Definition: Value.hpp:129
There are too many values to be expressed as the requested type.
Definition: results.h:80
There are no results to get a value from.
Definition: results.h:77
const char * getNoValueMessage()
Gets a message explaining why there is no value.
Definition: Value.hpp:119
51Degrees base namespace.
Definition: ComponentMetaDataBuilderHash.hpp:33
void setNoValueReason(fiftyoneDegreesResultsNoValueReason reason, const char *message)
Set the reason there is no value available.
Definition: Value.hpp:172
None of the above.
Definition: results.h:88
Exception indicating that there were no values in the results for the requested property.
Definition: Exceptions.hpp:165
The requested property does not exist, or is not a required property.
Definition: results.h:65
bool hasValue()
Indicates whether or not a valid value has been returned by the ResultsBase instance.
Definition: Value.hpp:100
fiftyoneDegreesResultsNoValueReason getNoValueReason()
Indicates the reason why valid values are not available.
Definition: Value.hpp:110
fiftyoneDegreesResultsNoValueReason
Enum containing reasons which cause a value to not be present or valid.
Definition: results.h:56
Value()
Default constructor.
Definition: Value.hpp:84
T operator *()
Gets the value contained in the Value instance.
Definition: Value.hpp:191
The difference value is higher than the threshold, see the Pattern API.
Definition: results.h:57
Exception indicating that an attempt was made to return a single value type (e.g.
Definition: Exceptions.hpp:153
void setValue(T targetValue)
Set the value to be contained in the Value instance.
Definition: Value.hpp:159
There is no result which contains a value for the requested property.
Definition: results.h:70