From 9abf73c8180e3664a71bac73629f67851c6c12da Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 28 Jul 2011 09:45:36 +0200 Subject: Add value_traits specializations for std::vector This allows using it as a buffer for BLOB mapping. --- odb/pgsql/traits.cxx | 22 ++++++++++++++++++++++ odb/pgsql/traits.hxx | 30 ++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/odb/pgsql/traits.cxx b/odb/pgsql/traits.cxx index bdffa45..1c167b8 100644 --- a/odb/pgsql/traits.cxx +++ b/odb/pgsql/traits.cxx @@ -54,5 +54,27 @@ namespace odb if (n != 0) memcpy (b.data (), v, n); } + + // + // default_value_traits, id_bytea> + // + + void default_value_traits, id_bytea>:: + set_image (details::buffer& b, + size_t& n, + bool& is_null, + const value_type& v) + { + is_null = false; + n = v.size (); + + if (n > b.capacity ()) + b.capacity (n); + + // std::vector::data() may not be available in older compilers. + // + if (n != 0) + memcpy (b.data (), &v[0], n); + } } } diff --git a/odb/pgsql/traits.hxx b/odb/pgsql/traits.hxx index 7b4f746..f34a1ca 100644 --- a/odb/pgsql/traits.hxx +++ b/odb/pgsql/traits.hxx @@ -9,6 +9,7 @@ #include #include +#include #include // std::size_t #include @@ -211,6 +212,35 @@ namespace odb { }; + // std::vector (buffer) specialization. + // + template <> + struct LIBODB_PGSQL_EXPORT default_value_traits, id_bytea> + { + public: + typedef std::vector value_type; + typedef std::vector query_type; + typedef details::buffer image_type; + + static void + set_value (value_type& v, + const details::buffer& b, + std::size_t n, + bool is_null) + { + if (!is_null) + v.assign (b.data (), b.data () + n); + else + v.clear (); + } + + static void + set_image (details::buffer&, + std::size_t& n, + bool& is_null, + const value_type&); + }; + // // type_traits // -- cgit v1.1