eRPC Generator (erpcgen)  Rev. 1.7.2
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 #include <cstdint>
14 #include <string>
15 
16 typedef enum { kIntegerValue, kStringValue, kFloatValue } value_type_t;
21 class Value
22 {
23 public:
29  Value(value_type_t theType)
30  : m_type(theType)
31  {
32  }
33 
37  virtual ~Value() {}
38 
44  virtual value_type_t getType() const { return m_type; }
45 
51  virtual std::string getTypeName() const = 0;
52 
58  virtual size_t getSize() const = 0;
59 
65  virtual std::string toString() const = 0;
66 
72  virtual Value *clone() const = 0;
73 
74 private:
75  value_type_t m_type;
76 };
77 
81 class IntegerValue : public Value
82 {
83 public:
85  typedef enum { kSigned, kSignedLong, kUnsigned, kUnsignedLong } int_type_t;
86 
90  IntegerValue(int_type_t type = kSigned)
91  : Value(kIntegerValue)
92  , m_value(0)
93  , m_intType(type)
94  {
95  }
96 
102  IntegerValue(uint64_t value, int_type_t type = kSigned)
103  : Value(kIntegerValue)
104  , m_value(value)
105  , m_intType(type)
106  {
107  }
108 
115  : Value(other.getType())
116  , m_value(other.m_value)
117  , m_intType(other.m_intType)
118  {
119  }
120 
126  virtual std::string getTypeName() const { return "integer"; }
127 
133  virtual size_t getSize() const { return sizeof(m_value); }
134 
140  uint64_t getValue() const
141  {
142  return (m_intType == kSignedLong || m_intType == kUnsignedLong) ? m_value : (uint32_t)m_value;
143  }
144 
150  int_type_t getIntType() { return m_intType; }
151 
157  operator uint64_t() const { return m_value; }
158 
166  IntegerValue &operator=(int64_t value)
167  {
168  m_value = value;
169  return *this;
170  }
171 
177  virtual std::string toString() const
178  {
179  if (m_intType == kUnsigned)
180  return format_string("%uU", (uint32_t)m_value);
181  else if (m_intType == kSigned)
182  return format_string("%d", (int32_t)m_value);
183  else if (m_intType == kUnsignedLong)
184  return format_string("%lluU", m_value);
185  else
186  return format_string("%lld", (int64_t)m_value);
187  }
188 
194  virtual Value *clone() const { return new IntegerValue(*this); }
195 
196 protected:
197  uint64_t m_value;
198  int_type_t m_intType;
199 };
200 
204 class FloatValue : public Value
205 {
206 public:
211  : Value(kFloatValue)
212  , m_value(0.0)
213  {
214  }
215 
221  FloatValue(double value)
222  : Value(kFloatValue)
223  , m_value(value)
224  {
225  }
226 
232  FloatValue(float value)
233  : Value(kFloatValue)
234  , m_value(value)
235  {
236  }
237 
243  FloatValue(const FloatValue &other)
244  : Value(kFloatValue)
245  , m_value(other.m_value)
246  {
247  }
248 
257  {
258  m_value = other.m_value;
259  return *this;
260  }
261 
267  virtual std::string getTypeName() const { return "float"; }
268 
274  virtual size_t getSize() const { return sizeof(m_value); }
275 
281  double getValue() const { return m_value; }
282 
288  operator double() const { return m_value; }
289 
295  operator float() const { return static_cast<float>(m_value); }
296 
304  FloatValue &operator=(double value)
305  {
306  m_value = value;
307  return *this;
308  }
309 
317  FloatValue &operator=(float value)
318  {
319  m_value = value;
320  return *this;
321  }
322 
328  virtual std::string toString() const { return format_string("%g", m_value); }
329 
335  virtual Value *clone() const { return new FloatValue(*this); }
336 
337 protected:
338  double m_value;
339 };
340 
346 class StringValue : public Value
347 {
348 public:
353  : Value(kStringValue)
354  , m_value()
355  {
356  }
357 
363  StringValue(const std::string &value)
364  : Value(kStringValue)
365  , m_value(value)
366  {
367  }
368 
374  StringValue(const std::string *value)
375  : Value(kStringValue)
376  , m_value(*value)
377  {
378  }
379 
385  StringValue(const StringValue &other)
386  : Value(kStringValue)
387  , m_value(other.m_value)
388  {
389  }
390 
396  virtual std::string getTypeName() const { return "string"; }
397 
403  virtual size_t getSize() const { return m_value.size(); }
404 
410  const std::string &getString() const { return m_value; }
411 
417  operator const char *() const { return m_value.c_str(); }
418 
424  operator const std::string &() const { return m_value; }
425 
431  operator std::string &() { return m_value; }
432 
438  operator const std::string *() { return &m_value; }
439 
445  operator std::string *() { return &m_value; }
446 
455  {
456  m_value = other.m_value;
457  return *this;
458  }
459 
467  StringValue &operator=(const std::string &value)
468  {
469  m_value = value;
470  return *this;
471  }
472 
480  StringValue &operator=(const char *value)
481  {
482  m_value = value;
483  return *this;
484  }
485 
491  virtual std::string toString() const { return m_value; }
492 
498  virtual Value *clone() const { return new StringValue(*this); }
499 
500 protected:
501  std::string m_value;
502 };
503 
504 #endif // _Value_h_
const std::string & getString() const
Get StringValue value.
Definition: Value.h:410
virtual std::string toString() const
Get StringValue type string representation.
Definition: Value.h:491
uint64_t m_value
The integer value.
Definition: Value.h:197
FloatValue & operator=(float value)
Assign operator.
Definition: Value.h:317
IntegerValue(const IntegerValue &other)
Copy constructor.
Definition: Value.h:114
FloatValue & operator=(const FloatValue &other)
Assign operator.
Definition: Value.h:256
IntegerValue(uint64_t value, int_type_t type=kSigned)
Constructor.
Definition: Value.h:102
StringValue & operator=(const StringValue &other)
Assign operator.
Definition: Value.h:454
virtual std::string getTypeName() const
Get StringValue type name.
Definition: Value.h:396
StringValue(const std::string &value)
Constructor.
Definition: Value.h:363
64-bit integer value.
Definition: Value.h:81
virtual std::string getTypeName() const =0
Get Value type name.
virtual Value * clone() const
Clone FloatValue.
Definition: Value.h:335
uint64_t getValue() const
This function returns value.
Definition: Value.h:140
virtual std::string toString() const =0
Get Value type string representation.
StringValue & operator=(const char *value)
Assign operator.
Definition: Value.h:480
virtual Value * clone() const
Clone IntegerValue.
Definition: Value.h:194
Double floating point value.
Definition: Value.h:204
String value.
Definition: Value.h:346
virtual std::string getTypeName() const
Get IntegerValue type name.
Definition: Value.h:126
virtual size_t getSize() const
Get IntegerValue type size.
Definition: Value.h:133
FloatValue(const FloatValue &other)
Copy constructor.
Definition: Value.h:243
int_type_t m_intType
The integer type.
Definition: Value.h:198
double m_value
The double value.
Definition: Value.h:338
int_type_t
Supported sizes of integers.
Definition: Value.h:85
Abstract base class for values of arbitrary types.
Definition: Value.h:21
virtual value_type_t getType() const
Get Value type.
Definition: Value.h:44
virtual size_t getSize() const
Get StringValue type size.
Definition: Value.h:403
virtual ~Value()
Destructor.
Definition: Value.h:37
virtual Value * clone() const =0
Clone Value.
double getValue() const
This function returns value.
Definition: Value.h:281
virtual std::string getTypeName() const
Get FloatValue type name.
Definition: Value.h:267
virtual Value * clone() const
Clone StringValue.
Definition: Value.h:498
virtual std::string toString() const
Get IntegerValue type string representation.
Definition: Value.h:177
virtual size_t getSize() const
Get FloatValue type size.
Definition: Value.h:274
Value(value_type_t theType)
Constructor.
Definition: Value.h:29
FloatValue(float value)
Constructor.
Definition: Value.h:232
int_type_t getIntType()
This function returns signed/unsigned type.
Definition: Value.h:150
virtual std::string toString() const
Get FloatValue type string representation.
Definition: Value.h:328
IntegerValue(int_type_t type=kSigned)
Constructor.
Definition: Value.h:90
StringValue()
Constructor.
Definition: Value.h:352
FloatValue(double value)
Constructor.
Definition: Value.h:221
FloatValue & operator=(double value)
Assign operator.
Definition: Value.h:304
IntegerValue & operator=(int64_t value)
Assign operator.
Definition: Value.h:166
StringValue(const StringValue &other)
Copy constructor.
Definition: Value.h:385
StringValue & operator=(const std::string &value)
Assign operator.
Definition: Value.h:467
FloatValue()
Constructor.
Definition: Value.h:210
std::string m_value
The string value.
Definition: Value.h:501
virtual size_t getSize() const =0
Get Value type size.
StringValue(const std::string *value)
Constructor.
Definition: Value.h:374