KaCanOpen
 All Classes Functions Variables Typedefs Enumerations Pages
value.h
1 /*
2  * Copyright (c) 2015, Thomas Keh
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #pragma once
33 
34 #include <cstdint>
35 #include <string>
36 #include <vector>
37 #include <limits>
38 
39 #include "utils.h"
40 
41 namespace kaco {
42 
53  struct Value {
54 
55  // Check real32 / real64 support
56  static_assert(std::numeric_limits<float>::is_iec559, "Your machine doesn't use IEEE 754 compliant single-precision floating point numbers.");
57  static_assert(sizeof(float)==4, "sizeof(float)!=4 on your machine.");
58  static_assert(sizeof(double)==8, "sizeof(double)!=8 on your machine.");
59 
61  Type type;
62 
65  std::string string;
66 
69  std::vector<uint8_t> octet_string;
70 
72  union {
73  uint8_t uint8;
74  uint16_t uint16;
75  uint32_t uint32;
76  uint64_t uint64;
77  int8_t int8;
78  int16_t int16;
79  int32_t int32;
80  int64_t int64;
81  float real32;
82  double real64;
83  bool boolean;
84  };
85 
87  Value();
88 
90  Value(uint8_t value);
91 
93  Value(uint16_t value);
94 
96  Value(uint32_t value);
97 
99  Value(uint64_t value);
100 
102  Value(int8_t value);
103 
105  Value(int16_t value);
106 
108  Value(int32_t value);
109 
111  Value(int64_t value);
112 
114  Value(float value);
115 
117  Value(double value);
118 
120  Value(bool value);
121 
123  Value(const std::string& value);
124 
126  Value(const char* value);
127 
129  Value(const std::vector<uint8_t>& value);
130 
133  Value(Type type_, const std::vector<uint8_t>& data);
134 
137  std::vector<uint8_t> get_bytes() const;
138 
141  operator uint8_t() const;
142 
145  operator uint16_t() const;
146 
149  operator uint32_t() const;
150 
153  operator uint64_t() const;
154 
157  operator int8_t() const;
158 
161  operator int16_t() const;
162 
165  operator int32_t() const;
166 
169  operator int64_t() const;
170 
173  operator float() const;
174 
177  operator double() const;
178 
181  operator bool() const;
182 
185  operator std::string() const;
186 
189  operator std::vector<uint8_t>() const;
190 
193  bool operator==(const Value& other) const;
194 
197  bool operator!=(const Value& other) const;
198 
200  std::string to_string() const;
201 
202  private:
203 
204  static const bool debug = false;
205 
206  };
207 
208  namespace value_printer {
210  std::ostream &operator<<(std::ostream &os, Value val);
211  }
212 
213 } // end namespace kaco
214 
215 // For correct usage of logger.h one should use the int8_printers, so
216 // the int8_printers namespace is automatically included. Because of
217 // this, logger.h should not be included in library headers.
218 using namespace kaco::value_printer;
bool operator!=(const Value &other) const
Compares not equal.
Definition: value.cpp:414
std::vector< uint8_t > get_bytes() const
Returns the byte representation (little-endian) as a vector.
Definition: value.cpp:222
std::string to_string() const
Returns the value as a printable string.
Definition: value.cpp:418
Value()
Constructs an invalid value.
Definition: value.cpp:41
Type type
Tyoe of the value.
Definition: value.h:56
bool operator==(const Value &other) const
Compares equal.
Definition: value.cpp:341
std::string string
The value if type==Type::string It's seperate because std::string is non-trivial and should not be pa...
Definition: value.h:65
This class contains a value to be stored in the object dictionary. The value can have one of the type...
Definition: value.h:53
std::vector< uint8_t > octet_string
The value if type==Type::octet_string It's seperate because std::vector is non-trivial and should not...
Definition: value.h:69