Author: wyoung
Date: Sat Nov 10 02:08:43 2007
New Revision: 1836
URL: http://svn.gna.org/viewcvs/mysqlpp?rev=1836&view=rev
Log:
- Added RefCountedBuffer ctor taking std::string, and factored out
common code between that and previous one taking const char* and a
length value.
- Added RefCountedBuffer::is_string()
Added:
trunk/lib/refcounted.cpp
Modified:
trunk/lib/refcounted.h
Added: trunk/lib/refcounted.cpp
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/refcounted.cpp?rev=1836&view=auto
==============================================================================
--- trunk/lib/refcounted.cpp (added)
+++ trunk/lib/refcounted.cpp Sat Nov 10 02:08:43 2007
@@ -1,0 +1,58 @@
+/***********************************************************************
+ refcounted.cpp - Implements the RefCountedBuffer class.
+
+ Copyright (c) 2007 by Educational Technology Resources, Inc.
+ Others may also hold copyrights on code in this file. See the
+ CREDITS file in the top directory of the distribution for details.
+
+ This file is part of MySQL++.
+
+ MySQL++ is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ MySQL++ is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with MySQL++; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
+ USA
+***********************************************************************/
+
+#include "refcounted.h"
+
+namespace mysqlpp {
+
+
+void
+RefCountedBuffer::init(const char* pd, size_type length,
+ mysql_type_info type, bool is_null)
+{
+ type_ = type;
+ is_null_ = is_null;
+ refs_ = 1;
+
+ 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.
+ 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=1836&r1=1835&r2=1836&view=diff
==============================================================================
--- trunk/lib/refcounted.h (original)
+++ trunk/lib/refcounted.h Sat Nov 10 02:08:43 2007
@@ -29,6 +29,8 @@
#define MYSQLPP_REFCOUNTED_H
#include "type_info.h"
+
+#include <string>
namespace mysqlpp {
@@ -210,7 +212,7 @@
/// \brief Type of length values
typedef unsigned int size_type;
- /// \brief Standard constructor
+ /// \brief Initialize object as a copy of a raw data buffer
///
/// Copies the string into a new buffer one byte longer than
/// the length value given, using that to hold a C string null
@@ -218,7 +220,12 @@
/// 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);
+ mysql_type_info type, bool is_null)
+ { init(data, length, type, is_null); }
+
+ /// \brief Initialize object as a copy of a C++ string object
+ RefCountedBuffer(const std::string& s, mysql_type_info type,
+ bool is_null) { init(s.data(), s.length(), type,
is_null); }
/// \brief Destructor
~RefCountedBuffer() { delete[] data_; }
@@ -237,6 +244,9 @@
/// \brief Return the SQL type of the data held in the buffer
const mysql_type_info& type() const { return type_; }
+ /// \brief Returns true if type of buffer's contents is string
+ bool is_string() { return type_ == mysql_type_info::string_type; }
+
/// \brief Return true if buffer's contents represent a SQL
/// null.
///
@@ -256,6 +266,9 @@
bool detach() { return --refs_ > 0; }
private:
+ void init(const char* pd, size_type len, mysql_type_info type,
+ bool is_null); ///< common initialization for ctors
+
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
_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits