-class wxCharBuffer
-{
-public:
- wxCharBuffer(const char *str)
- : m_str(str ? strdup(str) : NULL)
- {
- wxASSERT_MSG( str, wxT("NULL string in wxCharBuffer") );
- }
-
- wxCharBuffer(size_t len)
- : m_str((char *)malloc((len + 1)*sizeof(char)))
- {
- m_str[len] = '\0';
- }
-
- // no need to check for NULL, free() does it
- ~wxCharBuffer() { free(m_str); }
-
- wxCharBuffer(const wxCharBuffer& src)
- : m_str(src.m_str)
- {
- // no reference count yet...
- ((wxCharBuffer*)&src)->m_str = (char *)NULL;
- }
- wxCharBuffer& operator=(const wxCharBuffer& src)
- {
- m_str = src.m_str;
- // no reference count yet...
- ((wxCharBuffer*)&src)->m_str = (char *)NULL;
- return *this;
- }
-
- const char *data() const { return m_str; }
- operator const char *() const { return m_str; }
- char operator[](size_t n) const { return m_str[n]; }
-
-private:
- char *m_str;
-};
+#define DEFINE_BUFFER(classname, chartype, strdupfunc) \
+class WXDLLIMPEXP_BASE classname \
+{ \
+public: \
+ classname(const chartype *str) \
+ : m_str(str ? strdupfunc(str) : NULL) \
+ { \
+ } \
+ \
+ classname(size_t len=0) \
+ : m_str((chartype *)malloc((len + 1)*sizeof(chartype))) \
+ { \
+ m_str[len] = (chartype)0; \
+ } \
+ \
+ /* no need to check for NULL, free() does it */ \
+ ~classname() { free(m_str); } \
+ \
+ /* \
+ WARNING: \
+ \
+ the copy ctor and assignment operators change the passed in object \
+ even although it is declared as "const", so: \
+ \
+ a) it shouldn't be really const \
+ b) you shouldn't use it afterwards (or know that it was reset) \
+ \
+ This is very ugly but is unfortunately needed to make the normal use\
+ of classname buffer objects possible and is very similar to what \
+ std::auto_ptr<> does (as if it were an excuse...) \
+ */ \
+ \
+ /* \
+ because of the remark above, release() is declared const even if it \
+ isn't really const \
+ */ \
+ chartype *release() const \
+ { \
+ chartype *p = m_str; \
+ ((classname *)this)->m_str = NULL; \
+ return p; \
+ } \
+ \
+ classname(const classname& src) \
+ : m_str(src.release()) \
+ { \
+ } \
+ \
+ classname& operator=(const chartype *str) \
+ { \
+ free(m_str); \
+ m_str = str ? strdupfunc(str) : NULL; \
+ return *this; \
+ } \
+ \
+ classname& operator=(const classname& src) \
+ { \
+ free(m_str); \
+ m_str = src.release(); \
+ \
+ return *this; \
+ } \
+ \
+ chartype *data() { return m_str; } \
+ const chartype *data() const { return m_str; } \
+ operator const chartype *() const { return m_str; } \
+ chartype operator[](size_t n) const { return m_str[n]; } \
+ \
+private: \
+ chartype *m_str; \
+}
+
+DEFINE_BUFFER(wxCharBuffer, char, wxStrdupA);