removed redundant Poco::Data::Nullable

This commit is contained in:
Aleksandar Fabijanic 2012-09-12 02:10:31 +00:00
parent b7957f8cd0
commit 9ad1effd8b
7 changed files with 125 additions and 262 deletions

View File

@ -44,7 +44,7 @@
#include "Poco/Data/SQLite/Utility.h" #include "Poco/Data/SQLite/Utility.h"
#include "Poco/Dynamic/Var.h" #include "Poco/Dynamic/Var.h"
#include "Poco/Data/TypeHandler.h" #include "Poco/Data/TypeHandler.h"
#include "Poco/Data/Nullable.h" #include "Poco/Nullable.h"
#include "Poco/Data/DataException.h" #include "Poco/Data/DataException.h"
#include "Poco/Data/SQLite/SQLiteException.h" #include "Poco/Data/SQLite/SQLiteException.h"
#include "Poco/Tuple.h" #include "Poco/Tuple.h"
@ -75,8 +75,8 @@ using Poco::Data::Time;
using Poco::Data::AbstractExtractionVec; using Poco::Data::AbstractExtractionVec;
using Poco::Data::AbstractExtractionVecVec; using Poco::Data::AbstractExtractionVecVec;
using Poco::Data::AbstractBindingVec; using Poco::Data::AbstractBindingVec;
using Poco::Data::Nullable;
using Poco::Data::NotConnectedException; using Poco::Data::NotConnectedException;
using Poco::Nullable;
using Poco::Tuple; using Poco::Tuple;
using Poco::Any; using Poco::Any;
using Poco::AnyCast; using Poco::AnyCast;

View File

@ -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 <iostream>
namespace Poco {
namespace Data {
template <typename T>
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<T>& 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<T>& 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<T>& 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 <typename T>
bool operator == (const T& value, const Nullable<T>& nValue)
{
return (!nValue.isNull() && value == nValue.getValue());
}
template <typename T>
bool operator != (const T& value, const Nullable<T>& nValue)
{
return (nValue.isNull() || value != nValue.getValue());
}
template <typename T>
bool operator == (const NullData&, const Nullable<T>& nValue)
{
return nValue.isNull();
}
template <typename T>
bool operator != (const NullData&, const Nullable<T>& nValue)
{
return !nValue.isNull();
}
template <typename T>
std::ostream& operator<<(std::ostream& out, const Nullable<T>& obj)
{
if (obj.isNull())
{
out << "NULL";
}
else
{
out << obj.getValue();
}
return out;
}
} } // namespace Poco::Data
#endif // Data_Nullable_INCLUDED

View File

@ -44,7 +44,7 @@
#include "Poco/Data/AbstractBinder.h" #include "Poco/Data/AbstractBinder.h"
#include "Poco/Data/AbstractExtractor.h" #include "Poco/Data/AbstractExtractor.h"
#include "Poco/Data/AbstractPreparator.h" #include "Poco/Data/AbstractPreparator.h"
#include "Poco/Data/Nullable.h" #include "Poco/Nullable.h"
#include "Poco/Tuple.h" #include "Poco/Tuple.h"
#include "Poco/AutoPtr.h" #include "Poco/AutoPtr.h"
#include "Poco/SharedPtr.h" #include "Poco/SharedPtr.h"
@ -285,7 +285,7 @@ public:
} }
else else
{ {
pBinder->bind(pos++, obj.getValue(), dir); pBinder->bind(pos++, obj.value(), dir);
} }
} }
@ -298,7 +298,7 @@ public:
} }
else 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<T>& obj, const Nullable<T>& , AbstractExtractor* pExt) static void extract(std::size_t pos, Nullable<T>& obj, const Nullable<T>& , AbstractExtractor* pExt)
{ {
poco_assert_dbg (pExt != 0); 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 else
{ {
obj.setNull(true); obj.clear();
} }
} }

View File

