From 9ad1effd8bf3f99adbbf32b1dae8c56c2106e233 Mon Sep 17 00:00:00 2001 From: Aleksandar Fabijanic Date: Wed, 12 Sep 2012 02:10:31 +0000 Subject: [PATCH] removed redundant Poco::Data::Nullable --- Data/SQLite/testsuite/src/SQLiteTest.cpp | 4 +- Data/include/Poco/Data/Nullable.h | 217 ----------------------- Data/include/Poco/Data/TypeHandler.h | 14 +- Data/testsuite/src/DataTest.cpp | 35 ---- Data/testsuite/src/DataTest.h | 1 - Foundation/include/Poco/Nullable.h | 67 +++++++ Foundation/testsuite/src/CoreTest.cpp | 49 +++++ 7 files changed, 125 insertions(+), 262 deletions(-) delete mode 100644 Data/include/Poco/Data/Nullable.h diff --git a/Data/SQLite/testsuite/src/SQLiteTest.cpp b/Data/SQLite/testsuite/src/SQLiteTest.cpp index cb18545bd..a69b610fd 100644 --- a/Data/SQLite/testsuite/src/SQLiteTest.cpp +++ b/Data/SQLite/testsuite/src/SQLiteTest.cpp @@ -44,7 +44,7 @@ #include "Poco/Data/SQLite/Utility.h" #include "Poco/Dynamic/Var.h" #include "Poco/Data/TypeHandler.h" -#include "Poco/Data/Nullable.h" +#include "Poco/Nullable.h" #include "Poco/Data/DataException.h" #include "Poco/Data/SQLite/SQLiteException.h" #include "Poco/Tuple.h" @@ -75,8 +75,8 @@ using Poco::Data::Time; using Poco::Data::AbstractExtractionVec; using Poco::Data::AbstractExtractionVecVec; using Poco::Data::AbstractBindingVec; -using Poco::Data::Nullable; using Poco::Data::NotConnectedException; +using Poco::Nullable; using Poco::Tuple; using Poco::Any; using Poco::AnyCast; diff --git a/Data/include/Poco/Data/Nullable.h b/Data/include/Poco/Data/Nullable.h deleted file mode 100644 index 0cf22f36b..000000000 --- a/Data/include/Poco/Data/Nullable.h +++ /dev/null @@ -1,217 +0,0 @@ -// -// Nullable.h -// -// $Id: //poco/Main/Data/include/Poco/Data/Nullable.h#1 $ -// -// Library: Data -// Package: DataCore -// Module: Nullable -// -// Definition of the Nullable class. -// -// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. -// and Contributors. -// -// Permission is hereby granted, free of charge, to any person or organization -// obtaining a copy of the software and accompanying documentation covered by -// this license (the "Software") to use, reproduce, display, distribute, -// execute, and transmit the Software, and to prepare derivative works of the -// Software, and to permit third-parties to whom the Software is furnished to -// do so, all subject to the following: -// -// The copyright notices in the Software and this entire statement, including -// the above license grant, this restriction and the following disclaimer, -// must be included in all copies of the Software, in whole or in part, and -// all derivative works of the Software, unless such copies or derivative -// works are solely in the form of machine-executable object code generated by -// a source language processor. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT -// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE -// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. -// - - -#ifndef Data_Nullable_INCLUDED -#define Data_Nullable_INCLUDED - -#include "Poco/Data/AbstractBinder.h" -#include - -namespace Poco { -namespace Data { - -template -class Nullable { - /// Nullable class - template for field, that can be null -public: - Nullable() - : _value(), _isNull(true) - /// Creates the Nullable. - { - } - - Nullable(const T& value) - : _value(value), _isNull(false) - /// Creates the Nullable from value - { - } - - Nullable(const NullData&) - : _value(), _isNull(true) - /// Creates the Nullable from null - { - } - - Nullable& operator=(const T& value) - /// Assigns new value to Nullable - { - _isNull = false; - _value = value; - return *this; - } - - Nullable& operator=(const Nullable& other) - /// Assigns other Nullable to Nullable - { - _isNull = other._isNull; - _value = other._value; - return *this; - } - - Nullable& operator=(const NullData&) - /// Assigns null to Nullable - { - _isNull = true; - return *this; - } - - bool operator==(const Nullable& other) const - /// Compares two Nullable - { - return (_isNull && other._isNull) || (_isNull == other._isNull && _value == other._value); - } - - bool operator==(const T& value) const - /// Compares Nullable with value - { - return (!_isNull && _value == value); - } - - bool operator==(const NullData&) const - /// Compares Nullable with null - { - return _isNull; - } - - bool operator!=(const NullData&) const - /// Compares Nullable for non null - { - return !_isNull; - } - - bool operator!=(const T& value) const - /// Compares Nullable with value for non equal - { - return (_isNull || _value != value); - } - - bool operator < (const Nullable& other) const - /// Compares two Nullable objects - { - if (_isNull < other._isNull) - return true; - return (_value < other._value); - } - - operator T& () - /// Get reference to the value - { - return _value; - } - - operator const T& () const - /// Get const reference to the value - { - return _value; - } - - bool isNull() const - /// Test Nullable for null - { - return _isNull; - } - - void setNull(bool isNull = true) - /// Change Nullable "isNull" sign - { - _isNull = isNull; - } - - const T& getValue() const - /// Get value - { - return _value; - } - - void setValue(const T& value) - /// Set value - { - _isNull = false; _value = value; - } - -private: - - T _value; - bool _isNull; -}; - -// -// operators -// - -template -bool operator == (const T& value, const Nullable& nValue) -{ - return (!nValue.isNull() && value == nValue.getValue()); -} - -template -bool operator != (const T& value, const Nullable& nValue) -{ - return (nValue.isNull() || value != nValue.getValue()); -} - -template -bool operator == (const NullData&, const Nullable& nValue) -{ - return nValue.isNull(); -} - -template -bool operator != (const NullData&, const Nullable& nValue) -{ - return !nValue.isNull(); -} - -template -std::ostream& operator<<(std::ostream& out, const Nullable& obj) -{ - if (obj.isNull()) - { - out << "NULL"; - } - else - { - out << obj.getValue(); - } - return out; -} - -} } // namespace Poco::Data - -#endif // Data_Nullable_INCLUDED diff --git a/Data/include/Poco/Data/TypeHandler.h b/Data/include/Poco/Data/TypeHandler.h index eb1020d25..982a8a0bb 100644 --- a/Data/include/Poco/Data/TypeHandler.h +++ b/Data/include/Poco/Data/TypeHandler.h @@ -44,7 +44,7 @@ #include "Poco/Data/AbstractBinder.h" #include "Poco/Data/AbstractExtractor.h" #include "Poco/Data/AbstractPreparator.h" -#include "Poco/Data/Nullable.h" +#include "Poco/Nullable.h" #include "Poco/Tuple.h" #include "Poco/AutoPtr.h" #include "Poco/SharedPtr.h" @@ -285,7 +285,7 @@ public: } else { - pBinder->bind(pos++, obj.getValue(), dir); + pBinder->bind(pos++, obj.value(), dir); } } @@ -298,7 +298,7 @@ public: } else { - pPreparator->prepare(pos++, (T&)obj.getValue()); + pPreparator->prepare(pos++, (T&)obj.value()); } } @@ -310,15 +310,15 @@ public: static void extract(std::size_t pos, Nullable& obj, const Nullable& , AbstractExtractor* pExt) { poco_assert_dbg (pExt != 0); - T value; + T val; - if (pExt->extract(pos++, value)) + if (pExt->extract(pos++, val)) { - obj.setValue(value); + obj.value(val); } else { - obj.setNull(true); + obj.clear(); } } diff --git a/Data/testsuite/src/DataTest.cpp b/Data/testsuite/src/DataTest.cpp index c0daecf1e..234700883 100644 --- a/Data/testsuite/src/DataTest.cpp +++ b/Data/testsuite/src/DataTest.cpp @@ -51,7 +51,6 @@ #include "Poco/Dynamic/Var.h" #include "Poco/Data/DynamicLOB.h" #include "Poco/Data/DynamicDateTime.h" -#include "Poco/Data/Nullable.h" #include "Poco/Exception.h" #include #include @@ -93,7 +92,6 @@ using Poco::Data::AbstractExtractionVec; using Poco::Data::AbstractExtractionVecVec; using Poco::Data::AbstractBinding; using Poco::Data::AbstractBindingVec; -using Poco::Data::Nullable; using Poco::Data::NotConnectedException; @@ -1387,38 +1385,6 @@ void DataTest::testExternalBindingAndExtraction() } -void DataTest::testNullable() -{ - Nullable i; - Nullable f; - Nullable s; - - assert (i.isNull()); - assert (f.isNull()); - assert (s.isNull()); - - i = 1; - f = 1.5; - s = "abc"; - - assert (!i.isNull()); - assert (!f.isNull()); - assert (!s.isNull()); - - assert (i == 1); - assert (f == 1.5); - assert (s == "abc"); - - i.setNull(); - f.setNull(); - s.setNull(); - - assert (i.isNull()); - assert (f.isNull()); - assert (s.isNull()); -} - - void DataTest::setUp() { } @@ -1449,7 +1415,6 @@ CppUnit::Test* DataTest::suite() CppUnit_addTest(pSuite, DataTest, testRowFormat); CppUnit_addTest(pSuite, DataTest, testDateAndTime); CppUnit_addTest(pSuite, DataTest, testExternalBindingAndExtraction); - CppUnit_addTest(pSuite, DataTest, testNullable); return pSuite; } diff --git a/Data/testsuite/src/DataTest.h b/Data/testsuite/src/DataTest.h index 71ca8ab38..fc96dc559 100644 --- a/Data/testsuite/src/DataTest.h +++ b/Data/testsuite/src/DataTest.h @@ -65,7 +65,6 @@ public: void testRowFormat(); void testDateAndTime(); void testExternalBindingAndExtraction(); - void testNullable(); void setUp(); void tearDown(); diff --git a/Foundation/include/Poco/Nullable.h b/Foundation/include/Poco/Nullable.h index d405f4955..6fdac5738 100644 --- a/Foundation/include/Poco/Nullable.h +++ b/Foundation/include/Poco/Nullable.h @@ -43,6 +43,7 @@ #include "Poco/Foundation.h" #include "Poco/Exception.h" #include +#include namespace Poco { @@ -130,6 +131,53 @@ public: std::swap(_isNull, other._isNull); } + bool operator==(const Nullable& other) const + /// Compares two Nullables for equality + { + return (_isNull && other._isNull) || (_isNull == other._isNull && _value == other._value); + } + + bool operator==(const C& value) const + /// Compares Nullable with value for equality + { + return (!_isNull && _value == value); + } + + bool operator!=(const C& value) const + /// Compares Nullable with value for non equality + { + return !(*this == other); + } + + bool operator!=(const Nullable& other) const + /// Compares two Nullables for non equality + { + return !(*this == other); + } + + bool operator < (const Nullable& other) const + /// Compares two Nullable objects. Return true if this object's + /// value is smaler than the other object's value. + /// Null value is smaller than any non-null value. + { + if (_isNull && other._isNull) return false; + + if (!_isNull && !other._isNull) + return (_value < other._value); + + if (_isNull && !other._isNull) return true; + + return false; + } + + bool operator > (const Nullable& other) const + /// Compares two Nullable objects. Return true if this object's + /// value is greater than the other object's value. + /// Any non-null value is greater than a null value. + { + return !(*this == other) && !(*this < other); + } + const C& value() const /// Returns the Nullable's value. /// @@ -148,6 +196,18 @@ public: return _isNull ? deflt : _value; } + operator C& () + /// Get reference to the value + { + return value(); + } + + operator const C& () const + /// Get const reference to the value + { + return value(); + } + bool isNull() const /// Returns true iff the Nullable is empty. { @@ -173,6 +233,13 @@ inline void swap(Nullable& n1, Nullable& n2) } +template +std::ostream& operator<<(std::ostream& out, const Nullable& obj) +{ + if (!obj.isNull()) out << obj.value(); + return out; +} + } // namespace Poco diff --git a/Foundation/testsuite/src/CoreTest.cpp b/Foundation/testsuite/src/CoreTest.cpp index 2e4479cbf..0f7329d2e 100644 --- a/Foundation/testsuite/src/CoreTest.cpp +++ b/Foundation/testsuite/src/CoreTest.cpp @@ -47,6 +47,7 @@ #include "Poco/Delegate.h" #include "Poco/Exception.h" #include +#include #include #include @@ -750,10 +751,39 @@ void CoreTest::testAtomicCounter() void CoreTest::testNullable() { + Nullable i; + Nullable f; + Nullable s; + + assert (i.isNull()); + assert (f.isNull()); + assert (s.isNull()); + + i = 1; + f = 1.5; + s = "abc"; + + assert (!i.isNull()); + assert (!f.isNull()); + assert (!s.isNull()); + + assert (i == 1); + assert (f == 1.5); + assert (s == "abc"); + + i.clear(); + f.clear(); + s.clear(); + + assert (i.isNull()); + assert (f.isNull()); + assert (s.isNull()); + Nullable n1; assert (n1.isNull()); assert (n1.value(42) == 42); + assert (n1.isNull()); try { @@ -777,8 +807,27 @@ void CoreTest::testNullable() assert (!n1.isNull()); assert (n1.value() == 42); + std::ostringstream str; + str << n1; + assert (str.str() == "42"); + n1.clear(); assert (n1.isNull()); + + str.str(""); str << n1; + assert (str.str().empty()); + + n2.clear(); + assert (n1 == n2); + n1 = 1; n2 = 1; + assert (n1 == n2); + n1.clear(); + assert (n1 < n2); + assert (n2 > n1); + n2 = -1; n1 = 0; + assert (n2 < n1); + assert (n2 != n1); + assert (n1 > n2); }