51Degrees Common C/C++  4.1

A shared functionality library that is used by 51Degrees products

Value.hpp

1 /* *********************************************************************
2  * This Source Code Form 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 Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0.
8  *
9  * If a copy of the MPL was not distributed with this file, You can obtain
10  * one at http://mozilla.org/MPL/2.0/.
11  *
12  * This Source Code Form is "Incompatible With Secondary Licenses", as
13  * defined by the Mozilla Public License, v. 2.0.
14  * ********************************************************************* */
15 
16 #ifndef FIFTYONE_DEGREES_VALUE_HPP
17 #define FIFTYONE_DEGREES_VALUE_HPP
18 
19 #include "Exceptions.hpp"
20 #include "results.h"
21 
22 using namespace std;
23 
24 namespace FiftyoneDegrees {
25  namespace Common {
68  template <class T> class Value {
69  public:
78  Value() {
79  this->hasValueInternal = false;
80  }
81 
93  bool hasValue() {
94  return hasValueInternal;
95  }
96 
104  return noValueReason;
105  }
106 
112  const char* getNoValueMessage() {
113  return noValueMessage;
114  }
115 
122  T getValue() {
123  if (hasValueInternal) {
124  return value;
125  }
126  else {
127  switch (noValueReason) {
129  if (noValueMessage != nullptr) {
130  throw InvalidPropertyException(noValueMessage);
131  }
132  else {
133  throw InvalidPropertyException();
134  }
136  throw TooManyValuesException();
141  throw NoValuesAvailableException(noValueMessage);
142  default:
144  }
145  }
146  }
147 
152  void setValue(T value) {
153  this->value = value;
154  hasValueInternal = true;
155  }
156 
167  const char *message) {
168  this->noValueReason = reason;
169  this->noValueMessage = message;
170  }
171 
184  T operator*() {
185  return getValue();
186  }
187 
192  private:
193 
194  bool hasValueInternal;
195  T value;
196  const char *noValueMessage = nullptr;
199  };
200  }
201 }
202 
203 #endif
Encapsulates a value returned an instance of ResultsBase for a specified property.
Definition: Value.hpp:68
Exception thrown when a property does not exist in the data set.
Definition: Exceptions.hpp:102
T getValue()
Gets the value contained in the Value instance.
Definition: Value.hpp:122
There are too many values to be expressed as the requested type.
Definition: results.h:78
There are no results to get a value from.
Definition: results.h:75
const char * getNoValueMessage()
Gets a message explaining why there is no value.
Definition: Value.hpp:112
51Degrees base namespace.
Definition: Collection.hpp:24
void setNoValueReason(fiftyoneDegreesResultsNoValueReason reason, const char *message)
Set the reason there is no value available.
Definition: Value.hpp:165
None of the above.
Definition: results.h:82
Exception indicating that there were no values in the results for the requested property.
Definition: Exceptions.hpp:145
The requested property does not exist, or is not a required property.
Definition: results.h:63
bool hasValue()
Indicates whether or not a valid value has been returned by the ResultsBase instance.
Definition: Value.hpp:93
fiftyoneDegreesResultsNoValueReason getNoValueReason()
Indicates the reason why valid values are not available.
Definition: Value.hpp:103
fiftyoneDegreesResultsNoValueReason
Enum containing reasons which cause a value to not be present or valid.
Definition: results.h:54
Value()
Default constructor.
Definition: Value.hpp:78
The difference value is higher than the threshold, see the Pattern API.
Definition: results.h:55
void setValue(T value)
Set the value to be contained in the Value instance.
Definition: Value.hpp:152
Exception indicating that an attempt was made to return a single value type (e.g.
Definition: Exceptions.hpp:133
There is no result which contains a value for the requested property.
Definition: results.h:68