From 748e05183a0dd33631a3d4d4c0b943dc6703f4ff Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 11 Sep 2012 13:55:47 +0200 Subject: Add support for mapping std::array to BLOB and char[16] to UUID types --- odb/sqlite/traits.hxx | 150 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 131 insertions(+), 19 deletions(-) diff --git a/odb/sqlite/traits.hxx b/odb/sqlite/traits.hxx index 0de67f9..b812361 100644 --- a/odb/sqlite/traits.hxx +++ b/odb/sqlite/traits.hxx @@ -7,12 +7,18 @@ #include +#include // ODB_CXX11 + #include #include #include // std::numeric_limits #include // std::size_t #include // std::memcpy, std::memset, std::strlen +#ifdef ODB_CXX11 +# include +#endif + #include #include @@ -303,13 +309,13 @@ namespace odb { }; - template - struct default_value_traits: c_string_value_traits + template + struct default_value_traits: c_string_value_traits { }; - template - struct default_value_traits: c_string_value_traits + template + struct default_value_traits: c_string_value_traits { }; @@ -380,20 +386,20 @@ namespace odb { }; - template - struct image_traits: c_wstring_image_traits {}; + template + struct image_traits: c_wstring_image_traits {}; - template - struct default_value_traits: + template + struct default_value_traits: c_wstring_value_traits { }; - template - struct image_traits: c_wstring_image_traits {}; + template + struct image_traits: c_wstring_image_traits {}; - template - struct default_value_traits: + template + struct default_value_traits: c_wstring_value_traits { }; @@ -463,7 +469,7 @@ namespace odb const value_type&); }; - // char[n] (buffer) specialization. + // char[N] (buffer) specialization. // template struct default_value_traits @@ -501,7 +507,7 @@ namespace odb } }; - // unsigned char[n] (buffer) specialization. + // unsigned char[N] (buffer) specialization. // template struct default_value_traits @@ -539,6 +545,84 @@ namespace odb } }; +#ifdef ODB_CXX11 + // std::array (buffer) specialization. + // + template + struct default_value_traits, id_blob> + { + public: + typedef std::array value_type; + typedef value_type 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) + std::memcpy (v.data (), b.data (), (n < N ? n : N)); + else + std::memset (v.data (), 0, N); + } + + static void + set_image (details::buffer& b, + std::size_t& n, + bool& is_null, + const value_type& v) + { + is_null = false; + n = N; + + if (n > b.capacity ()) + b.capacity (n); + + std::memcpy (b.data (), v.data (), n); + } + }; + + // std::array (buffer) specialization. + // + template + struct default_value_traits, id_blob> + { + public: + typedef std::array value_type; + typedef value_type 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) + std::memcpy (v.data (), b.data (), (n < N ? n : N)); + else + std::memset (v.data (), 0, N); + } + + static void + set_image (details::buffer& b, + std::size_t& n, + bool& is_null, + const value_type& v) + { + is_null = false; + n = N; + + if (n > b.capacity ()) + b.capacity (n); + + std::memcpy (b.data (), v.data (), n); + } + }; +#endif + // // type_traits // @@ -633,7 +717,7 @@ namespace odb static const database_type_id db_type_id = id_real; }; - // String type. + // String types. // template <> struct default_type_traits @@ -647,17 +731,45 @@ namespace odb static const database_type_id db_type_id = id_text; }; - template - struct default_type_traits + template + struct default_type_traits { static const database_type_id db_type_id = id_text; }; - template - struct default_type_traits + template + struct default_type_traits { static const database_type_id db_type_id = id_text; }; + + // Binary types. + // + template <> + struct default_type_traits > + { + static const database_type_id db_type_id = id_blob; + }; + + template <> + struct default_type_traits > + { + static const database_type_id db_type_id = id_blob; + }; + +#ifdef ODB_CXX11 + template + struct default_type_traits > + { + static const database_type_id db_type_id = id_blob; + }; + + template + struct default_type_traits > + { + static const database_type_id db_type_id = id_blob; + }; +#endif } } -- cgit v1.1