summaryrefslogtreecommitdiffstats
path: root/src/lib/cc/element_value.h
blob: ff48561f09604a2d02a2ce1c05e6910ddf84b388 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (C) 2019 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef ELEMENT_VALUE_H
#define ELEMENT_VALUE_H

#include <asiolink/io_address.h>
#include <cc/data.h>
#include <string>

namespace isc {
namespace data {

/// @brief Template class for converting a value encapsulated in the
/// @c Element object into a simple type.
///
/// The @c Element object provides a set of accessors to retrieve
/// values of different types it encapsulates. These methods
/// however can't be always used in template methods and classes.
///
/// Consider a template function which returns a value of a type
/// specified as template argument. In order to convert a value
/// held in the @c Element object it would have to conditionally
/// call this object's accessors to return the value of the
/// appropriate type. This would however fail to compile because
/// the compiler would check for all possible value types returned
/// by the @c Element accessors and report an error for those that
/// don't cast to the returned type.
///
/// This class provides a mechanism to extract the value of the
/// appropriate type from the @c Element object within the
/// template function. It comes with a number of class specializations
/// for various data types to be returned. The default implementation
/// calls @c Element::intValue and casts it to the returned type.
/// There are class specializations for @c double, @c bool and
/// @c string.
///
/// @tparam T Type of the value to be extracted.
template<typename T>
class ElementValue {
public:

    /// @brief Function operator extracting an @c Element value as
    /// integer.
    ///
    /// @param el Element holding a value to be extracted.
    T operator()(ConstElementPtr el) const {
        return (static_cast<T>(el->intValue()));
    }
};

/// @brief The @c ElementValue specialization for double.
template<>
class ElementValue<double> {
public:

    /// @brief Function operator extracting an @c Element value as
    /// double.
    ///
    /// @param el Element holding a value to be extracted.
    double operator()(ConstElementPtr el) const {
        return (el->doubleValue());
    }
};

/// @brief The @c ElementValue specialization for boolean.
template<>
class ElementValue<bool> {
public:

    /// @brief Function operator extracting an @c Element value as
    /// boolean.
    ///
    /// @param el Element holding a value to be extracted.
    bool operator()(ConstElementPtr el) const {
        return (el->boolValue());
    }

};

/// @brief The @c ElementValue specialization for string.
template<>
class ElementValue<std::string> {
public:

    /// @brief Function operator extracting an @c Element value as
    /// string.
    ///
    /// @param el Element holding a value to be extracted.
    std::string operator()(ConstElementPtr el) const {
        return (el->stringValue());
    }
};

/// @brief The @c ElementValue specialization for IOAddress.
template<>
class ElementValue<asiolink::IOAddress> {
public:

    /// @brief Function operator extracting an @c Element value as
    /// IOAddress.
    ///
    /// @note This does NOT support empty string value.
    ///
    /// @param el Element holding a value to be extracted.
    asiolink::IOAddress operator()(ConstElementPtr el) const {
        return (asiolink::IOAddress(el->stringValue()));
    }
};

} // end of namespace isc::data
} // end of namespace isc

#endif // ELEMENT_VALUE_H