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