51Degrees Device Detection C/C++  4.1

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 2019 51 Degrees Mobile Experts Limited, 5 Charlotte Close,
4  * Caversham, Reading, Berkshire, United Kingdom RG4 7BY.
5  *
6  * This Original Work is licensed under the European Union Public Licence (EUPL)
7  * 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 using namespace std;
30 
31 namespace FiftyoneDegrees {
32  namespace Common {
75  template <class T> class Value {
76  public:
85  Value() {
86  this->value = T();
87  this->hasValueInternal = false;
88  }
89 
101  bool hasValue() {
102  return hasValueInternal;
103  }
104 
112  return noValueReason;
113  }
114 
120  const char* getNoValueMessage() {
121  return noValueMessage;
122  }
123 
130  T getValue() {
131  if (hasValueInternal) {
132  return value;
133  }
134  else {
135  switch (noValueReason) {
137  if (noValueMessage != nullptr) {
138  throw InvalidPropertyException(noValueMessage);
139  }
140  else {
141  throw InvalidPropertyException();
142  }
144  throw TooManyValuesException();
149  throw NoValuesAvailableException(noValueMessage);
150  default:
152  }
153  }
154  }
155 
160  void setValue(T value) {
161  this->value = value;
162  hasValueInternal = true;
163  }
164 
175  const char *message) {
176  this->noValueReason = reason;
177  this->noValueMessage = message;
178  }
179 
192  T operator*() {
193  return getValue();
194  }
195 
200  private:
201 
202  bool hasValueInternal;
203  T value;
204  const char *noValueMessage = nullptr;
207  };
208  }
209 }
210 
211 #endif
Encapsulates a value returned an instance of ResultsBase for a specified property.
Definition: Value.hpp:75
Exception thrown when a property does not exist in the data set.
Definition: Exceptions.hpp:109
T getValue()
Gets the value contained in the Value instance.
Definition: Value.hpp:130
There are too many values to be expressed as the requested type.
Definition: results.h:85
There are no results to get a value from.
Definition: results.h:82
const char * getNoValueMessage()
Gets a message explaining why there is no value.
Definition: Value.hpp:120
51Degrees base namespace.
Definition: ComponentMetaDataBuilderHash.hpp:34
void setNoValueReason(fiftyoneDegreesResultsNoValueReason reason, const char *message)
Set the reason there is no value available.
Definition: Value.hpp:173
None of the above.
Definition: results.h:93
Exception indicating that there were no values in the results for the requested property.
Definition: Exceptions.hpp:152
The requested property does not exist, or is not a required property.
Definition: results.h:70
bool hasValue()
Indicates whether or not a valid value has been returned by the ResultsBase instance.
Definition: Value.hpp:101
fiftyoneDegreesResultsNoValueReason getNoValueReason()
Indicates the reason why valid values are not available.
Definition: Value.hpp:111
fiftyoneDegreesResultsNoValueReason
Enum containing reasons which cause a value to not be present or valid.
Definition: results.h:61
Value()
Default constructor.
Definition: Value.hpp:85
The difference value is higher than the threshold, see the Pattern API.
Definition: results.h:62
void setValue(T value)
Set the value to be contained in the Value instance.
Definition: Value.hpp:160
Exception indicating that an attempt was made to return a single value type (e.g.
Definition: Exceptions.hpp:140
There is no result which contains a value for the requested property.
Definition: results.h:75