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 bool IsShareable() const 31 { 32 return fString._IsShareable(); 33 } 34 35 static int32& DataRefCount(char* data) 36 { 37 return *(((int32 *)data) - 2); 38 } 39 40 int32& DataRefCount() 41 { 42 return DataRefCount(Data()); 43 } 44 45 static int32& DataLength(char* data) 46 { 47 return *(((int32*)data) - 1); 48 } 49 50 int32& DataLength() 51 { 52 return DataLength(Data()); 53 } 54 55 static void IncrementDataRefCount(char* data) 56 { 57 if (data != NULL) 58 atomic_add(&DataRefCount(data), 1); 59 } 60 61 static void DecrementDataRefCount(char* data) 62 { 63 if (data != NULL) { 64 if (atomic_add(&DataRefCount(data), -1) == 1) 65 free(data - kPrivateDataOffset); 66 } 67 } 68 69 static BString StringFromData(char* data) 70 { 71 return BString(data, BString::PRIVATE_DATA); 72 } 73 74 private: 75 const BString& fString; 76 }; 77 78 79 #endif // _SUPPORT_BSTRING_PRIVATE_H_ 80 81