]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - tools/elftosb/common/Value.h
merged tx6dl-devel into denx master branch
[karo-tx-uboot.git] / tools / 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 {
17
18 /*!
19  * \brief Abstract base class for values of arbitrary types.
20  */
21 class Value
22 {
23 public:
24         Value() {}
25         virtual ~Value() {}
26         
27         virtual std::string getTypeName() const = 0;
28         virtual size_t getSize() const = 0;
29 };
30
31 /*!
32  * \brief 32-bit signed integer value.
33  */
34 class IntegerValue : public Value
35 {
36 public:
37         IntegerValue() : m_value(0) {}
38         IntegerValue(uint32_t value) : m_value(value) {}
39         IntegerValue(const IntegerValue & other) : m_value(other.m_value) {}
40         
41         virtual std::string getTypeName() const { return "integer"; }
42         virtual size_t getSize() const { return sizeof(m_value); }
43         
44         inline uint32_t getValue() const { return m_value; }
45         
46         inline operator uint32_t () const { return m_value; }
47         
48         inline IntegerValue & operator = (uint32_t value) { m_value = value; return *this; }
49
50 protected:
51         uint32_t m_value;       //!< The integer value.
52 };
53
54 /*!
55  * \brief Adds a word size attribute to IntegerValue.
56  *
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.
61  *
62  * The word size defaults to a 32-bit word.
63  */
64 class SizedIntegerValue : public IntegerValue
65 {
66 public:
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) {}
72         
73         virtual std::string getTypeName() const { return "sized integer"; }
74         virtual size_t getSize() const;
75         
76         inline int_size_t getWordSize() const { return m_size; }
77         inline void setWordSize(int_size_t size) { m_size = size; }
78         
79         //! \brief Returns a 32-bit mask value dependant on the word size attribute.
80         uint32_t getWordSizeMask() const;
81         
82         //! \name Assignment operators
83         //! These operators set the word size as well as the integer value.
84         //@{
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; }
88         //@}
89         
90 protected:
91         int_size_t m_size;      //!< Size of the integer.
92 };
93
94 /*!
95  * \brief String value.
96  *
97  * Simply wraps the STL std::string class.
98  */
99 class StringValue : public Value
100 {
101 public:
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) {}
106         
107         virtual std::string getTypeName() const { return "string"; }
108         virtual size_t getSize() const { return m_value.size(); }
109         
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; }
115         
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; }
119         
120 protected:
121         std::string m_value;
122 };
123
124 /*!
125  * \brief Binary object value of arbitrary size.
126  */
127 class BinaryValue : public Value, public Blob
128 {
129 public:
130         BinaryValue() : Value(), Blob() {}
131         
132         virtual std::string getTypeName() const { return "binary"; }
133         virtual size_t getSize() const { return getLength(); }
134 };
135
136 }; // namespace elftosb
137
138 #endif // _Value_h_