Author: wyoung
Date: Thu Nov  8 03:27:44 2007
New Revision: 1832

URL: http://svn.gna.org/viewcvs/mysqlpp?rev=1832&view=rev
Log:
- Parameterized tiny_int class on the value type, so you can get both
  signed and unsigned TINYINTs
- Changed the sql_tinyint and sql_tinyint_unsigned typedefs to use
  mysqlpp::tiny_int<VT> instead of raw chars
- SQLTypeAdapter mechanisms using chars now treat them as chars --
  single-character strings, basically
- Added tinyint interfaces to SQLTypeAdapter which basically replace the
  former char interfaces

Modified:
    trunk/Wishlist
    trunk/lib/custom.pl
    trunk/lib/sql_types.h
    trunk/lib/stadapter.cpp
    trunk/lib/stadapter.h
    trunk/lib/tiny_int.h

Modified: trunk/Wishlist
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/Wishlist?rev=1832&r1=1831&r2=1832&view=diff
==============================================================================
--- trunk/Wishlist (original)
+++ trunk/Wishlist Thu Nov  8 03:27:44 2007
@@ -16,14 +16,6 @@
     o The manipulator changes sometime during 2.3 broke the column
       formatting of the examples that dump tables.  A setw(x) call
       results in x spaces, not x - strlen(whatevercamebefore).
-
-    o Change SQLTypeAdapter(char) and friends to be single-character
-      string conversions, and add SQLTypeAdapter(tiny_int) to take
-      up old functionality.
-
-    o Replace 'char'-based typedefs in sql_types.h with tiny_int.
-
-    o Templatize class tiny_int to allow for unsigned storage.
 
     o Rename ColData to String, reflecting its general-purpose nature.
 

Modified: trunk/lib/custom.pl
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/custom.pl?rev=1832&r1=1831&r2=1832&view=diff
==============================================================================
--- trunk/lib/custom.pl (original)
+++ trunk/lib/custom.pl Thu Nov  8 03:27:44 2007
@@ -57,7 +57,7 @@
 #ifndef MYSQLPP_CUSTOM_H
 #define MYSQLPP_CUSTOM_H
 
-#include "tiny_int.h"
+#include "sql_types.h"
 
 #include <string>
 ---
@@ -91,7 +91,7 @@
 }
 ---
 