@ -51,7 +51,6 @@
#include "Poco/Dynamic/Var.h" #include "Poco/Dynamic/Var.h"
#include "Poco/Data/DynamicLOB.h" #include "Poco/Data/DynamicLOB.h"
#include "Poco/Data/DynamicDateTime.h" #include "Poco/Data/DynamicDateTime.h"
#include "Poco/Data/Nullable.h"
#include "Poco/Exception.h" #include "Poco/Exception.h"
#include <cstring> #include <cstring>
#include <sstream> #include <sstream>
@ -93,7 +92,6 @@ using Poco::Data::AbstractExtractionVec;
using Poco::Data::AbstractExtractionVecVec; using Poco::Data::AbstractExtractionVecVec;
using Poco::Data::AbstractBinding; using Poco::Data::AbstractBinding;
using Poco::Data::AbstractBindingVec; using Poco::Data::AbstractBindingVec;
using Poco::Data::Nullable;
using Poco::Data::NotConnectedException; using Poco::Data::NotConnectedException;
@ -1387,38 +1385,6 @@ void DataTest::testExternalBindingAndExtraction()
} }
void DataTest::testNullable()
{
Nullable<int> i;
Nullable<double> f;
Nullable<std::string> 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() void DataTest::setUp()
{ {
} }
@ -1449,7 +1415,6 @@ CppUnit::Test* DataTest::suite()
CppUnit_addTest(pSuite, DataTest, testRowFormat); CppUnit_addTest(pSuite, DataTest, testRowFormat);
CppUnit_addTest(pSuite, DataTest, testDateAndTime); CppUnit_addTest(pSuite, DataTest, testDateAndTime);
CppUnit_addTest(pSuite, DataTest, testExternalBindingAndExtraction); CppUnit_addTest(pSuite, DataTest, testExternalBindingAndExtraction);
CppUnit_addTest(pSuite, DataTest, testNullable);
return pSuite; return pSuite;
} }

View File

@ -65,7 +65,6 @@ public:
void testRowFormat(); void testRowFormat();
void testDateAndTime(); void testDateAndTime();
void testExternalBindingAndExtraction(); void testExternalBindingAndExtraction();
void testNullable();
void setUp(); void setUp();
void tearDown(); void tearDown();

View File

@ -43,6 +43,7 @@
#include "Poco/Foundation.h" #include "Poco/Foundation.h"
#include "Poco/Exception.h" #include "Poco/Exception.h"
#include <algorithm> #include <algorithm>
#include <iostream>
namespace Poco { namespace Poco {
@ -130,6 +131,53 @@ public:
std::swap(_isNull, other._isNull); std::swap(_isNull, other._isNull);
} }
bool operator==(const Nullable<C>& 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<C>& other) const
/// Compares two Nullables for non equality
{
return !(*this == other);
}
bool operator < (const Nullable<C>& 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<C>& 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 const C& value() const
/// Returns the Nullable's value. /// Returns the Nullable's value.
/// ///
@ -148,6 +196,18 @@ public:
return _isNull ? deflt : _value; 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 bool isNull() const
/// Returns true iff the Nullable is empty. /// Returns true iff the Nullable is empty.
{ {
@ -173,6 +233,13 @@ inline void swap(Nullable<C>& n1, Nullable<C>& n2)
} }
template <typename C>
std::ostream& operator<<(std::ostream& out, const Nullable<C>& obj)
{
if (!obj.isNull()) out << obj.value();
return out;
}
} // namespace Poco } // namespace Poco

View File

@ -47,6 +47,7 @@
#include "Poco/Delegate.h" #include "Poco/Delegate.h"
#include "Poco/Exception.h" #include "Poco/Exception.h"
#include <iostream> #include <iostream>
#include <sstream>
#include <vector> #include <vector>
#include <cstring> #include <cstring>
@ -750,10 +751,39 @@ void CoreTest::testAtomicCounter()
void CoreTest::testNullable() void CoreTest::testNullable()
{ {
Nullable<int> i;
Nullable<double> f;
Nullable<std::string> 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<int> n1; Nullable<int> n1;
assert (n1.isNull()); assert (n1.isNull());
assert (n1.value(42) == 42); assert (n1.value(42) == 42);
assert (n1.isNull());
try try
{ {
@ -777,8 +807,27 @@ void CoreTest::testNullable()
assert (!n1.isNull()); assert (!n1.isNull());
assert (n1.value() == 42); assert (n1.value() == 42);
std::ostringstream str;
str << n1;
assert (str.str() == "42");
n1.clear(); n1.clear();
assert (n1.isNull()); 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);
} }