4 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5 * See included license file for license details.
7 #if !defined(_Value_h_)
19 * \brief Abstract base class for values of arbitrary types.
27 virtual std::string getTypeName() const = 0;
28 virtual size_t getSize() const = 0;
32 * \brief 32-bit signed integer value.
34 class IntegerValue : public Value
37 IntegerValue() : m_value(0) {}
38 IntegerValue(uint32_t value) : m_value(value) {}
39 IntegerValue(const IntegerValue & other) : m_value(other.m_value) {}
41 virtual std::string getTypeName() const { return "integer"; }
42 virtual size_t getSize() const { return sizeof(m_value); }
44 inline uint32_t getValue() const { return m_value; }
46 inline operator uint32_t () const { return m_value; }
48 inline IntegerValue & operator = (uint32_t value) { m_value = value; return *this; }
51 uint32_t m_value; //!< The integer value.
55 * \brief Adds a word size attribute to IntegerValue.
57 * The word size really only acts as an attribute that is carried along
58 * with the integer value. It doesn't affect the actual value at all.
59 * However, you can use the getWordSizeMask() method to mask off bits
60 * that should not be there.
62 * The word size defaults to a 32-bit word.
64 class SizedIntegerValue : public IntegerValue
67 SizedIntegerValue() : IntegerValue(), m_size(kWordSize) {}
68 SizedIntegerValue(uint32_t value, int_size_t size=kWordSize) : IntegerValue(value), m_size(size) {}
69 SizedIntegerValue(uint16_t value) : IntegerValue(value), m_size(kHalfWordSize) {}
70 SizedIntegerValue(uint8_t value) : IntegerValue(value), m_size(kByteSize) {}
71 SizedIntegerValue(const SizedIntegerValue & other) : IntegerValue(other), m_size(other.m_size) {}
73 virtual std::string getTypeName() const { return "sized integer"; }
74 virtual size_t getSize() const;
76 inline int_size_t getWordSize() const { return m_size; }
77 inline void setWordSize(int_size_t size) { m_size = size; }
79 //! \brief Returns a 32-bit mask value dependant on the word size attribute.
80 uint32_t getWordSizeMask() const;
82 //! \name Assignment operators
83 //! These operators set the word size as well as the integer value.
85 SizedIntegerValue & operator = (uint8_t value) { m_value = value; m_size = kByteSize; return *this; }
86 SizedIntegerValue & operator = (uint16_t value) { m_value = value; m_size = kHalfWordSize; return *this; }
87 SizedIntegerValue & operator = (uint32_t value) { m_value = value; m_size = kWordSize; return *this; }
91 int_size_t m_size; //!< Size of the integer.
95 * \brief String value.
97 * Simply wraps the STL std::string class.
99 class StringValue : public Value
102 StringValue() : m_value() {}
103 StringValue(const std::string & value) : m_value(value) {}
104 StringValue(const std::string * value) : m_value(*value) {}
105 StringValue(const StringValue & other) : m_value(other.m_value) {}
107 virtual std::string getTypeName() const { return "string"; }
108 virtual size_t getSize() const { return m_value.size(); }
110 operator const char * () const { return m_value.c_str(); }
111 operator const std::string & () const { return m_value; }
112 operator std::string & () { return m_value; }
113 operator const std::string * () { return &m_value; }
114 operator std::string * () { return &m_value; }
116 StringValue & operator = (const StringValue & other) { m_value = other.m_value; return *this; }
117 StringValue & operator = (const std::string & value) { m_value = value; return *this; }
118 StringValue & operator = (const char * value) { m_value = value; return *this; }
125 * \brief Binary object value of arbitrary size.
127 class BinaryValue : public Value, public Blob
130 BinaryValue() : Value(), Blob() {}
132 virtual std::string getTypeName() const { return "binary"; }
133 virtual size_t getSize() const { return getLength(); }
136 }; // namespace elftosb