PMDA++  0.4.4
Header-only C++ library for writing PCP PMDAs
atom.hpp
Go to the documentation of this file.
1 // Copyright Paul Colby 2013 - 2015.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE.md or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5 
6 /**
7  * @file
8  * @brief Defines the pcp::atom template class.
9  */
10 
11 #ifndef __PCP_CPP_ATOM_HPP__
12 #define __PCP_CPP_ATOM_HPP__
13 
14 #include "config.hpp"
15 #include "exception.hpp"
16 #include "types.hpp"
17 
18 PCP_CPP_BEGIN_NAMESPACE
19 
20 namespace pcp {
21 
22 /**
23  * @brief Caset a value to a PCP pmAtomValue.
24  *
25  * This base template definition handles most numeric types.
26  *
27  * @param type The atom type to set.
28  * @param value The atom value to set.
29  *
30  * @tparam ValueType Type of value to set.
31  *
32  * @return A `pmAtomValue` containing \c value of type \c type.
33  */
34 template <typename ValueType>
35 pmAtomValue atom(const atom_type_type type, ValueType value)
36 {
37  pmAtomValue atom;
38  switch (type) {
39  case PM_TYPE_32: atom.l = static_cast< int32_t>(value); break;
40  case PM_TYPE_U32: atom.ul = static_cast<uint32_t>(value); break;
41  case PM_TYPE_64: atom.ll = static_cast< int64_t>(value); break;
42  case PM_TYPE_U64: atom.ull = static_cast<uint64_t>(value); break;
43  case PM_TYPE_FLOAT: atom.f = static_cast<float >(value); break;
44  case PM_TYPE_DOUBLE: atom.d = static_cast<double >(value); break;
45  //case PM_TYPE_STRING: atom.cp = value; break;
46  //case PM_TYPE_AGGREGATE: atom.vbp = value; break;
47  //case PM_TYPE_AGGREGATE_STATIC: atom.vbp = value; break;
48  //case PM_TYPE_EVENT: atom.vbp = value; break;
49  //case PM_TYPE_HIGHRES_EVENT: atom.vbp = value; break;
50  default:
51  throw pcp::exception(PM_ERR_TYPE);
52  }
53  return atom;
54 }
55 
56 /**
57  * @brief Caset a value to a PCP pmAtomValue.
58  *
59  * This template specialisation hanldes `char *` strings.
60  *
61  * @note We do not provide a `const char *` template specialisation, since that
62  * would require us to `const_cast` the value, which is something the
63  * caller ought to be very clear about / aware is happening. Hence, if you
64  * want to use a `const char *`, use something like:
65  * `pcp::atom(PM_TYPE_STRING, const_cast<char *>(value)`
66  *
67  * @param type The atom type to set.
68  * @param value The atom value to set.
69  *
70  * @return A `pmAtomValue` containing \c value of type \c type.
71  */
72 template <>
73 inline pmAtomValue atom<char *>(const atom_type_type type, char * value)
74 {
75  pmAtomValue atom;
76  switch (type) {
77  //case PM_TYPE_32: atom.l = value; break;
78  //case PM_TYPE_U32: atom.ul = value; break;
79  //case PM_TYPE_64: atom.ll = value; break;
80  //case PM_TYPE_U64: atom.ull = value; break;
81  //case PM_TYPE_FLOAT: atom.f = value; break;
82  //case PM_TYPE_DOUBLE: atom.d = value; break;
83  case PM_TYPE_STRING: atom.cp = value; break;
84  //case PM_TYPE_AGGREGATE: atom.vbp = value; break;
85  //case PM_TYPE_AGGREGATE_STATIC: atom.vbp = value; break;
86  //case PM_TYPE_EVENT: atom.vbp = value; break;
87  //case PM_TYPE_HIGHRES_EVENT: atom.vbp = value; break;
88  default:
89  throw pcp::exception(PM_ERR_TYPE);
90  }
91  return atom;
92 }
93 
94 /**
95  * @brief Caset a value to a PCP pmAtomValue.
96  *
97  * This template specialisation hanldes `pmValueBlock` values.
98  *
99  * @param type The atom type to set.
100  * @param value The atom value to set.
101  *
102  * @return A `pmAtomValue` containing \c value of type \c type.
103  */
104 template <>
105 inline pmAtomValue atom<pmValueBlock *>(const atom_type_type type, pmValueBlock * value)
106 {
107  pmAtomValue atom;
108  switch (type) {
109  //case PM_TYPE_32: atom.l = value; break;
110  //case PM_TYPE_U32: atom.ul = value; break;
111  //case PM_TYPE_64: atom.ll = value; break;
112  //case PM_TYPE_U64: atom.ull = value; break;
113  //case PM_TYPE_FLOAT: atom.f = value; break;
114  //case PM_TYPE_DOUBLE: atom.d = value; break;
115  //case PM_TYPE_STRING: atom.cp = value; break;
116  case PM_TYPE_AGGREGATE: atom.vbp = value; break;
117  case PM_TYPE_AGGREGATE_STATIC: atom.vbp = value; break;
118  case PM_TYPE_EVENT: atom.vbp = value; break;
119 #ifdef PM_TYPE_HIGHRES_EVENT // PM_TYPE_HIGHRES_EVENT added in PCP 3.9.10.
120  case PM_TYPE_HIGHRES_EVENT: atom.vbp = value; break;
121 #endif
122  default:
123  throw pcp::exception(PM_ERR_TYPE);
124  }
125  return atom;
126 }
127 
128 } // pcp namespace.
129 
130 PCP_CPP_END_NAMESPACE
131 
132 #endif
Definition: atom.hpp:20
uint_fast8_t atom_type_type
PM_TYPE_* (0 - 9)
Definition: types.hpp:22
Sets up common PMDA++ library macros.
Declares various types used throughout the PMDA++ library.
Base class for all PMDA++ exceptions.
Definition: exception.hpp:25
pmAtomValue atom(const atom_type_type type, ValueType value)
Caset a value to a PCP pmAtomValue.
Definition: atom.hpp:35
Defines the pcp::exception class.
atom_type_type type()
Get the PM_TYPE_* constant for a given C++ type.