-my @types = ("char", "unsigned char", "tiny_int", "int", "unsigned int",
+my @types = ("char", "unsigned char", "sql_tinyint", "int", "unsigned int",
          "short int", "unsigned short int", "unsigned long", "long");
 foreach my $type (@types) {
     print OUT0 << "---";

Modified: trunk/lib/sql_types.h
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/sql_types.h?rev=1832&r1=1831&r2=1832&view=diff
==============================================================================
--- trunk/lib/sql_types.h (original)
+++ trunk/lib/sql_types.h Thu Nov  8 03:27:44 2007
@@ -24,8 +24,10 @@
  USA
 ***********************************************************************/
 
-#if !defined(MYSQLPP_SQL_TYPES_H)
-#define MYSQLPP_SQL_TYPES_H
+#if !defined(MYSQLPP_SQL_TYPES_H_MAIN)
+#define MYSQLPP_SQL_TYPES_H_MAIN
+
+#include "tiny_int.h"
 
 #include <string>
 
@@ -37,51 +39,71 @@
 // Nearest C++ equivalents of MySQL data types.  These are only the "NOT
 // NULL" variants.  Wrap these types in MySQL++'s Null<> template to get
 // NULL-able types.
-typedef signed char            sql_tinyint;
-typedef unsigned char  sql_tinyint_unsigned;
-typedef short                  sql_smallint;
-typedef unsigned short sql_smallint_unsigned;
-typedef int                            sql_int;
-typedef unsigned int   sql_int_unsigned;
-typedef int                            sql_mediumint;
-typedef unsigned int   sql_mediumint_unsigned;
-typedef longlong               sql_bigint;
-typedef ulonglong              sql_bigint_unsigned;
+typedef tiny_int<signed char>  sql_tinyint;
+typedef tiny_int<unsigned char>        sql_tinyint_unsigned;
+typedef short                                  sql_smallint;
+typedef unsigned short                 sql_smallint_unsigned;
+typedef int                                            sql_int;
+typedef unsigned int                   sql_int_unsigned;
+typedef int                                            sql_mediumint;
+typedef unsigned int                   sql_mediumint_unsigned;
+typedef longlong                               sql_bigint;
+typedef ulonglong                              sql_bigint_unsigned;
 
-typedef float                  sql_float;
-typedef double                 sql_double;
-typedef double                 sql_decimal;
+typedef float                                  sql_float;
+typedef double                                 sql_double;
+typedef double                                 sql_decimal;
 
-typedef std::string            sql_enum;
+typedef std::string                            sql_enum;
 
-typedef ColData                        sql_blob;
-typedef ColData                        sql_tinyblob;
-typedef ColData                        sql_mediumblob;
-typedef ColData                        sql_longblob;
-
-typedef std::string            sql_char;
-typedef std::string            sql_varchar;
-typedef std::string            sql_tinytext;
-typedef std::string            sql_text;
-typedef std::string            sql_mediumtext;
-typedef std::string            sql_longtext;
-
-#ifdef MYSQLPP_DATETIME_H
-    // MySQL++ date and time types are defined, so make aliases for
-    // them matching the style of the above types.
-    typedef Date               sql_date;
-    typedef Time               sql_time;
-    typedef DateTime   sql_timestamp;
-    typedef DateTime   sql_datetime;
-#endif
-#ifdef MYSQLPP_MYSET_H
-    // Ditto for MySQL++'s SQL set type
-    typedef Set<>              sql_set;
-#endif
-
+typedef std::string                            sql_char;
+typedef std::string                            sql_varchar;
+typedef std::string                            sql_tinytext;
+typedef std::string                            sql_text;
+typedef std::string                            sql_mediumtext;
+typedef std::string                            sql_longtext;
 #endif // !defined(DOXYGEN_IGNORE)
 
 } // end namespace mysqlpp
 
-#endif // !defined(MYSQLPP_SQL_TYPES_H)
+#endif // !defined(MYSQLPP_SQL_TYPES_H_MAIN)
 
+
+// The following sections are treated separately to avoid making the
+// #include tree too dense: if coldata.h (for example) is not yet
+// #included, no sense pulling it in to define all the typedefs based
+// on ColData.  The separate #include guards for each section allow
+// this file to be #included as many times as necessary to build up the
+// full typedef set.  This trickery is necessary because sql_types.h
+// is needed in a few places within MySQL++, but we can't (and don't)
+// depend on having the full set of typedefs.  mysql++.h #includes this
+// at a late stage, ensuring that end-user code does see the full set.
+#if defined(MYSQLPP_COLDATA_H) && !defined(MYSQLPP_SQL_TYPES_H_COLDATA) && 
!defined(DOXYGEN_IGNORE)
+#      define MYSQLPP_SQL_TYPES_H_COLDATA
+       namespace mysqlpp {
+               typedef ColData                 sql_blob;
+               typedef ColData                 sql_tinyblob;
+               typedef ColData                 sql_mediumblob;
+               typedef ColData                 sql_longblob;
+       } // end namespace mysqlpp
+#endif
+
+
+#if defined(MYSQLPP_DATETIME_H) && !defined(MYSQLPP_SQL_TYPES_H_DATETIME) && 
!defined(DOXYGEN_IGNORE)
+#      define MYSQLPP_SQL_TYPES_H_DATETIME
+       namespace mysqlpp {
+               typedef Date                    sql_date;
+               typedef Time                    sql_time;
+               typedef DateTime                sql_timestamp;
+               typedef DateTime                sql_datetime;
+       } // end namespace mysqlpp
+#endif
+
+
+#if defined(MYSQLPP_MYSET_H) && !defined(MYSQLPP_SQL_TYPES_H_SET) && 
!defined(DOXYGEN_IGNORE)
+#      define MYSQLPP_SQL_TYPES_H_SET
+       namespace mysqlpp {
+               typedef Set<>                           sql_set;
+       } // end namespace mysqlpp
+#endif
+

Modified: trunk/lib/stadapter.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/stadapter.cpp?rev=1832&r1=1831&r2=1832&view=diff
==============================================================================
--- trunk/lib/stadapter.cpp (original)
+++ trunk/lib/stadapter.cpp Thu Nov  8 03:27:44 2007
@@ -69,29 +69,43 @@
 {
 }
 
-SQLTypeAdapter::SQLTypeAdapter(signed char i) :
-string(stream2string<int>(i)),
-is_string_(false),
-is_processed_(false)
-{
-}
-
-SQLTypeAdapter::SQLTypeAdapter(Null<char> i) :
-string(i.is_null ? null_str : stream2string<int>(i)),
-is_string_(false),
-is_processed_(false)
-{
-}
-
-SQLTypeAdapter::SQLTypeAdapter(unsigned char i) :
-string(stream2string<unsigned>(i)),
-is_string_(false),
-is_processed_(false)
-{
-}
-
-SQLTypeAdapter::SQLTypeAdapter(Null<unsigned char> i) :
-string(i.is_null ? null_str : stream2string<unsigned>(i)),
+SQLTypeAdapter::SQLTypeAdapter(char c) :
+string(stream2string(c)),
+is_string_(true),
+is_processed_(false)
+{
+}
+
+SQLTypeAdapter::SQLTypeAdapter(Null<char> c) :
+string(c.is_null ? null_str : stream2string(c)),
+is_string_(true),
+is_processed_(false)
+{
+}
+
+SQLTypeAdapter::SQLTypeAdapter(sql_tinyint i) :
+string(stream2string(i)),
+is_string_(false),
+is_processed_(false)
+{
+}
+
+SQLTypeAdapter::SQLTypeAdapter(Null<sql_tinyint> i) :
+string(i.is_null ? null_str : stream2string(i)),
+is_string_(false),
+is_processed_(false)
+{
+}
+
+SQLTypeAdapter::SQLTypeAdapter(sql_tinyint_unsigned i) :
+string(stream2string(i)),
+is_string_(false),
+is_processed_(false)
+{
+}
+
+SQLTypeAdapter::SQLTypeAdapter(Null<sql_tinyint_unsigned> i) :
+string(i.is_null ? null_str : stream2string(i)),
 is_string_(false),
 is_processed_(false)
 {

Modified: trunk/lib/stadapter.h
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/stadapter.h?rev=1832&r1=1831&r2=1832&view=diff
==============================================================================
--- trunk/lib/stadapter.h (original)
+++ trunk/lib/stadapter.h Thu Nov  8 03:27:44 2007
@@ -55,6 +55,7 @@
 
 #include "datetime.h"
 #include "null.h"
+#include "sql_types.h"
 
 #include <string>
 
@@ -78,13 +79,20 @@
        /// \brief Create a copy of an arbitrary block of data
        SQLTypeAdapter(const char* str, size_t len);
 
-       /// \brief Create a string representation of a \c char treated as a
-       /// small integer (not a character!)
-       SQLTypeAdapter(signed char i);
-
-       /// \brief Create a string representation of an \c unsigned \c char
-       /// treated as a small unsigned integer (not a character!)
-       SQLTypeAdapter(unsigned char i);
+       /// \brief Create a single-character string
+       ///
+       /// If you mean for \c c to be treated as a small integer, you
+       /// should be using mysqlpp::tiny_int instead.  It avoids the
+       /// confusion in C++ between integer and character.  See the
+       /// documentation for tiny_int.h for details.
+       SQLTypeAdapter(char c);
+
+       /// \brief Create a string representation of SQL \c TINYINT
+       SQLTypeAdapter(sql_tinyint i);
+
+       /// \brief Create a string representation of SQL \c TINYINT
+       /// \c UNSIGNED
+       SQLTypeAdapter(sql_tinyint_unsigned i);
 
        /// \brief Create a string representation of a \c short \c int value
        SQLTypeAdapter(short int i);
@@ -167,8 +175,9 @@
        // Parallel interface for Null<>-wrapped versions of types we
        // support above.  No need for parallel documentation.
        SQLTypeAdapter(const Null<std::string>& str);
-       SQLTypeAdapter(Null<char> i);
-       SQLTypeAdapter(Null<unsigned char> i);
+       SQLTypeAdapter(Null<char> c);
+       SQLTypeAdapter(Null<sql_tinyint> i);
+       SQLTypeAdapter(Null<sql_tinyint_unsigned> i);
        SQLTypeAdapter(Null<short int> i);
        SQLTypeAdapter(Null<unsigned short int> i);
        SQLTypeAdapter(Null<int> i);

Modified: trunk/lib/tiny_int.h
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/tiny_int.h?rev=1832&r1=1831&r2=1832&view=diff
==============================================================================
--- trunk/lib/tiny_int.h (original)
+++ trunk/lib/tiny_int.h Thu Nov  8 03:27:44 2007
@@ -1,5 +1,5 @@
-/// \file tiny_int.h
-/// \brief Declares class for holding a SQL tiny_int
+/// \file this_type.h
+/// \brief Declares class for holding a SQL TINYINT
 
 /***********************************************************************
  Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
@@ -25,26 +25,40 @@
  USA
 ***********************************************************************/
 
-#ifndef MYSQLPP_TINY_INT_H
+#if !defined(MYSQLPP_TINY_INT_H)
 #define MYSQLPP_TINY_INT_H
 
 #include "common.h"
 
+#include <ostream>
+
 namespace mysqlpp {
 
-/// \brief Class for holding an SQL \c tiny_int object.
+/// \brief Class for holding an SQL \c TINYINT value
 ///
 /// This is required because the closest C++ type, \c char, doesn't
 /// have all the right semantics.  For one, inserting a \c char into a
-/// stream won't give you a number.
+/// stream won't give you a number.  For another, if you don't specify
+/// signedness explicitly, C++ doesn't give a default, so it's signed
+/// on some platforms, unsigned on others.
 ///
-/// Several of the functions below accept a \c short \c int argument,
-/// but internally we store the data as a \c char. Beware of integer
-/// overflows!
-
+/// The template parameter is intended to allow instantiating it as
+/// tiny_int<unsigned char> to hold \c TINYINT \c UNSIGNED values.
+/// There's nothing stopping you from using any other integer type if
+/// you want to be perverse, but please don't do that.
+///
+/// Several of the functions below accept an \c int argument, but
+/// internally we store the data as a \c char by default. Beware of
+/// integer overflows!
+
+template <typename VT = signed char>
 class MYSQLPP_EXPORT tiny_int
 {
 public:
+       //// Type aliases
+       typedef tiny_int<VT> this_type;
+       typedef VT value_type;
+
        /// \brief Default constructor
        ///
        /// Value is uninitialized
@@ -52,190 +66,203 @@
        
        /// \brief Create object from any integral type that can be
        /// converted to a \c short \c int.
-       tiny_int(short int v) :
-       value_(char(v))
-       {
-       }
-       
-       /// \brief Return value as a \c short \c int.
-       operator short int() const
-       {
-               return static_cast<short int>(value_);
-       }
-
-       /// \brief Assign a \c short \c int to the object.
-       tiny_int& operator =(short int v)
-       {
-               value_ = char(v);
+       tiny_int(value_type v) :
+       value_(value_type(v))
+       {
+       }
+       
+       /// \brief Return value as an \c int.
+       operator int() const
+       {
+               return static_cast<int>(value_);
+       }
+
+       /// \brief Return raw data value with no size change
+       operator value_type() const
+       {
+               return value_;
+       }
+
+       /// \brief Assign a new value to the object.
+       this_type& operator =(int v)
+       {
+               value_ = static_cast<value_type>(v);
                return *this;
        }
 
        /// \brief Add another value to this object
-       tiny_int& operator +=(short int v)
-       {
-               value_ += char(v);
+       this_type& operator +=(int v)
+       {
+               value_ += static_cast<value_type>(v);
                return *this;
        }
 
        /// \brief Subtract another value to this object
-       tiny_int& operator -=(short int v)
-       {
-               value_ -= char(v);
+       this_type& operator -=(int v)
+       {
+               value_ -= static_cast<value_type>(v);
                return *this;
        }
 
        /// \brief Multiply this value by another object
-       tiny_int& operator *=(short int v)
-       {
-               value_ *= char(v);
+       this_type& operator *=(int v)
+       {
+               value_ *= static_cast<value_type>(v);
                return *this;
        }
 
        /// \brief Divide this value by another object
-       tiny_int& operator /=(short int v)
-       {
-               value_ /= char(v);
+       this_type& operator /=(int v)
+       {
+               value_ /= static_cast<value_type>(v);
                return *this;
        }
 
        /// \brief Divide this value by another object and store the
        /// remainder
-       tiny_int& operator %=(short int v)
-       {
-               value_ %= char(v);
+       this_type& operator %=(int v)
+       {
+               value_ %= static_cast<value_type>(v);
                return *this;
        }
 
        /// \brief Bitwise AND this value by another value
-       tiny_int& operator &=(short int v)
-       {
-               value_ &= char(v);
+       this_type& operator &=(int v)
+       {
+               value_ &= static_cast<value_type>(v);
                return *this;
        }
 
        /// \brief Bitwise OR this value by another value
-       tiny_int& operator |=(short int v)
-       {
-               value_ |= char(v);
+       this_type& operator |=(int v)
+       {
+               value_ |= static_cast<value_type>(v);
                return *this;
        }
 
        /// \brief Bitwise XOR this value by another value
-       tiny_int& operator ^=(short int v)
-       {
-               value_ ^= char(v);
+       this_type& operator ^=(int v)
+       {
+               value_ ^= static_cast<value_type>(v);
                return *this;
        }
 
        /// \brief Shift this value left by \c v positions
-       tiny_int& operator <<=(short int v)
-       {
-               value_ <<= char(v);
+       this_type& operator <<=(int v)
+       {
+               value_ <<= static_cast<value_type>(v);
                return *this;
        }
 
        /// \brief Shift this value right by \c v positions
-       tiny_int& operator >>=(short int v)
-       {
-               value_ >>= char(v);
+       this_type& operator >>=(int v)
+       {
+               value_ >>= static_cast<value_type>(v);
                return *this;
        }
 
        /// \brief Add one to this value and return that value
-       tiny_int& operator ++()
-       {
-               value_++;
+       this_type& operator ++()
+       {
+               ++value_;
                return *this;
        }
 
        /// \brief Subtract one from this value and return that value
-       tiny_int& operator --()
-       {
-               value_--;
+       this_type& operator --()
+       {
+               --value_;
                return *this;
        }
 
        /// \brief Add one to this value and return the previous value
-       tiny_int operator ++(int)
-       {
-               tiny_int tmp = value_;
-               value_++;
+       this_type operator ++(int)
+       {
+               this_type tmp = value_;
+               ++value_;
                return tmp;
        }
 
        /// \brief Subtract one from this value and return the previous
        /// value
-       tiny_int operator --(int)
-       {
-               tiny_int tmp = value_;
-               value_--;
+       this_type operator --(int)
+       {
+               this_type tmp = value_;
+               --value_;
                return tmp;
        }
 
        /// \brief Return this value minus \c i
-       tiny_int operator -(const tiny_int& i) const
+       this_type operator -(const this_type& i) const
        {
                return value_ - i;
        }
        
        /// \brief Return this value plus \c i
-       tiny_int operator +(const tiny_int& i) const
+       this_type operator +(const this_type& i) const
        {
                return value_ + i;
        }
        
        /// \brief Return this value multiplied by \c i
-       tiny_int operator *(const tiny_int& i) const
+       this_type operator *(const this_type& i) const
        {
                return value_ * i;
        }
        
        /// \brief Return this value divided by \c i
-       tiny_int operator /(const tiny_int& i) const
+       this_type operator /(const this_type& i) const
        {
                return value_ / i;
        }
        
        /// \brief Return the modulus of this value divided by \c i
-       tiny_int operator %(const tiny_int& i) const
+       this_type operator %(const this_type& i) const
        {
                return value_ % i;
        }
        
        /// \brief Return this value bitwise OR'd by \c i
-       tiny_int operator |(const tiny_int& i) const
+       this_type operator |(const this_type& i) const
        {
                return value_ | i;
        }
        
        /// \brief Return this value bitwise AND'd by \c i
-       tiny_int operator &(const tiny_int& i) const
+       this_type operator &(const this_type& i) const
        {
                return value_ & i;
        }
        
        /// \brief Return this value bitwise XOR'd by \c i
-       tiny_int operator ^(const tiny_int& i) const
+       this_type operator ^(const this_type& i) const
        {
                return value_ ^ i;
        }
        
        /// \brief Return this value bitwise shifted left by \c i
-       tiny_int operator <<(const tiny_int& i) const
+       this_type operator <<(const this_type& i) const
        {
                return value_ << i;
        }
        
        /// \brief Return this value bitwise shifted right by \c i
-       tiny_int operator >>(const tiny_int& i) const
+       this_type operator >>(const this_type& i) const
        {
                return value_ >> i;
        }
 
 private:
-       char value_;
+       value_type value_;
 };
 
+template <typename VT>
+std::ostream& operator <<(std::ostream& os, tiny_int<VT> i)
+{
+       os << static_cast<int>(i);
+       return os;
+}
+
 } // end namespace mysqlpp
 
 #endif


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

Reply via email to