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