From 7082f3a11bcbba197b8fbc3c825b58b8b3875345 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 4 Aug 2011 13:29:42 +0200 Subject: Add support for value wrappers Wrapper is a class that wraps another type. Examples of wrappers are various smart pointers, holders, etc. A wrapper can be transparent or it can handle the NULL semantics. The new odb::nullable class template is a NULL wrapper that helps to add the NULL semantics to a value type. New test: common/wrapper. --- odb/sqlite/traits.hxx | 119 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 118 insertions(+), 1 deletion(-) diff --git a/odb/sqlite/traits.hxx b/odb/sqlite/traits.hxx index 8863fb1..e2a4a4d 100644 --- a/odb/sqlite/traits.hxx +++ b/odb/sqlite/traits.hxx @@ -13,7 +13,10 @@ #include // std::size_t #include +#include + #include +#include #include #include @@ -53,12 +56,126 @@ namespace odb // value_traits // + template + struct wrapped_value_traits; + template struct default_value_traits; + template ::r> + struct select_traits; + + template + struct select_traits + { + typedef default_value_traits type; + }; + + template + struct select_traits + { + typedef + wrapped_value_traits::null_handler> + type; + }; + template - class value_traits: public default_value_traits + class value_traits: public select_traits::type + { + }; + + // The wrapped_value_traits specializations should be able to handle + // any value type which means we have to have every possible signature + // of the set_value() and set_image() functions. + // + template + struct wrapped_value_traits { + typedef wrapper_traits wtraits; + typedef typename wtraits::wrapped_type wrapped_type; + + typedef W value_type; + typedef wrapped_type query_type; + typedef typename image_traits::image_type image_type; + + typedef value_traits vtraits; + + static void + set_value (W& v, const image_type& i, bool is_null) + { + vtraits::set_value (wtraits::set_ref (v), i, is_null); + } + + static void + set_image (image_type& i, bool& is_null, const W& v) + { + vtraits::set_image (i, is_null, wtraits::get_ref (v)); + } + + // TEXT and BLOB. + // + static void + set_value (W& v, const details::buffer& b, std::size_t n, bool is_null) + { + vtraits::set_value (wtraits::set_ref (v), b, n, is_null); + } + + static void + set_image (details::buffer& b, std::size_t& n, bool& is_null, const W& v) + { + vtraits::set_image (b, n, is_null, wtraits::get_ref (v)); + } + }; + + template + struct wrapped_value_traits + { + typedef wrapper_traits wtraits; + typedef typename wtraits::wrapped_type wrapped_type; + + typedef W value_type; + typedef wrapped_type query_type; + typedef typename image_traits::image_type image_type; + + typedef value_traits vtraits; + + static void + set_value (W& v, const image_type& i, bool is_null) + { + if (is_null) + wtraits::set_null (v); + else + vtraits::set_value (wtraits::set_ref (v), i, is_null); + } + + static void + set_image (image_type& i, bool& is_null, const W& v) + { + is_null = wtraits::get_null (v); + + if (!is_null) + vtraits::set_image (i, is_null, wtraits::get_ref (v)); + } + + // TEXT and BLOB. + // + static void + set_value (W& v, const details::buffer& b, std::size_t n, bool is_null) + { + if (is_null) + wtraits::set_null (v); + else + vtraits::set_value (wtraits::set_ref (v), b, n, is_null); + } + + static void + set_image (details::buffer& b, std::size_t& n, bool& is_null, const W& v) + { + is_null = wtraits::get_null (v); + + if (!is_null) + vtraits::set_image (b, n, is_null, wtraits::get_ref (v)); + } }; template -- cgit v1.1