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