Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions include/soci/bool.h
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;
Copy link
Member

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.

Copy link
Contributor

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.

Copy link
Author

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

C++ bool <-> (internally) C++ short (or maybe int ?) <-> SQL bool (or equivalent)

bool value1 = true;	// or a model with a bool member
bool value2{}; 		// ...

connection << "DELETE FROM AAA WHERE value1 = :value1 RETURNING value2", use(value1), into(value2);

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).


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
3 changes: 3 additions & 0 deletions include/soci/soci.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "soci/use-type.h"
#include "soci/values.h"
#include "soci/values-exchange.h"
#include "soci/bool.h"
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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
Expand All @@ -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"
Copy link
Member

Choose a reason for hiding this comment

The 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 std::optional but was convinced to add it here because boost::optional had been already included, but pulling in std::fs into all files using SOCI really doesn't seem right.

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.

Copy link
Author

Choose a reason for hiding this comment

The 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
39 changes: 39 additions & 0 deletions include/soci/std-path.h
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
84 changes: 84 additions & 0 deletions include/soci/std-ptr.h
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> >
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're not using C++98 any more.

Suggested change
struct type_conversion<std::shared_ptr<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