+ // reference-counted data
+ struct Data
+ {
+ enum Kind
+ {
+ Owned,
+ NonOwned
+ };
+
+ Data(CharType *str, Kind kind = Owned)
+ : m_str(str), m_ref(1), m_owned(kind == Owned) {}
+
+ ~Data()
+ {
+ if ( m_owned )
+ free(m_str);
+ }
+
+ CharType *m_str;
+
+ // "short" to have sizeof(Data)=8 on 32bit archs
+ unsigned short m_ref;
+
+ bool m_owned;
+ };
+
+ // placeholder for NULL string, to simplify this code
+ // NB: this is defined in string.cpp, not (non-existent) buffer.cpp
+#ifdef __MINGW32__
+ // MinGW requires explicit WXDLLIMPEXP_DATA_BASE to avoid compilation
+ // errors
+ static WXDLLIMPEXP_DATA_BASE(Data) NullData;
+#else
+ // but Visual C++ doesn't like it
+ static Data NullData;
+#endif
+
+ void IncRef()
+ {
+ if ( m_data == &NullData ) // exception, not ref-counted
+ return;
+ m_data->m_ref++;
+ }
+
+ void DecRef()
+ {
+ if ( m_data == &NullData ) // exception, not ref-counted
+ return;
+ if ( --m_data->m_ref == 0 )
+ delete m_data;
+ m_data = &NullData;
+ }
+
+private:
+ Data *m_data;