Kinetis Bootloader Host  2.0.0
Host Tools for Kinetis devices
apps/elftosb/common/Value.h
1 /*
2  * File: Value.h
3  *
4  * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5  * See included license file for license details.
6  */
7 #if !defined(_Value_h_)
8 #define _Value_h_
9 
10 #include "stdafx.h"
11 #include <string>
12 #include "int_size.h"
13 #include "Blob.h"
14 
15 namespace elftosb
16 {
20 class Value
21 {
22 public:
23  Value() {}
24  virtual ~Value() {}
25  virtual std::string getTypeName() const = 0;
26  virtual size_t getSize() const = 0;
27 };
28 
32 class IntegerValue : public Value
33 {
34 public:
35  IntegerValue()
36  : m_value(0)
37  {
38  }
39  IntegerValue(uint32_t value)
40  : m_value(value)
41  {
42  }
43  IntegerValue(const IntegerValue &other)
44  : m_value(other.m_value)
45  {
46  }
47 
48  virtual std::string getTypeName() const { return "integer"; }
49  virtual size_t getSize() const { return sizeof(m_value); }
50  inline uint32_t getValue() const { return m_value; }
51  inline operator uint32_t() const { return m_value; }
52  inline IntegerValue &operator=(uint32_t value)
53  {
54  m_value = value;
55  return *this;
56  }
57 
58 protected:
59  uint32_t m_value;
60 };
61 
73 {
74 public:
76  : IntegerValue()
77  , m_size(kWordSize)
78  {
79  }
80  SizedIntegerValue(uint32_t value, int_size_t size = kWordSize)
81  : IntegerValue(value)
82  , m_size(size)
83  {
84  }
85  SizedIntegerValue(uint16_t value)
86  : IntegerValue(value)
87  , m_size(kHalfWordSize)
88  {
89  }
90  SizedIntegerValue(uint8_t value)
91  : IntegerValue(value)
92  , m_size(kByteSize)
93  {
94  }
96  : IntegerValue(other)
97  , m_size(other.m_size)
98  {
99  }
100 
101  virtual std::string getTypeName() const { return "sized integer"; }
102  virtual size_t getSize() const;
103 
104  inline int_size_t getWordSize() const { return m_size; }
105  inline void setWordSize(int_size_t size) { m_size = size; }
107  uint32_t getWordSizeMask() const;
108 
111 
112  SizedIntegerValue &operator=(uint8_t value)
113  {
114  m_value = value;
115  m_size = kByteSize;
116  return *this;
117  }
118  SizedIntegerValue &operator=(uint16_t value)
119  {
120  m_value = value;
121  m_size = kHalfWordSize;
122  return *this;
123  }
124  SizedIntegerValue &operator=(uint32_t value)
125  {
126  m_value = value;
127  m_size = kWordSize;
128  return *this;
129  }
131 
132 protected:
133  int_size_t m_size;
134 };
135 
141 class StringValue : public Value
142 {
143 public:
144  StringValue()
145  : m_value()
146  {
147  }
148  StringValue(const std::string &value)
149  : m_value(value)
150  {
151  }
152  StringValue(const std::string *value)
153  : m_value(*value)
154  {
155  }
156  StringValue(const StringValue &other)
157  : m_value(other.m_value)
158  {
159  }
160 
161  virtual std::string getTypeName() const { return "string"; }
162  virtual size_t getSize() const { return m_value.size(); }
163  operator const char *() const { return m_value.c_str(); }
164  operator const std::string &() const { return m_value; }
165  operator std::string &() { return m_value; }
166  operator const std::string *() { return &m_value; }
167  operator std::string *() { return &m_value; }
168  StringValue &operator=(const StringValue &other)
169  {
170  m_value = other.m_value;
171  return *this;
172  }
173  StringValue &operator=(const std::string &value)
174  {
175  m_value = value;
176  return *this;
177  }
178  StringValue &operator=(const char *value)
179  {
180  m_value = value;
181  return *this;
182  }
183 
184 protected:
185  std::string m_value;
186 };
187 
191 class BinaryValue : public Value, public Blob
192 {
193 public:
194  BinaryValue()
195  : Value()
196  , Blob()
197  {
198  }
199 
200  virtual std::string getTypeName() const { return "binary"; }
201  virtual size_t getSize() const { return getLength(); }
202 };
203 
204 }; // namespace elftosb
205 
206 #endif // _Value_h_
Binary object value of arbitrary size.
Definition: apps/elftosb/common/Value.h:191
32-bit signed integer value.
Definition: apps/elftosb/common/Value.h:32
uint32_t m_value
The integer value.
Definition: apps/elftosb/common/Value.h:59
Adds a word size attribute to IntegerValue.
Definition: apps/elftosb/common/Value.h:72
Definition: BootImage.h:13
Abstract base class for values of arbitrary types.
Definition: apps/elftosb/common/Value.h:20
String value.
Definition: apps/elftosb/common/Value.h:141
int_size_t m_size
Size of the integer.
Definition: apps/elftosb/common/Value.h:133
Manages a binary object of arbitrary length.
Definition: apps/elftosb/common/Blob.h:18