summaryrefslogtreecommitdiffstats
path: root/common/Value.h
blob: 8a676d5570d699e9b0cdce994d60f0a977b25c83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * File:	Value.h
 *
 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
 * See included license file for license details.
 */
#if !defined(_Value_h_)
#define _Value_h_

#include "stdafx.h"
#include <string>
#include "int_size.h"
#include "Blob.h"

namespace elftosb
{

/*!
 * \brief Abstract base class for values of arbitrary types.
 */
class Value
{
public:
	Value() {}
	virtual ~Value() {}
	
	virtual std::string getTypeName() const = 0;
	virtual size_t getSize() const = 0;
};

/*!
 * \brief 32-bit signed integer value.
 */
class IntegerValue : public Value
{
public:
	IntegerValue() : m_value(0) {}
	IntegerValue(uint32_t value) : m_value(value) {}
	IntegerValue(const IntegerValue & other) : m_value(other.m_value) {}
	
	virtual std::string getTypeName() const { return "integer"; }
	virtual size_t getSize() const { return sizeof(m_value); }
	
	inline uint32_t getValue() const { return m_value; }
	
	inline operator uint32_t () const { return m_value; }
	
	inline IntegerValue & operator = (uint32_t value) { m_value = value; return *this; }

protected:
	uint32_t m_value;	//!< The integer value.
};

/*!
 * \brief Adds a word size attribute to IntegerValue.
 *
 * The word size really only acts as an attribute that is carried along
 * with the integer value. It doesn't affect the actual value at all.
 * However, you can use the getWordSizeMask() method to mask off bits
 * that should not be there.
 *
 * The word size defaults to a 32-bit word.
 */
class SizedIntegerValue : public IntegerValue
{
public:
	SizedIntegerValue() : IntegerValue(), m_size(kWordSize) {}
	SizedIntegerValue(uint32_t value, int_size_t size=kWordSize) : IntegerValue(value), m_size(size) {}
	SizedIntegerValue(uint16_t value) : IntegerValue(value), m_size(kHalfWordSize) {}
	SizedIntegerValue(uint8_t value) : IntegerValue(value), m_size(kByteSize) {}
	SizedIntegerValue(const SizedIntegerValue & other) : IntegerValue(other), m_size(other.m_size) {}
	
	virtual std::string getTypeName() const { return "sized integer"; }
	virtual size_t getSize() const;
	
	inline int_size_t getWordSize() const { return m_size; }
	inline void setWordSize(int_size_t size) { m_size = size; }
	
	//! \brief Returns a 32-bit mask value dependant on the word size attribute.
	uint32_t getWordSizeMask() const;
	
	//! \name Assignment operators
	//! These operators set the word size as well as the integer value.
	//@{
	SizedIntegerValue & operator = (uint8_t value) { m_value = value; m_size = kByteSize; return *this; }
	SizedIntegerValue & operator = (uint16_t value) { m_value = value; m_size = kHalfWordSize; return *this; }
	SizedIntegerValue & operator = (uint32_t value) { m_value = value; m_size = kWordSize; return *this; }
	//@}
	
protected:
	int_size_t m_size;	//!< Size of the integer.
};

/*!
 * \brief String value.
 *
 * Simply wraps the STL std::string class.
 */
class StringValue : public Value
{
public:
	StringValue() : m_value() {}
	StringValue(const std::string & value) : m_value(value) {}
	StringValue(const std::string * value) : m_value(*value) {}
	StringValue(const StringValue & other) : m_value(other.m_value) {}
	
	virtual std::string getTypeName() const { return "string"; }
	virtual size_t getSize() const { return m_value.size(); }
	
	operator const char * () const { return m_value.c_str(); }
	operator const std::string & () const { return m_value; }
	operator std::string & () { return m_value; }
	operator const std::string * () { return &m_value; }
	operator std::string * () { return &m_value; }
	
	StringValue & operator = (const StringValue & other) { m_value = other.m_value; return *this; }
	StringValue & operator = (const std::string & value) { m_value = value; return *this; }
	StringValue & operator = (const char * value) { m_value = value; return *this; }
	
protected:
	std::string m_value;
};

/*!
 * \brief Binary object value of arbitrary size.
 */
class BinaryValue : public Value, public Blob
{
public:
	BinaryValue() : Value(), Blob() {}
	
	virtual std::string getTypeName() const { return "binary"; }
	virtual size_t getSize() const { return getLength(); }
};

}; // namespace elftosb

#endif // _Value_h_