eRPC Generator (erpcgen)  Rev. 1.9.0
NXP Semiconductors
Value.h
1 /*
2  * Copyright (c) 2014, Freescale Semiconductor, Inc.
3  * Copyright 2016 NXP
4  * All rights reserved.
5  *
6  *
7  * SPDX-License-Identifier: BSD-3-Clause
8  */
9 #if !defined(_Value_h_)
10 #define _Value_h_
11 
12 #include "format_string.h"
13 
14 #include <cstdint>
15 #include <string>
16 
17 typedef enum
18 {
19  kIntegerValue,
20  kStringValue,
21  kFloatValue
22 } value_type_t;
27 class Value
28 {
29 public:
35  Value(value_type_t theType)
36  : m_type(theType)
37  {
38  }
39 
43  virtual ~Value() {}
44 
50  virtual value_type_t getType() const { return m_type; }
51 
57  virtual std::string getTypeName() const = 0;
58 
64  virtual size_t getSize() const = 0;
65 
71  virtual std::string toString() const = 0;
72 
78  virtual Value *clone() const = 0;
79 
80 private:
81  value_type_t m_type;
82 };
83 
87 class IntegerValue : public Value
88 {
89 public:
91  typedef enum
92  {
93  kSigned,
94  kSignedLong,
95  kUnsigned,
96  kUnsignedLong
97  } int_type_t;
98 
102  IntegerValue(int_type_t type = kSigned)
103  : Value(kIntegerValue)
104  , m_value(0)
105  , m_intType(type)
106  {
107  }
108 
114  IntegerValue(uint64_t value, int_type_t type = kSigned)
115  : Value(kIntegerValue)
116  , m_value(value)
117  , m_intType(type)
118  {
119  }
120 
127  : Value(other.getType())
128  , m_value(other.m_value)
129  , m_intType(other.m_intType)
130  {
131  }
132 
138  virtual std::string getTypeName() const { return "integer"; }
139 
145  virtual size_t getSize() const { return sizeof(m_value); }
146 
152  uint64_t getValue() const
153  {
154  return (m_intType == kSignedLong || m_intType == kUnsignedLong) ? m_value : (uint32_t)m_value;
155  }
156 
162  int_type_t getIntType() { return m_intType; }
163 
169  operator uint64_t() const { return m_value; }
170 
178  IntegerValue &operator=(int64_t value)
179  {
180  m_value = value;
181  return *this;
182  }
183 
189  virtual std::string toString() const
190  {
191  if (m_intType == kUnsigned)
192  return format_string("%uU", (uint32_t)m_value);
193  else if (m_intType == kSigned)
194  return format_string("%d", (int32_t)m_value);
195  else if (m_intType == kUnsignedLong)
196  return format_string("%lluU", m_value);
197  else
198  return format_string("%lld", (int64_t)m_value);
199  }
200 
206  virtual Value *clone() const { return new IntegerValue(*this); }
207 
208 protected:
209  uint64_t m_value;
210  int_type_t m_intType;
211 };
212 
216 class FloatValue : public Value
217 {
218 public:
223  : Value(kFloatValue)
224  , m_value(0.0)
225  {
226  }
227 
233  FloatValue(double value)
234  : Value(kFloatValue)
235  , m_value(value)
236  {
237  }
238 
244  FloatValue(float value)
245  : Value(kFloatValue)
246  , m_value(value)
247  {
248  }
249 
255  FloatValue(const FloatValue &other)
256  : Value(kFloatValue)
257  , m_value(other.m_value)
258  {
259  }
260 
269  {
270  m_value = other.m_value;
271  return *this;
272  }
273 
279  virtual std::string getTypeName() const { return "float"; }
280 
286  virtual size_t getSize() const { return sizeof(m_value); }
287 
293  double getValue() const { return m_value; }
294 
300  operator double() const { return m_value; }
301 
307  operator float() const { return static_cast<float>(m_value); }
308 
316  FloatValue &operator=(double value)
317  {
318  m_value = value;
319  return *this;
320  }
321 
329  FloatValue &operator=(float value)
330  {
331  m_value = value;
332  return *this;
333  }
334 
340  virtual std::string toString() const { return format_string("%g", m_value); }
341 
347  virtual Value *clone() const { return new FloatValue(*this); }
348 
349 protected:
350  double m_value;
351 };
352 
358 class StringValue : public Value
359 {
360 public:
365  : Value(kStringValue)
366  , m_value()
367  {
368  }
369 
375  StringValue(const std::string &value)
376  : Value(kStringValue)
377  , m_value(value)
378  {
379  }
380 
386  StringValue(const std::string *value)
387  : Value(kStringValue)
388  , m_value(*value)
389  {
390  }
391 
397  StringValue(const StringValue &other)
398  : Value(kStringValue)
399  , m_value(other.m_value)
400  {
401  }
402 
408  virtual std::string getTypeName() const { return "string"; }
409 
415  virtual size_t getSize() const { return m_value.size(); }
416 
422  const std::string &getString() const { return m_value; }
423 
429  operator const char *() const { return m_value.c_str(); }
430 
436  operator const std::string &() const { return m_value; }
437 
443  operator std::string &() { return m_value; }
444 
450  operator const std::string *() { return &m_value; }
451 
457  operator std::string *() { return &m_value; }
458 
467  {
468  m_value = other.m_value;
469  return *this;
470  }
471 
479  StringValue &operator=(const std::string &value)
480  {
481  m_value = value;
482  return *this;
483  }
484 
492  StringValue &operator=(const char *value)
493  {
494  m_value = value;
495  return *this;
496  }
497 
503  virtual std::string toString() const { return m_value; }
504 
510  virtual Value *clone() const { return new StringValue(*this); }
511 
512 protected:
513  std::string m_value;
514 };
515 
516 #endif // _Value_h_
const std::string & getString() const
Get StringValue value.
Definition: Value.h:422
virtual std::string toString() const
Get StringValue type string representation.
Definition: Value.h:503
uint64_t m_value
The integer value.
Definition: Value.h:209
FloatValue & operator=(float value)
Assign operator.
Definition: Value.h:329
IntegerValue(const IntegerValue &other)
Copy constructor.
Definition: Value.h:126
FloatValue & operator=(const FloatValue &other)
Assign operator.
Definition: Value.h:268
IntegerValue(uint64_t value, int_type_t type=kSigned)
Constructor.
Definition: Value.h:114
StringValue & operator=(const StringValue &other)
Assign operator.
Definition: Value.h:466
virtual std::string getTypeName() const
Get StringValue type name.
Definition: Value.h:408
StringValue(const std::string &value)
Constructor.
Definition: Value.h:375
64-bit integer value.
Definition: Value.h:87
virtual std::string getTypeName() const =0
Get Value type name.
virtual Value * clone() const
Clone FloatValue.
Definition: Value.h:347
uint64_t getValue() const
This function returns value.
Definition: Value.h:152
virtual std::string toString() const =0
Get Value type string representation.
StringValue & operator=(const char *value)
Assign operator.
Definition: Value.h:492
virtual Value * clone() const
Clone IntegerValue.
Definition: Value.h:206
Double floating point value.
Definition: Value.h:216
String value.
Definition: Value.h:358
virtual std::string getTypeName() const
Get IntegerValue type name.
Definition: Value.h:138
virtual size_t getSize() const
Get IntegerValue type size.
Definition: Value.h:145
FloatValue(const FloatValue &other)
Copy constructor.
Definition: Value.h:255
int_type_t m_intType
The integer type.
Definition: Value.h:210
double m_value
The double value.
Definition: Value.h:350
int_type_t
Supported sizes of integers.
Definition: Value.h:91
Abstract base class for values of arbitrary types.
Definition: Value.h:27
virtual value_type_t getType() const
Get Value type.
Definition: Value.h:50
virtual size_t getSize() const
Get StringValue type size.
Definition: Value.h:415
virtual ~Value()
Destructor.
Definition: Value.h:43
virtual Value * clone() const =0
Clone Value.
double getValue() const
This function returns value.
Definition: Value.h:293
virtual std::string getTypeName() const
Get FloatValue type name.
Definition: Value.h:279
virtual Value * clone() const
Clone StringValue.
Definition: Value.h:510
virtual std::string toString() const
Get IntegerValue type string representation.
Definition: Value.h:189
virtual size_t getSize() const
Get FloatValue type size.
Definition: Value.h:286
Value(value_type_t theType)
Constructor.
Definition: Value.h:35
FloatValue(float value)
Constructor.
Definition: Value.h:244
int_type_t getIntType()
This function returns signed/unsigned type.
Definition: Value.h:162
virtual std::string toString() const
Get FloatValue type string representation.
Definition: Value.h:340
IntegerValue(int_type_t type=kSigned)
Constructor.
Definition: Value.h:102
StringValue()
Constructor.
Definition: Value.h:364
FloatValue(double value)
Constructor.
Definition: Value.h:233
FloatValue & operator=(double value)
Assign operator.
Definition: Value.h:316
IntegerValue & operator=(int64_t value)
Assign operator.
Definition: Value.h:178
StringValue(const StringValue &other)
Copy constructor.
Definition: Value.h:397
StringValue & operator=(const std::string &value)
Assign operator.
Definition: Value.h:479
FloatValue()
Constructor.
Definition: Value.h:222
std::string m_value
The string value.
Definition: Value.h:513
virtual size_t getSize() const =0
Get Value type size.
StringValue(const std::string *value)
Constructor.
Definition: Value.h:386