Split Bindign in CopyBinding and Binding. Fixes use(const T&) case

This commit is contained in:
Peter Schojer
2008-05-13 11:30:19 +00:00
parent e933e93b50
commit 92a0389b63

View File

@@ -1,7 +1,7 @@
//
// Binding.h
//
// $Id: //poco/Main/Data/include/Poco/Data/Binding.h#6 $
// $Id: //poco/Main/Data/include/Poco/Data/Binding.h#9 $
//
// Library: Data
// Package: DataCore
@@ -45,6 +45,7 @@
#include "Poco/Data/DataException.h"
#include "Poco/Data/TypeHandler.h"
#include "Poco/SharedPtr.h"
#include "Poco/MetaProgramming.h"
#include <vector>
#include <list>
#include <deque>
@@ -73,11 +74,9 @@ class Binding: public AbstractBinding
public:
explicit Binding(T& val,
const std::string& name = "",
Direction direction = PD_IN,
bool copy = false):
Direction direction = PD_IN):
AbstractBinding(name, direction),
_pVal(copy ? new T(val) : 0),
_val(copy ? *_pVal : val),
_val(val),
_bound(false)
/// Creates the Binding using the passed reference as bound value.
/// If copy is true, a copy of the value referred to is created.
@@ -120,12 +119,79 @@ public:
}
private:
SharedPtr<T> _pVal;
const T& _val;
bool _bound;
};
template <class T>
class CopyBinding: public AbstractBinding
/// Binding maps a value or multiple values (see Binding specializations for STL containers as
/// well as type handlers) to database column(s). Values to be bound can be either mapped
/// directly (by reference) or a copy can be created, depending on the value of the copy argument.
/// To pass a reference to a variable, it is recommended to pass it to the intermediate
/// utility function use(), which will create the proper binding. In cases when a reference
/// is passed to binding, the storage it refers to must be valid at the statement execution time.
/// To pass a copy of a variable, constant or string literal, use utility function bind().
/// Variables can be passed as either copies or references (i.e. using either use() or bind()).
/// Constants, however, can only be passed as copies. this is best achieved using bind() utility
/// function. An attempt to pass a constant by reference shall result in compile-time error.
{
public:
explicit CopyBinding(T& val,
const std::string& name = "",
Direction direction = PD_IN):
AbstractBinding(name, direction),
_pVal(new T(val)),
_bound(false)
/// Creates the Binding using the passed reference as bound value.
/// If copy is true, a copy of the value referred to is created.
{
}
~CopyBinding()
/// Destroys the Binding.
{
}
std::size_t numOfColumnsHandled() const
{
return TypeHandler<T>::size();
}
std::size_t numOfRowsHandled() const
{
return 1;
}
bool canBind() const
{
return !_bound;
}
void bind(std::size_t pos)
{
poco_assert_dbg(getBinder() != 0);
TypeHandler<T>::bind(pos, *_pVal, getBinder(), getDirection());
_bound = true;
}
void reset ()
{
_bound = false;
AbstractBinder* pBinder = getBinder();
poco_check_ptr (pBinder);
pBinder->reset();
}
private:
typedef typename TypeWrapper<T>::TYPE ValueType;
SharedPtr<ValueType> _pVal;
bool _bound;
};
template <>
class Binding<const char*>: public AbstractBinding
/// Binding const char* specialization wraps char pointer into string.
@@ -133,8 +199,7 @@ class Binding<const char*>: public AbstractBinding
public:
explicit Binding(const char* pVal,
const std::string& name = "",
Direction direction = PD_IN,
bool copy = true):
Direction direction = PD_IN):
AbstractBinding(name, direction),
_val(pVal ? pVal : throw NullPointerException() ),
_bound(false)
@@ -183,6 +248,63 @@ private:
};
template <>
class CopyBinding<const char*>: public AbstractBinding
/// Binding const char* specialization wraps char pointer into string.
{
public:
explicit CopyBinding(const char* pVal,
const std::string& name = "",
Direction direction = PD_IN):
AbstractBinding(name, direction),
_val(pVal ? pVal : throw NullPointerException() ),
_bound(false)
/// Creates the Binding by copying the passed string.
{
}
~CopyBinding()
/// Destroys the Binding.
{
}
std::size_t numOfColumnsHandled() const
{
return 1u;
}
std::size_t numOfRowsHandled() const
{
return 1u;
}
bool canBind() const
{
return !_bound;
}
void bind(std::size_t pos)
{
poco_assert_dbg(getBinder() != 0);
TypeHandler<std::string>::bind(pos, _val, getBinder(), getDirection());
_bound = true;
}
void reset ()
{
_bound = false;
AbstractBinder* pBinder = getBinder();
poco_check_ptr (pBinder);
pBinder->reset();
}
private:
std::string _val;
bool _bound;
};
template <class T>
class Binding<std::vector<T> >: public AbstractBinding
/// Specialization for std::vector.
@@ -190,17 +312,16 @@ class Binding<std::vector<T> >: public AbstractBinding
public:
explicit Binding(std::vector<T>& val,
const std::string& name = "",
Direction direction = PD_IN,
bool copy = false):
Direction direction = PD_IN):
AbstractBinding(name, direction),
_pVal(copy ? new std::vector<T>(val) : 0),
_val(copy ? *_pVal : val),
_begin(_val.begin()),
_end(_val.end())
_val(val),
_begin(),
_end()
/// Creates the Binding.
{
if (PD_IN == direction && numOfRowsHandled() == 0)
throw BindingException("It is illegal to bind to an empty data collection");
reset();
}
~Binding()
@@ -238,13 +359,76 @@ public:
_end = _val.end();
}
private:
typedef std::vector<T> Type;
typedef typename Type::const_iterator Iterator;
const Type& _val;
Iterator _begin;
Iterator _end;
};
template <class T>
class CopyBinding<std::vector<T> >: public AbstractBinding
/// Specialization for std::vector.
{
public:
explicit CopyBinding(std::vector<T>& val,
const std::string& name = "",
Direction direction = PD_IN):
AbstractBinding(name, direction),
_pVal(new std::vector<T>(val))
_begin(),
_end()
/// Creates the Binding.
{
if (PD_IN == direction && numOfRowsHandled() == 0)
throw BindingException("It is illegal to bind to an empty data collection");
reset();
}
~CopyBinding()
/// Destroys the Binding.
{
}
std::size_t numOfColumnsHandled() const
{
return TypeHandler<T>::size();
}
std::size_t numOfRowsHandled() const
{
return _pVal->size();
}
bool canBind() const
{
return _begin != _end;
}
void bind(std::size_t pos)
{
poco_assert_dbg(getBinder() != 0);
poco_assert_dbg(canBind());
TypeHandler<T>::bind(pos, *_begin, getBinder(), getDirection());
++_begin;
}
void reset()
{
_begin = _pVal->begin();
_end = _pVal->end();
}
private:
typedef std::vector<T> Type;
typedef SharedPtr<Type> TypePtr;
typedef typename Type::const_iterator Iterator;
TypePtr _pVal;
const Type& _val;
Iterator _begin;
Iterator _end;
};
@@ -272,8 +456,8 @@ public:
AbstractBinding(name, direction),
_val(val),
_deq(_val.begin(), _val.end()),
_begin(_deq.begin()),
_end(_deq.end())
_begin(),
_end()
/// Creates the Binding.
{
if (PD_IN != direction)
@@ -281,6 +465,7 @@ public:
if (numOfRowsHandled() == 0)
throw BindingException("It is illegal to bind to an empty data collection");
reset();
}
~Binding()
@@ -326,6 +511,82 @@ private:
};
template <>
class CopyBinding<std::vector<bool> >: public AbstractBinding
/// Specialization for std::vector<bool>.
/// This specialization is necessary due to the nature of std::vector<bool>.
/// For details, see the standard library implementation of std::vector<bool>
/// or
/// S. Meyers: "Effective STL" (Copyright Addison-Wesley 2001),
/// Item 18: "Avoid using vector<bool>."
///
/// The workaround employed here is using std::deque<bool> as an
/// internal replacement container.
///
/// IMPORTANT:
/// Only IN binding is supported.
{
public:
explicit CopyBinding(const std::vector<bool>& val,
const std::string& name = "",
Direction direction = PD_IN):
AbstractBinding(name, direction),
_deq(val.begin(), val.end()),
_begin(),
_end()
/// Creates the Binding.
{
if (PD_IN != direction)
throw BindingException("Only IN direction is legal for std:vector<bool> binding.");
if (numOfRowsHandled() == 0)
throw BindingException("It is illegal to bind to an empty data collection");
reset();
}
~CopyBinding()
/// Destroys the Binding.
{
}
std::size_t numOfColumnsHandled() const
{
return 1u;
}
std::size_t numOfRowsHandled() const
{
return _deq.size();
}
bool canBind() const
{
return _begin != _end;
}
void bind(std::size_t pos)
{
poco_assert_dbg(getBinder() != 0);
poco_assert_dbg(canBind());
TypeHandler<bool>::bind(pos, *_begin, getBinder(), getDirection());
++_begin;
}
void reset()
{
_begin = _deq.begin();
_end = _deq.end();
}
private:
std::deque<bool> _deq;
std::deque<bool>::const_iterator _begin;
std::deque<bool>::const_iterator _end;
};
template <class T>
class Binding<std::list<T> >: public AbstractBinding
/// Specialization for std::list.
@@ -333,17 +594,16 @@ class Binding<std::list<T> >: public AbstractBinding
public:
explicit Binding(std::list<T>& val,
const std::string& name = "",
Direction direction = PD_IN,
bool copy = false):
Direction direction = PD_IN):
AbstractBinding(name, direction),
_pVal(copy ? new std::list<T>(val) : 0),
_val(copy ? *_pVal : val),
_begin(_val.begin()),
_end(_val.end())
_val(val),
_begin(),
_end()
/// Creates the Binding.
{
if (PD_IN == direction && numOfRowsHandled() == 0)
throw BindingException("It is illegal to bind to an empty data collection");
reset();
}
~Binding()
@@ -380,13 +640,75 @@ public:
_end = _val.end();
}
private:
typedef std::list<T> Type;
typedef typename Type::const_iterator Iterator;
const Type& _val;
Iterator _begin;
Iterator _end;
};
template <class T>
class CopyBinding<std::list<T> >: public AbstractBinding
/// Specialization for std::list.
{
public:
explicit CopyBinding(std::list<T>& val,
const std::string& name = "",
Direction direction = PD_IN):
AbstractBinding(name, direction),
_pVal(new std::list<T>(val))
_begin(),
_end()
/// Creates the Binding.
{
if (PD_IN == direction && numOfRowsHandled() == 0)
throw BindingException("It is illegal to bind to an empty data collection");
reset();
}
~CopyBinding()
/// Destroys the Binding.
{
}
std::size_t numOfColumnsHandled() const
{
return TypeHandler<T>::size();
}
std::size_t numOfRowsHandled() const
{
return _pVal->size();
}
bool canBind() const
{
return _begin != _end;
}
void bind(std::size_t pos)
{
poco_assert_dbg(getBinder() != 0);
poco_assert_dbg(canBind());
TypeHandler<T>::bind(pos, *_begin, getBinder(), getDirection());
++_begin;
}
void reset()
{
_begin = _pVal->begin();
_end = _pVal->end();
}
private:
typedef std::list<T> Type;
typedef SharedPtr<Type> TypePtr;
typedef typename Type::const_iterator Iterator;
TypePtr _pVal;
const Type& _val;
Iterator _begin;
Iterator _end;
};
@@ -399,17 +721,16 @@ class Binding<std::deque<T> >: public AbstractBinding
public:
explicit Binding(std::deque<T>& val,
const std::string& name = "",
Direction direction = PD_IN,
bool copy = false):
Direction direction = PD_IN):
AbstractBinding(name, direction),
_pVal(copy ? new std::deque<T>(val) : 0),
_val(copy ? *_pVal : val),
_begin(_val.begin()),
_end(_val.end())
_val(val),
_begin(),
_end()
/// Creates the Binding.
{
if (PD_IN == direction && numOfRowsHandled() == 0)
throw BindingException("It is illegal to bind to an empty data collection");
reset();
}
~Binding()
@@ -446,13 +767,75 @@ public:
_end = _val.end();
}
private:
typedef std::deque<T> Type;
typedef typename Type::const_iterator Iterator;
const Type& _val;
Iterator _begin;
Iterator _end;
};
template <class T>
class CopyBinding<std::deque<T> >: public AbstractBinding
/// Specialization for std::deque.
{
public:
explicit CopyBinding(std::deque<T>& val,
const std::string& name = "",
Direction direction = PD_IN):
AbstractBinding(name, direction),
_pVal(new std::deque<T>(val)),
_begin(),
_end()
/// Creates the Binding.
{
if (PD_IN == direction && numOfRowsHandled() == 0)
throw BindingException("It is illegal to bind to an empty data collection");
reset();
}
~CopyBinding()
/// Destroys the Binding.
{
}
std::size_t numOfColumnsHandled() const
{
return TypeHandler<T>::size();
}
std::size_t numOfRowsHandled() const
{
return _pVal->size();
}
bool canBind() const
{
return _begin != _end;
}
void bind(std::size_t pos)
{
poco_assert_dbg(getBinder() != 0);
poco_assert_dbg(canBind());
TypeHandler<T>::bind(pos, *_begin, getBinder(), getDirection());
++_begin;
}
void reset()
{
_begin = _pVal->begin();
_end = _pVal->end();
}
private:
typedef std::deque<T> Type;
typedef SharedPtr<Type> TypePtr;
typedef typename Type::const_iterator Iterator;
TypePtr _pVal;
const Type& _val;
Iterator _begin;
Iterator _end;
};
@@ -465,17 +848,16 @@ class Binding<std::set<T> >: public AbstractBinding
public:
explicit Binding(std::set<T>& val,
const std::string& name = "",
Direction direction = PD_IN,
bool copy = false):
Direction direction = PD_IN):
AbstractBinding(name, direction),
_pVal(copy ? new std::set<T>(val) : 0),
_val(copy ? *_pVal : val),
_begin(_val.begin()),
_end(_val.end())
_val(val),
_begin(),
_end()
/// Creates the Binding.
{
if (PD_IN == direction && numOfRowsHandled() == 0)
throw BindingException("It is illegal to bind to an empty data collection");
reset();
}
~Binding()
@@ -512,13 +894,75 @@ public:
_end = _val.end();
}
private:
typedef std::set<T> Type;
typedef typename Type::const_iterator Iterator;
const Type& _val;
Iterator _begin;
Iterator _end;
};
template <class T>
class CopyBinding<std::set<T> >: public AbstractBinding
/// Specialization for std::set.
{
public:
explicit CopyBinding(std::set<T>& val,
const std::string& name = "",
Direction direction = PD_IN):
AbstractBinding(name, direction),
_pVal(new std::set<T>(val)),
_begin(),
_end()
/// Creates the Binding.
{
if (PD_IN == direction && numOfRowsHandled() == 0)
throw BindingException("It is illegal to bind to an empty data collection");
reset();
}
~CopyBinding()
/// Destroys the Binding.
{
}
std::size_t numOfColumnsHandled() const
{
return TypeHandler<T>::size();
}
std::size_t numOfRowsHandled() const
{
return _pVal->size();
}
bool canBind() const
{
return _begin != _end;
}
void bind(std::size_t pos)
{
poco_assert_dbg(getBinder() != 0);
poco_assert_dbg(canBind());
TypeHandler<T>::bind(pos, *_begin, getBinder(), getDirection());
++_begin;
}
void reset()
{
_begin = _pVal->begin();
_end = _pVal->end();
}
private:
typedef std::set<T> Type;
typedef SharedPtr<Type> TypePtr;
typedef typename Type::const_iterator Iterator;
TypePtr _pVal;
const Type& _val;
Iterator _begin;
Iterator _end;
};
@@ -531,17 +975,16 @@ class Binding<std::multiset<T> >: public AbstractBinding
public:
explicit Binding(std::multiset<T>& val,
const std::string& name = "",
Direction direction = PD_IN,
bool copy = false):
Direction direction = PD_IN):
AbstractBinding(name, direction),
_pVal(copy ? new std::multiset<T>(val) : 0),
_val(copy ? *_pVal : val),
_begin(_val.begin()),
_end(_val.end())
_val(val),
_begin(),
_end()
/// Creates the Binding.
{
if (PD_IN == direction && numOfRowsHandled() == 0)
throw BindingException("It is illegal to bind to an empty data collection");
reset();
}
~Binding()
@@ -578,13 +1021,75 @@ public:
_end = _val.end();
}
private:
typedef std::multiset<T> Type;
typedef typename Type::const_iterator Iterator;
const Type& _val;
Iterator _begin;
Iterator _end;
};
template <class T>
class CopyBinding<std::multiset<T> >: public AbstractBinding
/// Specialization for std::multiset.
{
public:
explicit CopyBinding(std::multiset<T>& val,
const std::string& name = "",
Direction direction = PD_IN):
AbstractBinding(name, direction),
_pVal(new std::multiset<T>(val)),
_begin(),
_end()
/// Creates the Binding.
{
if (PD_IN == direction && numOfRowsHandled() == 0)
throw BindingException("It is illegal to bind to an empty data collection");
reset();
}
~CopyBinding()
/// Destroys the Binding.
{
}
std::size_t numOfColumnsHandled() const
{
return TypeHandler<T>::size();
}
std::size_t numOfRowsHandled() const
{
return _pVal->size();
}
bool canBind() const
{
return _begin != _end;
}
void bind(std::size_t pos)
{
poco_assert_dbg(getBinder() != 0);
poco_assert_dbg(canBind());
TypeHandler<T>::bind(pos, *_begin, getBinder(), getDirection());
++_begin;
}
void reset()
{
_begin = _pVal->begin();
_end = _pVal->end();
}
private:
typedef std::multiset<T> Type;
typedef SharedPtr<Type> TypePtr;
typedef typename Type::const_iterator Iterator;
TypePtr _pVal;
const Type& _val;
Iterator _begin;
Iterator _end;
};
@@ -597,17 +1102,16 @@ class Binding<std::map<K, V> >: public AbstractBinding
public:
explicit Binding(std::map<K, V>& val,
const std::string& name = "",
Direction direction = PD_IN,
bool copy = false):
Direction direction = PD_IN):
AbstractBinding(name, direction),
_pVal(copy ? new std::map<K, V>(val) : 0),
_val(copy ? *_pVal : val),
_begin(_val.begin()),
_end(_val.end())
_val(val),
_begin(),
_end()
/// Creates the Binding.
{
if (PD_IN == direction && numOfRowsHandled() == 0)
throw BindingException("It is illegal to bind to an empty data collection");
reset();
}
~Binding()
@@ -644,13 +1148,75 @@ public:
_end = _val.end();
}
private:
typedef std::map<K, V> Type;
typedef typename Type::const_iterator Iterator;
const Type& _val;
Iterator _begin;
Iterator _end;
};
template <class K, class V>
class CopyBinding<std::map<K, V> >: public AbstractBinding
/// Specialization for std::map.
{
public:
explicit CopyBinding(std::map<K, V>& val,
const std::string& name = "",
Direction direction = PD_IN):
AbstractBinding(name, direction),
_pVal(new std::map<K, V>(val)),
_begin(),
_end()
/// Creates the Binding.
{
if (PD_IN == direction && numOfRowsHandled() == 0)
throw BindingException("It is illegal to bind to an empty data collection");
reset();
}
~CopyBinding()
/// Destroys the Binding.
{
}
std::size_t numOfColumnsHandled() const
{
return TypeHandler<V>::size();
}
std::size_t numOfRowsHandled() const
{
return _pVal->size();
}
bool canBind() const
{
return _begin != _end;
}
void bind(std::size_t pos)
{
poco_assert_dbg(getBinder() != 0);
poco_assert_dbg(canBind());
TypeHandler<V>::bind(pos, _begin->second, getBinder(), getDirection());
++_begin;
}
void reset()
{
_begin = _pVal->begin();
_end = _pVal->end();
}
private:
typedef std::map<K, V> Type;
typedef SharedPtr<Type> TypePtr;
typedef typename Type::const_iterator Iterator;
TypePtr _pVal;
const Type& _val;
Iterator _begin;
Iterator _end;
};
@@ -663,17 +1229,16 @@ class Binding<std::multimap<K, V> >: public AbstractBinding
public:
explicit Binding(std::multimap<K, V>& val,
const std::string& name = "",
Direction direction = PD_IN,
bool copy = false):
Direction direction = PD_IN):
AbstractBinding(name, direction),
_pVal(copy ? new std::multimap<K, V>(val) : 0),
_val(copy ? *_pVal : val),
_begin(_val.begin()),
_end(_val.end())
_val(val),
_begin(),
_end()
/// Creates the Binding.
{
if (PD_IN == direction && numOfRowsHandled() == 0)
throw BindingException("It is illegal to bind to an empty data collection");
reset();
}
~Binding()
@@ -710,13 +1275,75 @@ public:
_end = _val.end();
}
private:
typedef std::multimap<K, V> Type;
typedef typename Type::const_iterator Iterator;
const Type& _val;
Iterator _begin;
Iterator _end;
};
template <class K, class V>
class CopyBinding<std::multimap<K, V> >: public AbstractBinding
/// Specialization for std::multimap.
{
public:
explicit CopyBinding(std::multimap<K, V>& val,
const std::string& name = "",
Direction direction = PD_IN):
AbstractBinding(name, direction),
_pVal(new std::multimap<K, V>(val)),
_begin(),
_end()
/// Creates the Binding.
{
if (PD_IN == direction && numOfRowsHandled() == 0)
throw BindingException("It is illegal to bind to an empty data collection");
reset();
}
~CopyBinding()
/// Destroys the Binding.
{
}
std::size_t numOfColumnsHandled() const
{
return TypeHandler<V>::size();
}
std::size_t numOfRowsHandled() const
{
return _pVal->size();
}
bool canBind() const
{
return _begin != _end;
}
void bind(std::size_t pos)
{
poco_assert_dbg(getBinder() != 0);
poco_assert_dbg(canBind());
TypeHandler<V>::bind(pos, _begin->second, getBinder(), getDirection());
++_begin;
}
void reset()
{
_begin = _pVal->begin();
_end = _pVal->end();
}
private:
typedef std::multimap<K, V> Type;
typedef SharedPtr<Type> TypePtr;
typedef typename Type::const_iterator Iterator;
TypePtr _pVal;
const Type& _val;
Iterator _begin;
Iterator _end;
};
@@ -800,16 +1427,16 @@ inline AbstractBindingVec& io(AbstractBindingVec& bv)
template <typename T>
inline Binding<T>* bind(T t, const std::string& name)
inline CopyBinding<T>* bind(T t, const std::string& name)
/// Convenience function for a more compact Binding creation.
/// This funtion differs from use() in its value copy semantics.
{
return new Binding<T>(t, name, AbstractBinding::PD_IN, true);
return new CopyBinding<T>(t, name, AbstractBinding::PD_IN);
}
template <typename T>
inline Binding<T>* bind(T t)
inline CopyBinding<T>* bind(T t)
/// Convenience function for a more compact Binding creation.
/// This funtion differs from use() in its value copy semantics.
{