1 /* 2 * Copyright 2011, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _SUPPORT_BSTRING_PRIVATE_H_ 6 #define _SUPPORT_BSTRING_PRIVATE_H_ 7 8 9 #include <stdlib.h> 10 11 #include <String.h> 12 13 14 class BString::Private { 15 public: 16 static const uint32 kPrivateDataOffset = 2 * sizeof(int32); 17 18 public: 19 Private(const BString& string) 20 : 21 fString(string) 22 { 23 } 24 25 char* Data() 26 { 27 return fString.fPrivateData; 28 } 29 30 static vint32& DataRefCount(char* data) 31 { 32 return *(((int32 *)data) - 2); 33 } 34 35 vint32& DataRefCount() 36 { 37 return DataRefCount(Data()); 38 } 39 40 static int32& DataLength(char* data) 41 { 42 return *(((int32*)data) - 1); 43 } 44 45 int32& DataLength() 46 { 47 return DataLength(Data()); 48 } 49 50 static void IncrementDataRefCount(char* data) 51 { 52 if (data != NULL) 53 atomic_add(&DataRefCount(data), 1); 54 } 55 56 static void DecrementDataRefCount(char* data) 57 { 58 if (data != NULL) { 59 if (atomic_add(&DataRefCount(data), -1) == 1) 60 free(data - kPrivateDataOffset); 61 } 62 } 63 64 static BString StringFromData(char* data) 65 { 66 return BString(data, BString::PRIVATE_DATA); 67 } 68 69 private: 70 const BString& fString; 71 }; 72 73 74 #endif // _SUPPORT_BSTRING_PRIVATE_H_ 75 76