Kinetis Bootloader Host  2.0.0
Host Tools for Kinetis devices
src/blfwk/Value.h
1 /*
2  * Copyright (c) 2013-14, Freescale Semiconductor, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  * of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  * list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #if !defined(_Value_h_)
31 #define _Value_h_
32 
33 #include <string>
34 #include "blfwk/stdafx.h"
35 #include "blfwk/int_size.h"
36 #include "blfwk/Blob.h"
37 
38 namespace blfwk
39 {
43 class Value
44 {
45 public:
46  Value() {}
47  virtual ~Value() {}
48  virtual std::string getTypeName() const = 0;
49  virtual size_t getSize() const = 0;
50 };
51 
55 class IntegerValue : public Value
56 {
57 public:
58  IntegerValue()
59  : m_value(0)
60  {
61  }
62  IntegerValue(uint32_t value)
63  : m_value(value)
64  {
65  }
66  IntegerValue(const IntegerValue &other)
67  : m_value(other.m_value)
68  {
69  }
70 
71  virtual std::string getTypeName() const { return "integer"; }
72  virtual size_t getSize() const { return sizeof(m_value); }
73  inline uint32_t getValue() const { return m_value; }
74  inline operator uint32_t() const { return m_value; }
75  inline IntegerValue &operator=(uint32_t value)
76  {
77  m_value = value;
78  return *this;
79  }
80 
81 protected:
82  uint32_t m_value;
83 };
84 
96 {
97 public:
99  : IntegerValue()
100  , m_size(kWordSize)
101  {
102  }
103  SizedIntegerValue(uint32_t value, int_size_t size = kWordSize)
104  : IntegerValue(value)
105  , m_size(size)
106  {
107  }
108  SizedIntegerValue(uint16_t value)
109  : IntegerValue(value)
110  , m_size(kHalfWordSize)
111  {
112  }
113  SizedIntegerValue(uint8_t value)
114  : IntegerValue(value)
115  , m_size(kByteSize)
116  {
117  }
119  : IntegerValue(other)
120  , m_size(other.m_size)
121  {
122  }
123 
124  virtual std::string getTypeName() const { return "sized integer"; }
125  virtual size_t getSize() const;
126 
127  inline int_size_t getWordSize() const { return m_size; }
128  inline void setWordSize(int_size_t size) { m_size = size; }
130  uint32_t getWordSizeMask() const;
131 
134 
135  SizedIntegerValue &operator=(uint8_t value)
136  {
137  m_value = value;
138  m_size = kByteSize;
139  return *this;
140  }
141  SizedIntegerValue &operator=(uint16_t value)
142  {
143  m_value = value;
144  m_size = kHalfWordSize;
145  return *this;
146  }
147  SizedIntegerValue &operator=(uint32_t value)
148  {
149  m_value = value;
150  m_size = kWordSize;
151  return *this;
152  }
154 
155 protected:
156  int_size_t m_size;
157 };
158 
164 class StringValue : public Value
165 {
166 public:
167  StringValue()
168  : m_value()
169  {
170  }
171  StringValue(const std::string &value)
172  : m_value(value)
173  {
174  }
175  StringValue(const std::string *value)
176  : m_value(*value)
177  {
178  }
179  StringValue(const StringValue &other)
180  : m_value(other.m_value)
181  {
182  }
183 
184  virtual std::string getTypeName() const { return "string"; }
185  virtual size_t getSize() const { return m_value.size(); }
186  operator const char *() const { return m_value.c_str(); }
187  operator const std::string &() const { return m_value; }
188  operator std::string &() { return m_value; }
189  operator const std::string *() { return &m_value; }
190  operator std::string *() { return &m_value; }
191  StringValue &operator=(const StringValue &other)
192  {
193  m_value = other.m_value;
194  return *this;
195  }
196  StringValue &operator=(const std::string &value)
197  {
198  m_value = value;
199  return *this;
200  }
201  StringValue &operator=(const char *value)
202  {
203  m_value = value;
204  return *this;
205  }
206 
207 protected:
208  std::string m_value;
209 };
210 
214 class BinaryValue : public Value, public Blob
215 {
216 public:
217  BinaryValue()
218  : Value()
219  , Blob()
220  {
221  }
222 
223  virtual std::string getTypeName() const { return "binary"; }
224  virtual size_t getSize() const { return getLength(); }
225 };
226 
227 }; // namespace blfwk
228 
229 #endif // _Value_h_
Definition: BlfwkErrors.h:16
uint32_t m_value
The integer value.
Definition: src/blfwk/Value.h:82
Adds a word size attribute to IntegerValue.
Definition: src/blfwk/Value.h:95
Abstract base class for values of arbitrary types.
Definition: src/blfwk/Value.h:43
String value.
Definition: src/blfwk/Value.h:164
32-bit signed integer value.
Definition: src/blfwk/Value.h:55
Binary object value of arbitrary size.
Definition: src/blfwk/Value.h:214
Manages a binary object of arbitrary length.
Definition: apps/elftosb/common/Blob.h:18
int_size_t m_size
Size of the integer.
Definition: src/blfwk/Value.h:156