BString::Private class to access BString internals
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
#include <../private/support/StringPrivate.h>
|
||||||
@@ -321,9 +321,20 @@ public:
|
|||||||
BString& operator<<(float value);
|
BString& operator<<(float value);
|
||||||
BString& operator<<(double value);
|
BString& operator<<(double value);
|
||||||
|
|
||||||
|
public:
|
||||||
|
class Private;
|
||||||
|
friend class Private;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
class PosVect;
|
class PosVect;
|
||||||
friend class BStringRef;
|
friend class BStringRef;
|
||||||
|
|
||||||
|
enum PrivateDataTag {
|
||||||
|
PRIVATE_DATA
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
BString(char* privateData, PrivateDataTag tag);
|
||||||
|
|
||||||
// Management
|
// Management
|
||||||
status_t _MakeWritable();
|
status_t _MakeWritable();
|
||||||
|
|||||||
@@ -0,0 +1,75 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2011, Ingo Weinhold, [email protected].
|
||||||
|
* Distributed under the terms of the MIT License.
|
||||||
|
*/
|
||||||
|
#ifndef _SUPPORT_BSTRING_PRIVATE_H_
|
||||||
|
#define _SUPPORT_BSTRING_PRIVATE_H_
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include <String.h>
|
||||||
|
|
||||||
|
|
||||||
|
class BString::Private {
|
||||||
|
public:
|
||||||
|
static const uint32 kPrivateDataOffset = 2 * sizeof(int32);
|
||||||
|
|
||||||
|
public:
|
||||||
|
Private(const BString& string)
|
||||||
|
:
|
||||||
|
fString(string)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
char* Data()
|
||||||
|
{
|
||||||
|
return fString.fPrivateData;
|
||||||
|
}
|
||||||
|
|
||||||
|
static vint32& DataRefCount(char* data)
|
||||||
|
{
|
||||||
|
return *(((int32 *)data) - 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
vint32& DataRefCount()
|
||||||
|
{
|
||||||
|
return DataRefCount(Data());
|
||||||
|
}
|
||||||
|
|
||||||
|
static int32& DataLength(char* data)
|
||||||
|
{
|
||||||
|
return *(((int32*)data) - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int32& DataLength()
|
||||||
|
{
|
||||||
|
return DataLength(Data());
|
||||||
|
}
|
||||||
|
|
||||||
|
static void IncrementDataRefCount(char* data)
|
||||||
|
{
|
||||||
|
if (data != NULL)
|
||||||
|
atomic_add(&DataRefCount(data), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void DecrementDataRefCount(char* data)
|
||||||
|
{
|
||||||
|
if (data != NULL) {
|
||||||
|
if (atomic_add(&DataRefCount(data), -1) == 1)
|
||||||
|
free(data - kPrivateDataOffset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static BString StringFromData(char* data)
|
||||||
|
{
|
||||||
|
return BString(data, BString::PRIVATE_DATA);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
const BString& fString;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif // _SUPPORT_BSTRING_PRIVATE_H_
|
||||||
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
SubDir HAIKU_TOP src build libbe support ;
|
SubDir HAIKU_TOP src build libbe support ;
|
||||||
|
|
||||||
UsePrivateBuildHeaders app interface shared ;
|
UsePrivateBuildHeaders app interface shared support ;
|
||||||
|
|
||||||
USES_BE_API on <libbe_build>support_kit.o = true ;
|
USES_BE_API on <libbe_build>support_kit.o = true ;
|
||||||
|
|
||||||
|
|||||||
+16
-20
@@ -23,6 +23,7 @@
|
|||||||
|
|
||||||
#include <Debug.h>
|
#include <Debug.h>
|
||||||
|
|
||||||
|
#include <StringPrivate.h>
|
||||||
#include <utf8_functions.h>
|
#include <utf8_functions.h>
|
||||||
|
|
||||||
|
|
||||||
@@ -34,7 +35,7 @@
|
|||||||
#define REPLACE_ALL 0x7FFFFFFF
|
#define REPLACE_ALL 0x7FFFFFFF
|
||||||
|
|
||||||
|
|
||||||
const uint32 kPrivateDataOffset = 2 * sizeof(int32);
|
static const uint32 kPrivateDataOffset = BString::Private::kPrivateDataOffset;
|
||||||
|
|
||||||
const char* B_EMPTY_STRING = "";
|
const char* B_EMPTY_STRING = "";
|
||||||
|
|
||||||
@@ -79,20 +80,6 @@ safestr(const char* str)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static inline vint32&
|
|
||||||
data_reference_count(char* data)
|
|
||||||
{
|
|
||||||
return *(((int32 *)data) - 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static inline int32&
|
|
||||||
data_length(char* data)
|
|
||||||
{
|
|
||||||
return *(((int32*)data) - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// #pragma mark - PosVect
|
// #pragma mark - PosVect
|
||||||
|
|
||||||
|
|
||||||
@@ -202,14 +189,14 @@ BStringRef::operator&()
|
|||||||
inline vint32&
|
inline vint32&
|
||||||
BString::_ReferenceCount()
|
BString::_ReferenceCount()
|
||||||
{
|
{
|
||||||
return data_reference_count(fPrivateData);
|
return Private::DataRefCount(fPrivateData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inline const vint32&
|
inline const vint32&
|
||||||
BString::_ReferenceCount() const
|
BString::_ReferenceCount() const
|
||||||
{
|
{
|
||||||
return data_reference_count(fPrivateData);
|
return Private::DataRefCount(fPrivateData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -2132,6 +2119,15 @@ BString::operator<<(double value)
|
|||||||
// #pragma mark - Private or reserved
|
// #pragma mark - Private or reserved
|
||||||
|
|
||||||
|
|
||||||
|
BString::BString(char* privateData, PrivateDataTag tag)
|
||||||
|
:
|
||||||
|
fPrivateData(privateData)
|
||||||
|
{
|
||||||
|
if (fPrivateData != NULL)
|
||||||
|
atomic_add(&_ReferenceCount(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*! Detaches this string from an eventually shared fPrivateData, ie. this makes
|
/*! Detaches this string from an eventually shared fPrivateData, ie. this makes
|
||||||
this string writable.
|
this string writable.
|
||||||
*/
|
*/
|
||||||
@@ -2210,8 +2206,8 @@ BString::_Allocate(int32 length)
|
|||||||
newData[length] = '\0';
|
newData[length] = '\0';
|
||||||
|
|
||||||
// initialize reference count & length
|
// initialize reference count & length
|
||||||
data_reference_count(newData) = 1;
|
Private::DataRefCount(newData) = 1;
|
||||||
data_length(newData) = length & 0x7fffffff;
|
Private::DataLength(newData) = length & 0x7fffffff;
|
||||||
|
|
||||||
return newData;
|
return newData;
|
||||||
}
|
}
|
||||||
@@ -2303,7 +2299,7 @@ BString::_ShrinkAtBy(int32 offset, int32 length)
|
|||||||
void
|
void
|
||||||
BString::_SetLength(int32 length)
|
BString::_SetLength(int32 length)
|
||||||
{
|
{
|
||||||
data_length(fPrivateData) = length & 0x7fffffff;
|
Private::DataLength(fPrivateData) = length & 0x7fffffff;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user