Author: wyoung
Date: Thu Nov 15 17:30:02 2007
New Revision: 1854

URL: http://svn.gna.org/viewcvs/mysqlpp?rev=1854&view=rev
Log:
Added RefCountedBuffer::assign() methods, to allow replacing contents of
the internal buffer.  Doesn't change reference counts.

Modified:
    trunk/lib/refcounted.cpp
    trunk/lib/refcounted.h

Modified: trunk/lib/refcounted.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/refcounted.cpp?rev=1854&r1=1853&r2=1854&view=diff
==============================================================================
--- trunk/lib/refcounted.cpp (original)
+++ trunk/lib/refcounted.cpp Thu Nov 15 17:30:02 2007
@@ -28,30 +28,58 @@
 namespace mysqlpp {
 
 
+RefCountedBuffer&
+RefCountedBuffer::assign(const char* data, size_type length,
+               unsigned char type, bool is_null)
+{
+       replace_buffer(data, length);
+       type_ = type;
+       is_null_ = is_null;
+       return *this;
+}
+
+RefCountedBuffer& 
+RefCountedBuffer::assign(const std::string& s, unsigned char type,
+               bool is_null)
+{
+       replace_buffer(s.data(), s.length());
+       type_ = type;
+       is_null_ = is_null;
+       return *this;
+}
+
 void
 RefCountedBuffer::init(const char* pd, size_type length,
                mysql_type_info type, bool is_null)
 {
+       refs_ = 1;
        type_ = type;
        is_null_ = is_null;
-       refs_ = 1;
+
+       replace_buffer(pd, length);
+}
+
+void
+RefCountedBuffer::replace_buffer(const char* pd, size_type length)
+{
+       if (data_ || !pd) {
+               delete[] data_;
+               data_ = 0;
+               length_ = 0;
+       }
 
        if (pd) {
                // The casts for the data member are because the C type system
-               // can't distinguish initialization from modification.  We don't
-               // care to enforce constness on this buffer until after this
-               // function returns.  The cast for pd is just because memcpy()
-               // doesn't declare its second parameter const for historical
-               // reasons, not because it actually does modify it.
+               // can't distinguish initialization from modification when it
+               // happens in 2 steps like this.
+               // 
+               // We cast away const for pd in case we're on a system that uses
+               // the old definition of memcpy() with non-const 2nd parameter.
                data_ = new char[length + 1];
                length_ = length;
                memcpy(const_cast<char*>(data_), const_cast<char*>(pd), 
length_);
                const_cast<char*>(data_)[length_] = '\0';
        }
-       else {
-               data_ = 0;
-               length_ = 0;
-       }
 }
 
 } // end namespace mysqlpp

Modified: trunk/lib/refcounted.h
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/refcounted.h?rev=1854&r1=1853&r2=1854&view=diff
==============================================================================
--- trunk/lib/refcounted.h (original)
+++ trunk/lib/refcounted.h Thu Nov 15 17:30:02 2007
@@ -230,6 +230,16 @@
        /// \brief Destructor
        ~RefCountedBuffer() { delete[] data_; }
 
+       /// \brief Replace contents of buffer with copy of given C string
+       RefCountedBuffer& assign(const char* data, size_type length,
+                       unsigned char type = mysql_type_info::string_type,
+                       bool is_null = false);
+
+       /// \brief Replace contents of buffer with copy of given C++ string
+       RefCountedBuffer& assign(const std::string& s,
+                       unsigned char type = mysql_type_info::string_type,
+                       bool is_null = false);
+
        /// \brief Return pointer to raw data buffer
        const char* data() const { return data_; }
 
@@ -266,8 +276,11 @@
        bool detach() { return --refs_ > 0; }
 
 private:
+       /// \brief Common initialization for ctors
        void init(const char* pd, size_type len, mysql_type_info type,
-                       bool is_null);  ///< common initialization for ctors
+                       bool is_null);
+       /// \brief Implementation detail of assign() and init()
+       void replace_buffer(const char* pd, size_type length);
 
        const char* data_;              ///< pointer to the raw data buffer
        size_type length_;              ///< bytes in buffer, without trailing 
null


_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits

Reply via email to