-#define DEFINE_BUFFER(classname, chartype, strdupfunc) \
-class classname \
-{ \
-public: \
- classname(const chartype *str) \
- : m_str(str ? strdupfunc(str) : NULL) \
- { \
- } \
- \
- classname(size_t len) \
- : 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, strdup);