Author: wyoung
Date: Thu Nov 8 03:58:29 2007
New Revision: 1833
URL: http://svn.gna.org/viewcvs/mysqlpp?rev=1833&view=rev
Log:
- Extracted ColData::Buffer class as new top-level class, calling it
RefCountedBuffer, defining it in the same module as RefCountedPointer.
Interface changed slightly so it doesn't have to declare ColData as
its friend.
- Put RefCountedPointer in namespace mysqlpp. Should have been done
when it was created. Oops. :)
Modified:
trunk/lib/coldata.cpp
trunk/lib/coldata.h
trunk/lib/refcounted.h
trunk/mysql++.bkl
Modified: trunk/lib/coldata.cpp
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/coldata.cpp?rev=1833&r1=1832&r2=1833&view=diff
==============================================================================
--- trunk/lib/coldata.cpp (original)
+++ trunk/lib/coldata.cpp Thu Nov 8 03:58:29 2007
@@ -141,10 +141,10 @@
ColData::it_is_null()
{
if (buffer_) {
- buffer_->is_null_ = true;
+ buffer_->set_null();
}
else {
- buffer_ = new Buffer(0, 0, mysql_type_info::string_type, true);
+ buffer_ = new RefCountedBuffer(0, 0,
mysql_type_info::string_type, true);
}
}
@@ -182,31 +182,4 @@
}
-ColData::Buffer::Buffer(const char* pd, ColData::size_type length,
- mysql_type_info type, bool is_null) :
-data_(pd ? new char[length + 1] : 0),
-length_(pd ? length : 0),
-type_(type),
-is_null_(is_null),
-refs_(1)
-{
- if (data_) {
- // 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
- // ctor 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.
- memcpy(const_cast<char*>(data_), const_cast<char*>(pd),
length_);
- const_cast<char*>(data_)[length_] = '\0';
- }
-}
-
-
-ColData::Buffer::~Buffer()
-{
- delete[] data_;
-}
-
-
} // end namespace mysqlpp
Modified: trunk/lib/coldata.h
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/coldata.h?rev=1833&r1=1832&r2=1833&view=diff
==============================================================================
--- trunk/lib/coldata.h (original)
+++ trunk/lib/coldata.h Thu Nov 8 03:58:29 2007
@@ -38,8 +38,8 @@
#include "datetime.h"
#include "exceptions.h"
#include "null.h"
+#include "refcounted.h"
#include "string_util.h"
-#include "type_info.h"
#include <string>
@@ -117,7 +117,7 @@
ColData(const ColData& cd) :
buffer_(cd.buffer_)
{
- ++buffer_->refs_;
+ buffer_->attach();
}
/// \brief Full constructor.
@@ -135,7 +135,7 @@
explicit ColData(const char* str, size_type len,
mysql_type_info type = mysql_type_info::string_type,
bool is_null = false) :
- buffer_(new Buffer(str, len, type, is_null))
+ buffer_(new RefCountedBuffer(str, len, type, is_null))
{
}
@@ -149,7 +149,7 @@
explicit ColData(const std::string& str,
mysql_type_info type = mysql_type_info::string_type,
bool is_null = false) :
- buffer_(new Buffer(str.data(), str.length(), type, is_null))
+ buffer_(new RefCountedBuffer(str.data(), str.length(), type, is_null))
{
}
@@ -163,7 +163,7 @@
explicit ColData(const char* str,
mysql_type_info type = mysql_type_info::string_type,
bool is_null = false) :
- buffer_(new Buffer(str, strlen(str), type, is_null))
+ buffer_(new RefCountedBuffer(str, strlen(str), type, is_null))
{
}
@@ -181,7 +181,7 @@
bool is_null = false)
{
dec_ref_count();
- buffer_ = new Buffer(str, len, type, is_null);
+ buffer_ = new RefCountedBuffer(str, len, type, is_null);
}
/// \brief Assign a C++ string to this object
@@ -195,7 +195,7 @@
bool is_null = false)
{
dec_ref_count();
- buffer_ = new Buffer(str.data(), str.length(), type, is_null);
+ buffer_ = new RefCountedBuffer(str.data(), str.length(), type,
is_null);
}
/// \brief Assign a C string to this object
@@ -209,7 +209,7 @@
bool is_null = false)
{
dec_ref_count();
- buffer_ = new Buffer(str, strlen(str), type, is_null);
+ buffer_ = new RefCountedBuffer(str, strlen(str), type, is_null);
}
/// \brief Return a character within the string.
@@ -307,7 +307,7 @@
{
dec_ref_count();
- buffer_ = new Buffer(rhs.data(), rhs.length(),
+ buffer_ = new RefCountedBuffer(rhs.data(), rhs.length(),
mysql_type_info::string_type, false);
return *this;
@@ -321,7 +321,7 @@
{
dec_ref_count();
- buffer_ = new Buffer(str, strlen(str),
+ buffer_ = new RefCountedBuffer(str, strlen(str),
mysql_type_info::string_type, false);
return *this;
@@ -337,7 +337,7 @@
dec_ref_count();
buffer_ = cd.buffer_;
- ++buffer_->refs_;
+ buffer_->attach();
return *this;
}
@@ -430,59 +430,13 @@
/// contents. If ref count falls to 0, deallocates the buffer.
void dec_ref_count()
{
- if (buffer_ && (--buffer_->refs_ == 0)) {
+ if (buffer_ && !buffer_->detach()) {
delete buffer_;
+ buffer_ = 0;
}
}
-
- /// \brief Holds a ColData object's internal reference-counted
- /// string buffer.
- class Buffer {
- public:
- /// \brief Standard constructor
- ///
- /// Copies the string into a new buffer one byte longer than
- /// the length value given, using that to hold a C string null
- /// terminator, just for safety. The length value we keep does
- /// not include this extra byte, allowing this same mechanism
- /// to work for both C strings and binary data.
- Buffer(const char* data, size_type length, mysql_type_info type,
- bool is_null);
-
- /// \brief Destructor
- ~Buffer();
-
- /// \brief Return pointer to raw data buffer
- const char* data() const { return data_; }
-
- /// \brief Return number of bytes in data buffer
- ///
- /// Count does not include the trailing null we tack on to our
- /// copy of the buffer for ease of use in C string contexts.
- /// We do this because we can be holding binary data just as
- /// easily as a C string.
- size_type length() const { return length_; }
-
- /// \brief Return the SQL type of the data held in the buffer
- const mysql_type_info& type() const { return type_; }
-
- /// \brief Return true if buffer's contents represent a SQL
- /// null.
- ///
- /// The buffer's actual content will probably be "NULL" or
- /// something like it, but in the SQL data type system, a SQL
- /// null is distinct from a plain string with value "NULL".
- bool is_null() const { return is_null_; }
-
- private:
- const char* data_; ///< pointer to the raw data
buffer
- size_type length_; ///< bytes in buffer, without
trailing null
- mysql_type_info type_; ///< SQL type of data in the buffer
- bool is_null_; ///< if true, string represents
a SQL null
- unsigned int refs_; ///< reference count for this
object
-
- friend class ColData; // allow our parent to modify us
- } *buffer_;
+
+ RefCountedBuffer* buffer_; ///< pointer to data buffer manager
object
};
Modified: trunk/lib/refcounted.h
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/refcounted.h?rev=1833&r1=1832&r2=1833&view=diff
==============================================================================
--- trunk/lib/refcounted.h (original)
+++ trunk/lib/refcounted.h Thu Nov 8 03:58:29 2007
@@ -1,5 +1,6 @@
/// \file refcounted.h
-/// \brief Declares the RefCountedPointer template
+/// \brief Declares the RefCountedPointer template and RefCountedBuffer
+/// class
/***********************************************************************
Copyright (c) 2007 by Educational Technology Resources, Inc.
@@ -26,6 +27,10 @@
#if !defined(MYSQLPP_REFCOUNTED_H)
#define MYSQLPP_REFCOUNTED_H
+
+#include "type_info.h"
+
+namespace mysqlpp {
/// \brief Creates an object that acts as a reference-counted pointer
/// to another object.
@@ -196,5 +201,69 @@
size_t* refs_;
};
+
+/// \brief Holds a reference-counted data buffer, like a primitive
+/// sort of std::string.
+class RefCountedBuffer
+{
+public:
+ /// \brief Type of length values
+ typedef unsigned int size_type;
+
+ /// \brief Standard constructor
+ ///
+ /// Copies the string into a new buffer one byte longer than
+ /// the length value given, using that to hold a C string null
+ /// terminator, just for safety. The length value we keep does
+ /// not include this extra byte, allowing this same mechanism
+ /// to work for both C strings and binary data.
+ RefCountedBuffer(const char* data, size_type length,
+ mysql_type_info type, bool is_null);
+
+ /// \brief Destructor
+ ~RefCountedBuffer() { delete[] data_; }
+
+ /// \brief Return pointer to raw data buffer
+ const char* data() const { return data_; }
+
+ /// \brief Return number of bytes in data buffer
+ ///
+ /// Count does not include the trailing null we tack on to our
+ /// copy of the buffer for ease of use in C string contexts.
+ /// We do this because we can be holding binary data just as
+ /// easily as a C string.
+ size_type length() const { return length_; }
+
+ /// \brief Return the SQL type of the data held in the buffer
+ const mysql_type_info& type() const { return type_; }
+
+ /// \brief Return true if buffer's contents represent a SQL
+ /// null.
+ ///
+ /// The buffer's actual content will probably be "NULL" or
+ /// something like it, but in the SQL data type system, a SQL
+ /// null is distinct from a plain string with value "NULL".
+ bool is_null() const { return is_null_; }
+
+ /// \brief Sets the internal SQL null flag
+ void set_null() { is_null_ = true; }
+
+ /// \brief Increment reference count
+ void attach() { ++refs_; }
+
+ /// \brief Decrement reference count
+ /// \return True if reference count has not yet fallen to zero
+ bool detach() { return --refs_ > 0; }
+
+private:
+ const char* data_; ///< pointer to the raw data buffer
+ size_type length_; ///< bytes in buffer, without trailing
null
+ mysql_type_info type_; ///< SQL type of data in the buffer
+ bool is_null_; ///< if true, string represents a SQL
null
+ unsigned int refs_; ///< reference count for this object
+};
+
+} // end namespace mysqlpp
+
#endif // !defined(MYSQLPP_REFCOUNTED_H)
Modified: trunk/mysql++.bkl
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/mysql%2B%2B.bkl?rev=1833&r1=1832&r2=1833&view=diff
==============================================================================
--- trunk/mysql++.bkl (original)
+++ trunk/mysql++.bkl Thu Nov 8 03:58:29 2007
@@ -54,6 +54,7 @@
lib/null.cpp
lib/qparms.cpp
lib/query.cpp
+ lib/refcounted.cpp
lib/result.cpp
lib/row.cpp
lib/stadapter.cpp
_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits