-
Notifications
You must be signed in to change notification settings - Fork 478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for bool, std::filesystem::path, std::unique_ptr, std::shared_ptr #1043
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<bool> | ||
{ | ||
typedef short base_type; | ||
|
||
static void from_base(base_type in, indicator ind, bool & out) | ||
{ | ||
out = ind != i_null ? static_cast<bool>(in) : false; | ||
} | ||
|
||
static void to_base(bool in, base_type & out, indicator & ind) | ||
{ | ||
out = static_cast<base_type>(in); | ||
ind = i_ok; | ||
} | ||
}; | ||
|
||
} // namespace soci | ||
|
||
#endif // SOCI_BOOL_H_INCLUDED |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ | |
#include "soci/use-type.h" | ||
#include "soci/values.h" | ||
#include "soci/values-exchange.h" | ||
#include "soci/bool.h" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The headers here are sorted in alphabetical order except the very first one which needs to come first, so this line should be moved above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok thanks, I will make the change. |
||
|
||
// 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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to draw a line somewhere and not include all standard library headers from here. I was already not sure about Maybe we could have an opt-in way to do it, but the simplest would be to just provide the header and document that it should be included explicitly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I understand. In that case, maybe I should only include the headers for smart pointers? Because even though both smart pointers and std::filesystem::path are "convenient objects", std::filesystem::path seems to me more optional and project-specific than smart pointers. |
||
#endif // SOCI_HAVE_CXX17 | ||
|
||
#endif // SOCI_H_INCLUDED |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <filesystem> | ||
#include <string> | ||
|
||
namespace soci | ||
{ | ||
|
||
// simple fall-back for std::filesystem::path | ||
template <> | ||
struct type_conversion<std::filesystem::path> | ||
{ | ||
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 |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -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 <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> > | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're not using C++98 any more.
Suggested change
|
||||||
{ | ||||||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm really not sure about this one, there is
boolean
SQL type and I think we ought to use it instead of storing bool as short.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like adding this specialization creates more confusion than anything else. At least the postgres backend has a mapping from its SQL boolean type to a C++ int type already.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The objective behind this addition was to be able to use the C++ bool type directly in my model and use it with soci
However, using a type_conversion specialization for this purpose might not be the most appropriate approach after all (I'll try to integrate bool support directly into SOCI, like other primitive types already are).