+ void DecRef()
+ {
+ if ( m_data == GetNullData() ) // exception, not ref-counted
+ return;
+ if ( --m_data->m_ref == 0 )
+ delete m_data;
+ m_data = GetNullData();
+ }
+
+ // sets this object to a be copy of 'other'; if 'src' is non-owned,
+ // a deep copy is made and 'this' will contain new instance of the data
+ void MakeOwnedCopyOf(const wxScopedCharTypeBuffer& src)
+ {
+ this->DecRef();
+
+ if ( src.m_data == this->GetNullData() )
+ {
+ this->m_data = this->GetNullData();
+ }
+ else if ( src.m_data->m_owned )
+ {
+ this->m_data = src.m_data;
+ this->IncRef();
+ }
+ else
+ {
+ // if the scoped buffer had non-owned data, we have to make
+ // a copy here, because src.m_data->m_str is valid only for as long
+ // as 'src' exists
+ this->m_data = new Data
+ (
+ StrCopy(src.data(), src.length()),
+ src.length()
+ );
+ }
+ }
+
+ static CharType *StrCopy(const CharType *src, size_t len)
+ {
+ CharType *dst = (CharType*)malloc(sizeof(CharType) * (len + 1));
+ memcpy(dst, src, sizeof(CharType) * (len + 1));
+ return dst;
+ }
+
+protected:
+ Data *m_data;
+};
+
+typedef wxScopedCharTypeBuffer<char> wxScopedCharBuffer;
+typedef wxScopedCharTypeBuffer<wchar_t> wxScopedWCharBuffer;
+
+
+// this buffer class always stores data in "owned" (persistent) manner
+template <typename T>
+class wxCharTypeBuffer : public wxScopedCharTypeBuffer<T>
+{
+protected:
+ typedef typename wxScopedCharTypeBuffer<T>::Data Data;
+
+public:
+ typedef T CharType;
+
+ wxCharTypeBuffer(const CharType *str = NULL, size_t len = wxNO_LEN)
+ {
+ if ( str )
+ {
+ if ( len == wxNO_LEN )
+ len = wxStrlen(str);
+ this->m_data = new Data(StrCopy(str, len), len);
+ }
+ else
+ {
+ this->m_data = this->GetNullData();
+ }
+ }
+
+ wxCharTypeBuffer(size_t len)
+ {
+ this->m_data =
+ new Data((CharType *)malloc((len + 1)*sizeof(CharType)), len);
+ this->m_data->Get()[len] = (CharType)0;
+ }
+
+ wxCharTypeBuffer(const wxCharTypeBuffer& src)
+ : wxScopedCharTypeBuffer<T>(src) {}
+