-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for std::unique_ptr and std::shared_ptr
- Loading branch information
Elyas El Idrissi
committed
Apr 6, 2023
1 parent
827bb5c
commit 4880523
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// | ||
// 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 <memory> | ||
|
||
namespace soci | ||
{ | ||
// simple fall-back for std::unique_ptr | ||
template <typename T> | ||
struct type_conversion<std::unique_ptr<T>> | ||
{ | ||
typedef typename type_conversion<T>::base_type base_type; | ||
|
||
static void from_base(base_type const & in, indicator ind, std::unique_ptr<T> & out) | ||
{ | ||
if (ind == i_null) | ||
{ | ||
out.reset(); | ||
} | ||
else | ||
{ | ||
T tmp = T(); | ||
type_conversion<T>::from_base(in, ind, tmp); | ||
out = std::make_unique<T>(tmp); | ||
} | ||
} | ||
|
||
static void to_base(std::unique_ptr<T> const & in, base_type & out, indicator & ind) | ||
{ | ||
if (in) | ||
{ | ||
type_conversion<T>::to_base(*in, out, ind); | ||
} | ||
else | ||
{ | ||
ind = i_null; | ||
} | ||
} | ||
}; | ||
|
||
// simple fall-back for std::shared_ptr | ||
template <typename T> | ||
struct type_conversion<std::shared_ptr<T> > | ||
{ | ||
typedef typename type_conversion<T>::base_type base_type; | ||
|
||
static void from_base(base_type const & in, indicator ind, | ||
std::shared_ptr<T> & out) | ||
{ | ||
if (ind == i_null) | ||
{ | ||
out.reset(); | ||
} | ||
else | ||
{ | ||
T tmp = T(); | ||
type_conversion<T>::from_base(in, ind, tmp); | ||
out = std::make_shared<T>(tmp); | ||
} | ||
} | ||
|
||
static void to_base(std::shared_ptr<T> const & in, base_type & out, indicator & ind) | ||
{ | ||
if (in) | ||
{ | ||
type_conversion<T>::to_base(*in, out, ind); | ||
} | ||
else | ||
{ | ||
ind = i_null; | ||
} | ||
} | ||
}; | ||
} | ||
|
||
#endif // SOCI_STD_PTR_H_INCLUDED |