ELFAttributeValue.h revision 87f34658dec9097d987d254a990ea7f311bfc95f
1//===- ELFAttributeValue.h ------------------------------------------------===// 2// 3// The MCLinker Project 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9#ifndef MCLD_TARGET_ELFATTRIBUTEVALUE_H 10#define MCLD_TARGET_ELFATTRIBUTEVALUE_H 11#ifdef ENABLE_UNITTEST 12#include <gtest.h> 13#endif 14 15#include <string> 16 17namespace mcld 18{ 19 20/** \class ELFAttributeValue 21 * \brief ELFAttributeValue stroes the value of an attribute tag. The attribtue 22 * tag itself is not stored in this object. 23 */ 24class ELFAttributeValue 25{ 26public: 27 // Type of value that an attribute tag holds. 28 enum Type { 29 // The value contains no data and has unknown type. 30 Uninitialized = 0, 31 32 // The value contains integer data. 33 Int = 1L << 0, 34 35 // The value contains string data. 36 String = 1L << 1, 37 38 // This is for attribute in which "default value" (0 for int type and empty 39 // string for string type) has special meaning for them. That is, the 40 // default value is "disabled" and meaningful for those attribute. 41 NoDefault = 1L << 2, 42 }; 43 44public: 45 ELFAttributeValue() 46 : m_Type(Uninitialized), m_IntValue(0), m_StringValue() { } 47 48 ~ELFAttributeValue() { } 49 50public: 51 unsigned int type() const 52 { return m_Type; } 53 54 void setType(unsigned int pType) 55 { m_Type = pType; } 56 57 unsigned int getIntValue() const 58 { return m_IntValue; } 59 60 void setIntValue(unsigned int pIntValue) 61 { m_IntValue = pIntValue; } 62 63 const std::string &getStringValue() const 64 { return m_StringValue; } 65 66 void setStringValue(const std::string &pStringValue) 67 { m_StringValue = pStringValue; } 68 69 void setStringValue(const char *pStringValue, size_t pSize) 70 { m_StringValue.assign(pStringValue, pSize); } 71 72 void setStringValue(const char *pStringValue) 73 { m_StringValue.assign(pStringValue); } 74 75 size_t getSize() const; 76 77 inline bool isUninitialized() const 78 { return (m_Type == Uninitialized); } 79 80 inline bool isInitialized() const 81 { return !isUninitialized(); } 82 83 inline bool isIntValue() const 84 { return (m_Type & Int); } 85 86 inline bool isStringValue() const 87 { return (m_Type & String); } 88 89 inline bool hasNoDefault() const 90 { return (m_Type & NoDefault); } 91 92 bool isDefaultValue() const; 93 94 // Returns true if this attribute value should be emitted to the output. 95 inline bool shouldEmit() const { 96 // Attribute with non-default value should be emitted. 97 return !isDefaultValue(); 98 } 99 100 bool equals(const ELFAttributeValue& pValue) const; 101 102 bool operator==(const ELFAttributeValue& pValue) const 103 { return equals(pValue); } 104 bool operator!=(const ELFAttributeValue& pValue) const 105 { return !equals(pValue); } 106 107 /// reset - reset this value to the uninitialized state 108 void reset() 109 { 110 m_Type = Uninitialized; 111 m_IntValue = 0; 112 m_StringValue.clear(); 113 return; 114 } 115 116private: 117 unsigned int m_Type; 118 119 unsigned int m_IntValue; 120 std::string m_StringValue; 121}; 122 123} // namespace of mcld 124 125#endif 126