eRPC Generator (erpcgen)  Rev. 1.7.0
NXP Semiconductors
Value.h
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2014, Freescale Semiconductor, Inc.
4  * Copyright 2016 NXP
5  * All rights reserved.
6  *
7  *
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted (subject to the limitations in the disclaimer below) provided
10  * that the following conditions are met:
11  *
12  * o Redistributions of source code must retain the above copyright notice, this list
13  * of conditions and the following disclaimer.
14  *
15  * o Redistributions in binary form must reproduce the above copyright notice, this
16  * list of conditions and the following disclaimer in the documentation and/or
17  * other materials provided with the distribution.
18  *
19  * o Neither the name of the copyright holder nor the names of its
20  * contributors may be used to endorse or promote products derived from this
21  * software without specific prior written permission.
22  *
23  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 #if !defined(_Value_h_)
36 #define _Value_h_
37 
38 #include "format_string.h"
39 #include <cstdint>
40 #include <string>
41 
42 typedef enum { kIntegerValue, kStringValue, kFloatValue } value_type_t;
47 class Value
48 {
49 public:
55  Value(value_type_t theType)
56  : m_type(theType)
57  {
58  }
59 
63  virtual ~Value() {}
64 
70  virtual value_type_t getType() const { return m_type; }
71 
77  virtual std::string getTypeName() const = 0;
78 
84  virtual size_t getSize() const = 0;
85 
91  virtual std::string toString() const = 0;
92 
98  virtual Value *clone() const = 0;
99 
100 private:
101  value_type_t m_type;
102 };
103 
107 class IntegerValue : public Value
108 {
109 public:
111  typedef enum { kSigned, kSignedLong, kUnsigned, kUnsignedLong } int_type_t;
112 
116  IntegerValue(int_type_t type = kSigned)
117  : Value(kIntegerValue)
118  , m_value(0)
119  , m_intType(type)
120  {
121  }
122 
128  IntegerValue(uint64_t value, int_type_t type = kSigned)
129  : Value(kIntegerValue)
130  , m_value(value)
131  , m_intType(type)
132  {
133  }
134 
141  : Value(other.getType())
142  , m_value(other.m_value)
143  , m_intType(other.m_intType)
144  {
145  }
146 
152  virtual std::string getTypeName() const { return "integer"; }
153 
159  virtual size_t getSize() const { return sizeof(m_value); }
160 
166  uint64_t getValue() const
167  {
168  return (m_intType == kSignedLong || m_intType == kUnsignedLong) ? m_value : (uint32_t)m_value;
169  }
170 
176  int_type_t getIntType() { return m_intType; }
177 
183  operator uint64_t() const { return m_value; }
184 
192  IntegerValue &operator=(int64_t value)
193  {
194  m_value = value;
195  return *this;
196  }
197 
203  virtual std::string toString() const
204  {
205  if (m_intType == kUnsigned)
206  return format_string("%uU", (uint32_t)m_value);
207  else if (m_intType == kSigned)
208  return format_string("%d", (int32_t)m_value);
209  else if (m_intType == kUnsignedLong)
210  return format_string("%lluU", m_value);
211  else
212  return format_string("%lld", (int64_t)m_value);
213  }
214 
220  virtual Value *clone() const { return new IntegerValue(*this); }
221 
222 protected:
223  uint64_t m_value;
224  int_type_t m_intType;
225 };
226 
230 class FloatValue : public Value
231 {
232 public:
237  : Value(kFloatValue)
238  , m_value(0.0)
239  {
240  }
241 
247  FloatValue(double value)
248  : Value(kFloatValue)
249  , m_value(value)
250  {
251  }
252 
258  FloatValue(float value)
259  : Value(kFloatValue)
260  , m_value(value)
261  {
262  }
263 
269  FloatValue(const FloatValue &other)
270  : Value(kFloatValue)
271  , m_value(other.m_value)
272  {
273  }
274 
283  {
284  m_value = other.m_value;
285  return *this;
286  }
287 
293  virtual std::string getTypeName() const { return "float"; }
294 
300  virtual size_t getSize() const { return sizeof(m_value); }
301 
307  double getValue() const { return m_value; }
308 
314  operator double() const { return m_value; }
315 
321  operator float() const { return static_cast<float>(m_value); }
322 
330  FloatValue &operator=(double value)
331  {
332  m_value = value;
333  return *this;
334  }
335 
343  FloatValue &operator=(float value)
344  {
345  m_value = value;
346  return *this;
347  }
348 
354  virtual std::string toString() const { return format_string("%g", m_value); }
355 
361  virtual Value *clone() const { return new FloatValue(*this); }
362 
363 protected:
364  double m_value;
365 };
366 
372 class StringValue : public Value
373 {
374 public:
379  : Value(kStringValue)
380  , m_value()
381  {
382  }
383 
389  StringValue(const std::string &value)
390  : Value(kStringValue)
391  , m_value(value)
392  {
393  }
394 
400  StringValue(const std::string *value)
401  : Value(kStringValue)
402  , m_value(*value)
403  {
404  }
405 
411  StringValue(const StringValue &other)
412  : Value(kStringValue)
413  , m_value(other.m_value)
414  {
415  }
416 
422  virtual std::string getTypeName() const { return "string"; }
423 
429  virtual size_t getSize() const { return m_value.size(); }
430 
436  const std::string &getString() const { return m_value; }
437 
443  operator const char *() const { return m_value.c_str(); }
444 
450  operator const std::string &() const { return m_value; }
451 
457  operator std::string &() { return m_value; }
458 
464  operator const std::string *() { return &m_value; }
465 
471  operator std::string *() { return &m_value; }
472 
481  {
482  m_value = other.m_value;
483  return *this;
484  }
485 
493  StringValue &operator=(const std::string &value)
494  {
495  m_value = value;
496  return *this;
497  }
498 
506  StringValue &operator=(const char *value)
507  {
508  m_value = value;
509  return *this;
510  }
511 
517  virtual std::string toString() const { return m_value; }
518 
524  virtual Value *clone() const { return new StringValue(*this); }
525 
526 protected:
527  std::string m_value;
528 };
529 
530 #endif // _Value_h_
const std::string & getString() const
Get StringValue value.
Definition: Value.h:436
virtual std::string toString() const
Get StringValue type string representation.
Definition: Value.h:517
uint64_t m_value
The integer value.
Definition: Value.h:223
FloatValue & operator=(float value)
Assign operator.
Definition: Value.h:343
IntegerValue(const IntegerValue &other)
Copy constructor.
Definition: Value.h:140
FloatValue & operator=(const FloatValue &other)
Assign operator.
Definition: Value.h:282
IntegerValue(uint64_t value, int_type_t type=kSigned)
Constructor.
Definition: Value.h:128
StringValue & operator=(const StringValue &other)
Assign operator.
Definition: Value.h:480
virtual std::string getTypeName() const
Get StringValue type name.
Definition: Value.h:422
StringValue(const std::string &value)
Constructor.
Definition: Value.h:389
64-bit integer value.
Definition: Value.h:107
virtual std::string getTypeName() const =0
Get Value type name.
virtual Value * clone() const
Clone FloatValue.
Definition: Value.h:361
uint64_t getValue() const
This function returns value.
Definition: Value.h:166
virtual std::string toString() const =0
Get Value type string representation.
StringValue & operator=(const char *value)
Assign operator.
Definition: Value.h:506
virtual Value * clone() const
Clone IntegerValue.
Definition: Value.h:220
Double floating point value.
Definition: Value.h:230
String value.
Definition: Value.h:372
virtual std::string getTypeName() const
Get IntegerValue type name.
Definition: Value.h:152
virtual size_t getSize() const
Get IntegerValue type size.
Definition: Value.h:159
FloatValue(const FloatValue &other)
Copy constructor.
Definition: Value.h:269
int_type_t m_intType
The integer type.
Definition: Value.h:224
double m_value
The double value.
Definition: Value.h:364
int_type_t
Supported sizes of integers.
Definition: Value.h:111
Abstract base class for values of arbitrary types.
Definition: Value.h:47
virtual value_type_t getType() const
Get Value type.
Definition: Value.h:70
virtual size_t getSize() const
Get StringValue type size.
Definition: Value.h:429
virtual ~Value()
Destructor.
Definition: Value.h:63
virtual Value * clone() const =0
Clone Value.
double getValue() const
This function returns value.
Definition: Value.h:307
virtual std::string getTypeName() const
Get FloatValue type name.
Definition: Value.h:293
virtual Value * clone() const
Clone StringValue.
Definition: Value.h:524
virtual std::string toString() const
Get IntegerValue type string representation.
Definition: Value.h:203
virtual size_t getSize() const
Get FloatValue type size.
Definition: Value.h:300
Value(value_type_t theType)
Constructor.
Definition: Value.h:55
FloatValue(float value)
Constructor.
Definition: Value.h:258
int_type_t getIntType()
This function returns signed/unsigned type.
Definition: Value.h:176
virtual std::string toString() const
Get FloatValue type string representation.
Definition: Value.h:354
IntegerValue(int_type_t type=kSigned)
Constructor.
Definition: Value.h:116
StringValue()
Constructor.
Definition: Value.h:378
FloatValue(double value)
Constructor.
Definition: Value.h:247
FloatValue & operator=(double value)
Assign operator.
Definition: Value.h:330
IntegerValue & operator=(int64_t value)
Assign operator.
Definition: Value.h:192
StringValue(const StringValue &other)
Copy constructor.
Definition: Value.h:411
StringValue & operator=(const std::string &value)
Assign operator.
Definition: Value.h:493
FloatValue()
Constructor.
Definition: Value.h:236
std::string m_value
The string value.
Definition: Value.h:527
virtual size_t getSize() const =0
Get Value type size.
StringValue(const std::string *value)
Constructor.
Definition: Value.h:400