From 49dfa4d9d00cdae2d3cb69e40c179c84f1f83f8e Mon Sep 17 00:00:00 2001 From: Elyas El Idrissi Date: Thu, 6 Apr 2023 12:32:19 +0200 Subject: [PATCH] Add support for std::unique_ptr and std::shared_ptr --- include/soci/std-ptr.h | 84 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 include/soci/std-ptr.h diff --git a/include/soci/std-ptr.h b/include/soci/std-ptr.h new file mode 100644 index 000000000..3c4852720 --- /dev/null +++ b/include/soci/std-ptr.h @@ -0,0 +1,84 @@ +// +// Copyright (C) 2023 Elyas El Idrissi +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// + +#ifndef SOCI_STD_PTR_H_INCLUDED +#define SOCI_STD_PTR_H_INCLUDED + +#include "soci/type-conversion-traits.h" + +#include + +namespace soci +{ + // simple fall-back for std::unique_ptr + template + struct type_conversion> + { + typedef typename type_conversion::base_type base_type; + + static void from_base(base_type const & in, indicator ind, std::unique_ptr & out) + { + if (ind == i_null) + { + out.reset(); + } + else + { + T tmp = T(); + type_conversion::from_base(in, ind, tmp); + out = std::make_unique(tmp); + } + } + + static void to_base(std::unique_ptr const & in, base_type & out, indicator & ind) + { + if (in) + { + type_conversion::to_base(*in, out, ind); + } + else + { + ind = i_null; + } + } + }; + + // simple fall-back for std::shared_ptr + template + struct type_conversion > + { + typedef typename type_conversion::base_type base_type; + + static void from_base(base_type const & in, indicator ind, std::shared_ptr & out) + { + if (ind == i_null) + { + out.reset(); + } + else + { + T tmp = T(); + type_conversion::from_base(in, ind, tmp); + out = std::make_shared(tmp); + } + } + + static void to_base(std::shared_ptr const & in, base_type & out, indicator & ind) + { + if (in) + { + type_conversion::to_base(*in, out, ind); + } + else + { + ind = i_null; + } + } + }; +} + +#endif // SOCI_STD_PTR_H_INCLUDED