Author: wyoung
Date: Thu Jan 31 20:49:52 2008
New Revision: 2149
URL: http://svn.gna.org/viewcvs/mysqlpp?rev=2149&view=rev
Log:
- Date, Time and DateTime classes were all supposed to have std::string
conversion operators, but it didn't work because the template method
wasn't instantiated at library build time, and it was defined in
datetime.cpp, so it couldn't happen at library use time. There's no
real advantage to having this in the base class, so just moved it down
into the leaves, so it's always available.
- Added str() methods to these classes, needed to support previous.
Plus, it's a good C++ish thing to do.
- Expanded test/datetime.cpp to cover these new methods
Modified:
trunk/lib/datetime.cpp
trunk/lib/datetime.h
trunk/test/datetime.cpp
Modified: trunk/lib/datetime.cpp
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/datetime.cpp?rev=2149&r1=2148&r2=2149&view=diff
==============================================================================
--- trunk/lib/datetime.cpp (original)
+++ trunk/lib/datetime.cpp Thu Jan 31 20:49:52 2008
@@ -38,13 +38,6 @@
using namespace std;
namespace mysqlpp {
-
-template <class T>
-DTbase<T>::operator std::string() const
-{
- return stream2string(*this);
-}
-
std::ostream& operator <<(std::ostream& os, const Date& d)
{
@@ -192,6 +185,25 @@
}
}
}
+
+
+Date::operator std::string() const
+{
+ return stream2string(*this);
+}
+
+
+DateTime::operator std::string() const
+{
+ return stream2string(*this);
+}
+
+
+Time::operator std::string() const
+{
+ return stream2string(*this);
+}
+
DateTime::operator time_t() const
{
@@ -215,6 +227,7 @@
return mktime(&tm);
}
}
+
DateTime::DateTime(time_t t)
{
Modified: trunk/lib/datetime.h
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/datetime.h?rev=2149&r1=2148&r2=2149&view=diff
==============================================================================
--- trunk/lib/datetime.h (original)
+++ trunk/lib/datetime.h Thu Jan 31 20:49:52 2008
@@ -50,9 +50,6 @@
{
/// \brief Destroy object
virtual ~DTbase() { }
-
- /// \brief Return a copy of the item in C++ string form
- operator std::string() const;
/// \brief Compare this object to another of the same type
///
@@ -214,8 +211,14 @@
/// This is just syntactic sugar around the default ctor
static DateTime now() { return DateTime(); }
+ /// Convert to std::string
+ operator std::string() const;
+
/// Convert to time_t
operator time_t() const;
+
+ /// Return our value in std::string form
+ std::string str() const { return *this; }
private:
bool now_; ///< true if object not initialized with explicit value
@@ -294,6 +297,12 @@
/// \brief Parse a MySQL date string into this object.
cchar* convert(cchar*);
+
+ /// Convert to std::string
+ operator std::string() const;
+
+ /// Return our value in std::string form
+ std::string str() const { return *this; }
};
/// \brief Inserts a Date object into a C++ stream
@@ -367,6 +376,12 @@
/// Returns < 0 if this time is before the other, 0 of they are
/// equal, and > 0 if this time is after the other.
int compare(const Time& other) const;
+
+ /// Convert to std::string
+ operator std::string() const;
+
+ /// Return our value in std::string form
+ std::string str() const { return *this; }
};
/// \brief Inserts a Time object into a C++ stream in a MySQL-compatible
Modified: trunk/test/datetime.cpp
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/test/datetime.cpp?rev=2149&r1=2148&r2=2149&view=diff
==============================================================================
--- trunk/test/datetime.cpp (original)
+++ trunk/test/datetime.cpp Thu Jan 31 20:49:52 2008
@@ -1,7 +1,7 @@
/***********************************************************************
test/datetime.cpp - Tests the Date, DateTime, and Time classes.
- Copyright (c) 2007 by Educational Technology Resources, Inc.
+ Copyright (c) 2007-2008 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.
@@ -27,6 +27,7 @@
#include <iostream>
#include <sstream>
+#include <string>
#include <stdio.h>
@@ -37,7 +38,8 @@
// Compare the given string against the object inserted into an ostream.
template <class T>
static unsigned int
-test_ostream_form(const T& object, const char* expected, const char* what)
+test_ostream_insert(const T& object, const char* expected,
+ const char* what)
{
ostringstream os;
os << object;
@@ -46,9 +48,56 @@
}
else {
cerr << what << " '" << object << "' should be '" <<
- expected << "' in string form!" << endl;
+ expected << "' when inserted into ostream!" <<
endl;
return 1;
}
+}
+
+
+// Compare the given string against the return value of the object's
+// str() method.
+template <class T>
+static unsigned int
+test_str_method(const T& object, const char* expected, const char* what)
+{
+ if (object.str().compare(expected) == 0) {
+ return 0;
+ }
+ else {
+ cerr << what << " '" << object << "' should return '" <<
+ expected << "' from str() method!" << endl;
+ return 1;
+ }
+}
+
+
+// Compare the given string against the object when cast to std::string
+template <class T>
+static unsigned int
+test_string_operator(const T& object, const char* expected,
+ const char* what)
+{
+ if (string(object).compare(expected) == 0) {
+ return 0;
+ }
+ else {
+ cerr << what << " '" << object << "' should be '" <<
+ expected << "' when cast to std::string!" <<
endl;
+ return 1;
+ }
+}
+
+
+// Compare the given string against the object when converted in several
+// different ways to a string.
+template <class T>
+static unsigned int
+test_stringization(const T& object, const char* expected,
+ const char* what)
+{
+ return test_ostream_insert(object, expected, what) +
+ test_string_operator(object, expected, what) +
+ test_str_method(object, expected, what);
}
@@ -61,7 +110,7 @@
char ac[20];
snprintf(ac, sizeof(ac), "%04d-%02d-%02d",
year, month, day);
- return test_ostream_form(d, ac, "Date");
+ return test_stringization(d, ac, "Date");
}
else {
cerr << "Date '" << d << "' values should be '" <<
@@ -80,7 +129,7 @@
char ac[20];
snprintf(ac, sizeof(ac), "%02d:%02d:%02d",
hour, minute, second);
- return test_ostream_form(t, ac, "Time");
+ return test_stringization(t, ac, "Time");
}
else {
cerr << "Time '" << t << "' values should be '" <<
@@ -124,10 +173,10 @@
unsigned int failures = 0;
failures += test(0, 0, 0, 0, 0, 0);
failures += test(1, 2, 3, 4, 5, 6);
- failures += test_ostream_form(DateTime(), "NOW()", "DateTime");
+ failures += test_stringization(DateTime(), "NOW()", "DateTime");
DateTime dt;
dt.year = 2007;
- failures += test_ostream_form(dt, "2007-00-00 00:00:00", "DateTime");
+ failures += test_stringization(dt, "2007-00-00 00:00:00", "DateTime");
return failures;
}
_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits