Get rid of soci/fmt.h hack

Provide soci_error constructors taking a format string and an argument
instead.

This is still unsafe, because we lose the compile-time check of the
format string, but is slightly less ugly than defining format() in our
own namespace.
This commit is contained in:
Vadim Zeitlin
2026-03-16 00:38:18 +01:00
parent b5855d1d67
commit e61ac187e1
7 changed files with 27 additions and 49 deletions

View File

@@ -22,6 +22,12 @@ class SOCI_DECL soci_error : public std::runtime_error
public:
explicit soci_error(std::string const & msg);
// Provide a simple way to construct the message from a format (using fmt
// syntax, i.e. with {} placeholders) and one argument of the given type.
soci_error(char const* format, std::string const& arg);
soci_error(char const* format, int arg);
soci_error(char const* format, size_t arg);
soci_error(soci_error const& e) noexcept;
soci_error& operator=(soci_error const& e) noexcept;

View File

@@ -1,24 +0,0 @@
//
// Copyright (C) 2026 Vadim Zeitlin
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
//
#ifndef SOCI_FMT_H_INCLUDED
#define SOCI_FMT_H_INCLUDED
#include <string>
namespace soci
{
// Export a couple of overloads of format() used in public headers to allow
// using them there without requiring including fmt headers.
std::string SOCI_DECL format(const char* fmt, std::string const& arg);
std::string SOCI_DECL format(const char* fmt, int arg);
std::string SOCI_DECL format(const char* fmt, size_t arg);
} // namespace soci
#endif // SOCI_FMT_H_INCLUDED

View File

@@ -10,7 +10,6 @@
#include "soci/blob.h"
#include "soci/error.h"
#include "soci/fmt.h"
#include "soci/soci-backend.h"
#include "soci/soci-types.h"
@@ -486,7 +485,7 @@ private:
}
// This should be unreachable
throw soci_error(format("Created holder with unsupported type {}", static_cast<int>(dt)));
throw soci_error("Created holder with unsupported type {}", static_cast<int>(dt));
}
const db_type dt_;

View File

@@ -11,7 +11,6 @@
#include "soci/statement.h"
#include "soci/into-type.h"
#include "soci/use-type.h"
#include "soci/fmt.h"
// std
#include <cstddef>
#include <map>
@@ -67,7 +66,7 @@ public:
}
else
{
throw soci_error(format("Column at position {} contains NULL value and no default was provided", pos));
throw soci_error("Column at position {} contains NULL value and no default was provided", pos);
}
}
@@ -124,7 +123,7 @@ public:
}
else
{
throw soci_error(format("Column at position {} contains NULL value and no default was provided", currentPos_));
throw soci_error("Column at position {} contains NULL value and no default was provided", currentPos_);
}
return *this;
@@ -290,7 +289,7 @@ private:
}
else
{
throw soci_error(format("Value at position {} was set using a different type than the one passed to get()", pos));
throw soci_error("Value at position {} was set using a different type than the one passed to get()", pos);
}
}

View File

@@ -72,7 +72,6 @@ target_sources(soci_core
"${PROJECT_SOURCE_DIR}/include/soci/error.h"
"${PROJECT_SOURCE_DIR}/include/soci/exchange-traits.h"
"${PROJECT_SOURCE_DIR}/include/soci/fixed-size-ints.h"
"${PROJECT_SOURCE_DIR}/include/soci/fmt.h"
"${PROJECT_SOURCE_DIR}/include/soci/into-type.h"
"${PROJECT_SOURCE_DIR}/include/soci/into.h"
"${PROJECT_SOURCE_DIR}/include/soci/is-detected.h"

View File

@@ -7,14 +7,11 @@
//
#include "soci/error.h"
#include "soci/fmt.h"
#include "soci-mktime.h"
#include <climits>
#include <cstdlib>
#include <ctime>
#include <fmt/format.h>
namespace // anonymous
{
@@ -140,18 +137,3 @@ time_t soci::details::timegm_impl_soci ( struct tm* tb )
return since_epoch;
}
std::string soci::format(const char* fmt, std::string const& arg)
{
return fmt::vformat(fmt, fmt::make_format_args(arg));
}
std::string soci::format(const char* fmt, int arg)
{
return fmt::vformat(fmt, fmt::make_format_args(arg));
}
std::string soci::format(const char* fmt, size_t arg)
{
return fmt::vformat(fmt, fmt::make_format_args(arg));
}

View File

@@ -10,6 +10,8 @@
#include <vector>
#include <fmt/format.h>
namespace soci
{
@@ -97,6 +99,21 @@ soci_error::soci_error(std::string const & msg)
{
}
soci_error::soci_error(char const* format, std::string const& arg)
: std::runtime_error(fmt::vformat(format, fmt::make_format_args(arg)))
{
}
soci_error::soci_error(char const* format, int arg)
: std::runtime_error(fmt::vformat(format, fmt::make_format_args(arg)))
{
}
soci_error::soci_error(char const* format, size_t arg)
: std::runtime_error(fmt::vformat(format, fmt::make_format_args(arg)))
{
}
soci_error::soci_error(soci_error const& e) noexcept
: std::runtime_error(e)
{