diff --git a/include/soci/bool.h b/include/soci/bool.h new file mode 100644 index 000000000..fc501b318 --- /dev/null +++ b/include/soci/bool.h @@ -0,0 +1,36 @@ +// +// 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_BOOL_H_INCLUDED +#define SOCI_BOOL_H_INCLUDED + +#include "soci/type-conversion-traits.h" + +namespace soci +{ + +// simple fall-back for bool +template <> +struct type_conversion +{ + typedef short base_type; + + static void from_base(base_type in, indicator ind, bool & out) + { + out = ind != i_null ? static_cast(in) : false; + } + + static void to_base(bool in, base_type & out, indicator & ind) + { + out = static_cast(in); + ind = i_ok; + } +}; + +} // namespace soci + +#endif // SOCI_BOOL_H_INCLUDED diff --git a/include/soci/soci.h b/include/soci/soci.h index 628cd6e7e..611af3af3 100644 --- a/include/soci/soci.h +++ b/include/soci/soci.h @@ -43,6 +43,7 @@ #include "soci/use-type.h" #include "soci/values.h" #include "soci/values-exchange.h" +#include "soci/bool.h" // namespace boost #ifdef SOCI_USE_BOOST @@ -58,6 +59,8 @@ // C++17 #ifdef SOCI_HAVE_CXX17 #include "soci/std-optional.h" +#include "soci/std-ptr.h" +#include "soci/std-path.h" #endif // SOCI_HAVE_CXX17 #endif // SOCI_H_INCLUDED diff --git a/include/soci/std-path.h b/include/soci/std-path.h new file mode 100644 index 000000000..7351e856d --- /dev/null +++ b/include/soci/std-path.h @@ -0,0 +1,39 @@ +// +// 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_PATH_H_INCLUDED +#define SOCI_STD_PATH_H_INCLUDED + +#include "soci/type-conversion-traits.h" + +#include +#include + +namespace soci +{ + +// simple fall-back for std::filesystem::path +template <> +struct type_conversion +{ + typedef std::string base_type; + + static void from_base(base_type in, indicator ind, std::filesystem::path & out) + { + out = ind != i_null ? std::filesystem::path{in} : std::filesystem::path{}; + } + + static void to_base(std::filesystem::path in, base_type & out, indicator & ind) + { + out = in.string(); + ind = i_ok; + } +}; + +} // namespace soci + +#endif // SOCI_STD_PATH_H_INCLUDED 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