1 /* 2 * Copyright 2009,2011, Haiku, Inc. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _PACKAGE__HPKG__PACKAGE_ATTRIBUTE_VALUE_H_ 6 #define _PACKAGE__HPKG__PACKAGE_ATTRIBUTE_VALUE_H_ 7 8 9 #include <string.h> 10 11 #include <package/hpkg/HPKGDefs.h> 12 13 14 namespace BPackageKit { 15 16 namespace BHPKG { 17 18 19 struct BPackageAttributeValue { 20 union { 21 int64 signedInt; 22 uint64 unsignedInt; 23 const char* string; 24 struct { 25 uint64 size; 26 union { 27 uint64 offset; 28 uint8 raw[B_HPKG_MAX_INLINE_DATA_SIZE]; 29 }; 30 } data; 31 }; 32 uint8 type; 33 uint8 encoding; 34 35 public: 36 inline BPackageAttributeValue(); 37 38 inline void SetTo(int8 value); 39 inline void SetTo(uint8 value); 40 inline void SetTo(int16 value); 41 inline void SetTo(uint16 value); 42 inline void SetTo(int32 value); 43 inline void SetTo(uint32 value); 44 inline void SetTo(int64 value); 45 inline void SetTo(uint64 value); 46 inline void SetTo(const char* value); 47 inline void SetToData(uint64 size, uint64 offset); 48 inline void SetToData(uint64 size, const void* rawData); 49 }; 50 51 52 BPackageAttributeValue::BPackageAttributeValue() 53 : 54 type(B_HPKG_ATTRIBUTE_TYPE_INVALID) 55 { 56 } 57 58 59 void 60 BPackageAttributeValue::SetTo(int8 value) 61 { 62 signedInt = value; 63 type = B_HPKG_ATTRIBUTE_TYPE_INT; 64 } 65 66 67 void 68 BPackageAttributeValue::SetTo(uint8 value) 69 { 70 unsignedInt = value; 71 type = B_HPKG_ATTRIBUTE_TYPE_UINT; 72 } 73 74 75 void 76 BPackageAttributeValue::SetTo(int16 value) 77 { 78 signedInt = value; 79 type = B_HPKG_ATTRIBUTE_TYPE_INT; 80 } 81 82 83 void 84 BPackageAttributeValue::SetTo(uint16 value) 85 { 86 unsignedInt = value; 87 type = B_HPKG_ATTRIBUTE_TYPE_UINT; 88 } 89 90 91 void 92 BPackageAttributeValue::SetTo(int32 value) 93 { 94 signedInt = value; 95 type = B_HPKG_ATTRIBUTE_TYPE_INT; 96 } 97 98 99 void 100 BPackageAttributeValue::SetTo(uint32 value) 101 { 102 unsignedInt = value; 103 type = B_HPKG_ATTRIBUTE_TYPE_UINT; 104 } 105 106 107 void 108 BPackageAttributeValue::SetTo(int64 value) 109 { 110 signedInt = value; 111 type = B_HPKG_ATTRIBUTE_TYPE_INT; 112 } 113 114 115 void 116 BPackageAttributeValue::SetTo(uint64 value) 117 { 118 unsignedInt = value; 119 type = B_HPKG_ATTRIBUTE_TYPE_UINT; 120 } 121 122 123 void 124 BPackageAttributeValue::SetTo(const char* value) 125 { 126 string = value; 127 type = B_HPKG_ATTRIBUTE_TYPE_STRING; 128 } 129 130 131 void 132 BPackageAttributeValue::SetToData(uint64 size, uint64 offset) 133 { 134 data.size = size; 135 data.offset = offset; 136 type = B_HPKG_ATTRIBUTE_TYPE_RAW; 137 encoding = B_HPKG_ATTRIBUTE_ENCODING_RAW_HEAP; 138 } 139 140 141 void 142 BPackageAttributeValue::SetToData(uint64 size, const void* rawData) 143 { 144 data.size = size; 145 if (size > 0) 146 memcpy(data.raw, rawData, size); 147 type = B_HPKG_ATTRIBUTE_TYPE_RAW; 148 encoding = B_HPKG_ATTRIBUTE_ENCODING_RAW_INLINE; 149 } 150 151 152 } // namespace BHPKG 153 154 } // namespace BPackageKit 155 156 157 #endif // _PACKAGE__HPKG__PACKAGE_ATTRIBUTE_VALUE_H_ 158