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

FiftyoneDegrees::Common::Value< T > Class Template Reference

Detailed Description

template<class T>
class FiftyoneDegrees::Common::Value< T >

Encapsulates a value returned an instance of ResultsBase for a specified property.

An instance contains either the value, or the reason which there is no value.

Before retrieving the value itself, its presence should first be checked using the hasValue() method. The typed value can be returned either using the getValue() method, or more simply, by dereferencing (similarly to an iterator). If there is no value, then an exception will be thrown.

Usage Example

using namespace FiftyoneDegrees::Common;
// Check that there is a valid value
if (value.hasValue()) {
// Get the value
string stringValue = *value;
// Do something with the value
// ...
}
else {
// Get the reason for the value being invalid
string message = value.getNoValueMessage();
// Do something with the message
// ...
}
// Or just try getting the value and catch the exception. Note that
// this is not the recommended method.
try {
string stringValue = *value;
}
catch (exception e) {
const char *message = e.what();
}
Value()
Default constructor.
Definition Value.hpp:86
const char * getNoValueMessage() const
Gets a message explaining why there is no value.
Definition Value.hpp:121
Shared classes.
Definition Collection.hpp:31
Template Parameters
Tthe type of the value e.g. string
Examples
Hash/GettingStarted.cpp.

Public Member Functions

Constructors

 Value ()
 Default constructor.

Value Methods

bool hasValue () const
 Indicates whether or not a valid value has been returned by the ResultsBase instance.
fiftyoneDegreesResultsNoValueReason getNoValueReason () const
 Indicates the reason why valid values are not available.
const char * getNoValueMessage () const
 Gets a message explaining why there is no value.
T getValue () const
 Gets the value contained in the Value instance.
void setValue (T targetValue)
 Set the value to be contained in the Value instance.
void setNoValueReason (fiftyoneDegreesResultsNoValueReason reason, const char *message)
 Set the reason there is no value available.

Operators

T operator* () const
 Gets the value contained in the Value instance.

Member Function Documentation

◆ getNoValueMessage()

template<class T>
const char * FiftyoneDegrees::Common::Value< T >::getNoValueMessage ( ) const
inline

Gets a message explaining why there is no value.

Calling this if hasValue() returned true will result in undefined behavior.

Returns
message explaining the reason for the missing value
Examples
Hash/GettingStarted.cpp.

◆ getNoValueReason()

template<class T>
inline

Indicates the reason why valid values are not available.

This can be called if hasValue() returns false. For a more detailed message, call getNoValueMessage().

Returns
enum indicating the reason for no valid values

◆ getValue()

template<class T>
T FiftyoneDegrees::Common::Value< T >::getValue ( ) const
inline

Gets the value contained in the Value instance.

If there is no valid value, then an exception will be thrown. To prevent an exception, the hasValue() method should be checked first.

Returns
value
Examples
Hash/GettingStarted.cpp.

◆ hasValue()

template<class T>
bool FiftyoneDegrees::Common::Value< T >::hasValue ( ) const
inline

Indicates whether or not a valid value has been returned by the ResultsBase instance.

Returns
true if there is a valid value
Examples
Hash/GettingStarted.cpp.

◆ operator*()

template<class T>
T FiftyoneDegrees::Common::Value< T >::operator* ( ) const
inline

Gets the value contained in the Value instance.

If there is no valid value, then an exception will be thrown. To prevent an exception, the hasValue() method should be checked first.

Returns
value

◆ setNoValueReason()

template<class T>
void FiftyoneDegrees::Common::Value< T >::setNoValueReason ( fiftyoneDegreesResultsNoValueReason reason,
const char * message )
inline

Set the reason there is no value available.

This will be used if an exception is thrown by the setValue method.

Parameters
reasonthe reason enum indicating why the value is missing
messagethe message containing a fuller description on why the value is missing

◆ setValue()

template<class T>
void FiftyoneDegrees::Common::Value< T >::setValue ( T targetValue)
inline

Set the value to be contained in the Value instance.

Parameters
targetValuethe value to set

The documentation for this class was generated from the following file: