constness and modernisation of enable_if etc.

This commit is contained in:
Gulliver
2025-09-29 01:58:08 +02:00
parent 2f121069a3
commit 923d4c71c6
14 changed files with 98 additions and 94 deletions

View File

@@ -209,8 +209,7 @@ namespace crow
/// \brief An HTTP server that runs on SSL with an SSLAdaptor
using ssl_server_t = Server<Crow, TCPAcceptor, SSLAdaptor, Middlewares...>;
#endif
Crow()
{}
Crow()=default;
/// \brief Construct Crow with a subset of middleware
template<typename... Ts>
@@ -275,7 +274,7 @@ namespace crow
}
/// \brief Get the default max payload size for websockets
uint64_t websocket_max_payload()
uint64_t websocket_max_payload() const
{
return max_payload_;
}
@@ -341,14 +340,14 @@ namespace crow
}
/// \brief Set the server name included in the 'Server' HTTP response header. If set to an empty string, the header will be omitted by default.
self_t& server_name(std::string server_name)
self_t& server_name(const std::string& server_name)
{
server_name_ = server_name;
return *this;
}
/// \brief The IP address that Crow will handle requests on (default is 0.0.0.0)
self_t& bindaddr(std::string bindaddr)
self_t& bindaddr(const std::string& bindaddr)
{
bindaddr_ = bindaddr;
return *this;
@@ -361,7 +360,7 @@ namespace crow
}
/// \brief Disable tcp/ip and use unix domain socket instead
self_t& local_socket_path(std::string path)
self_t& local_socket_path(const std::string& path)
{
bindaddr_ = path;
use_unix_ = true;
@@ -617,9 +616,9 @@ namespace crow
}
}
void close_websockets()
void close_websockets() const
{
for (auto websocket : websockets_)
for (const auto websocket : websockets_)
{
CROW_LOG_INFO << "Quitting Websocket: " << websocket;
websocket->close("Websocket Closed");
@@ -805,7 +804,7 @@ namespace crow
static_routes_added_ = true;
}
bool are_static_routes_added() {
bool are_static_routes_added() const {
return static_routes_added_;
}

View File

@@ -285,7 +285,7 @@ namespace crow
routing_params r_params;
HTTPMethod method;
routing_handle_result() {}
routing_handle_result()=default;
routing_handle_result(size_t rule_index_, std::vector<size_t> blueprint_indices_, routing_params r_params_):
rule_index(rule_index_),

View File

@@ -5,10 +5,10 @@ namespace crow
{
struct bad_request : public std::runtime_error
{
bad_request(const std::string& what_arg)
explicit bad_request(const std::string& what_arg)
: std::runtime_error(what_arg) {}
bad_request(const char* what_arg)
explicit bad_request(const char* what_arg)
: std::runtime_error(what_arg) {}
};
}

View File

@@ -69,7 +69,7 @@ namespace crow
headers.emplace(std::move(key), std::move(value));
}
const std::string& get_header_value(const std::string& key)
const std::string& get_header_value(const std::string& key) const
{
return crow::get_header_value(headers, key);
}
@@ -128,15 +128,15 @@ namespace crow
// clang-format off
response() {}
explicit response(int code_) : code(code_) {}
response(std::string body_) : body(std::move(body_)) {}
response(int code_, std::string body_) : code(code_), body(std::move(body_)) {}
explicit response(std::string body_) : body(std::move(body_)) {}
explicit response(int code_, std::string body_) : code(code_), body(std::move(body_)) {}
// clang-format on
response(returnable&& value)
explicit response(returnable&& value)
{
body = value.dump();
set_header("Content-Type", value.content_type);
}
response(returnable& value)
explicit response(returnable& value)
{
body = value.dump();
set_header("Content-Type", value.content_type);
@@ -271,13 +271,13 @@ namespace crow
}
/// Check if the connection is still alive (usually by checking the socket status).
bool is_alive()
bool is_alive() const
{
return is_alive_helper_ && is_alive_helper_();
}
/// Check whether the response has a static file defined.
bool is_static_type()
bool is_static_type() const
{
return file_info.path.size();
}
@@ -294,7 +294,7 @@ namespace crow
};
/// Return a static file as the response body, the content_type may be specified explicitly.
void set_static_file_info(std::string path, std::string content_type = "")
void set_static_file_info(std::string path, const std::string& content_type = "")
{
utility::sanitize_filename(path);
set_static_file_info_unsafe(path, content_type);

View File

@@ -285,7 +285,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
}
private:
size_t pick_io_context_idx()
size_t pick_io_context_idx() const
{
size_t min_queue_idx = 0;

View File

@@ -1391,7 +1391,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
for (auto it = r.begin(); it != r.end(); ++it)
l->emplace_back(*it);
}
wvalue(list& r):
explicit wvalue(list& r):
returnable("application/json")
{
t_ = type::List;
@@ -2047,7 +2047,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
// Used for accessing the internals of a wvalue
struct wvalue_reader
{
int64_t get(int64_t fallback)
int64_t get(int64_t fallback) const
{
if (ref.t() != type::Number || ref.nt == num_type::Floating_point ||
ref.nt == num_type::Double_precision_floating_point)
@@ -2063,14 +2063,14 @@ namespace crow // NOTE: Already documented in "crow/app.h"
return ref.num.d;
}
bool get(bool fallback)
bool get(bool fallback) const
{
if (ref.t() == type::True) return true;
if (ref.t() == type::False) return false;
return fallback;
}
std::string get(const std::string& fallback)
std::string get(const std::string& fallback) const
{
if (ref.t() != type::String) return fallback;
return ref.s;

View File

@@ -105,7 +105,7 @@ namespace crow
class logger
{
public:
logger(LogLevel level):
explicit logger(LogLevel level):
level_(level)
{}
~logger()

View File

@@ -50,7 +50,7 @@ namespace crow
value_ = std::forward<U>(value);
}
Cookie(const std::string& key):
explicit Cookie(const std::string& key):
Cookie(key, "") {}
// format cookie to HTTP header format

View File

@@ -89,17 +89,17 @@ namespace crow
}
/// Handle CORS on specific prefix path
CORSRules& prefix(const std::string& prefix);
CORSRules& prefix(const std::string& prefix) const;
/// Handle CORS for specific blueprint
CORSRules& blueprint(const Blueprint& bp);
CORSRules& blueprint(const Blueprint& bp) const;
/// Global CORS policy
CORSRules& global();
CORSRules& global() const;
private:
CORSRules() = delete;
CORSRules(CORSHandler* handler):
explicit CORSRules(CORSHandler* handler):
handler_(handler) {}
/// build comma separated list
@@ -219,17 +219,17 @@ namespace crow
CORSRules default_ = CORSRules(this);
};
inline CORSRules& CORSRules::prefix(const std::string& prefix)
inline CORSRules& CORSRules::prefix(const std::string& prefix) const
{
return handler_->prefix(prefix);
}
inline CORSRules& CORSRules::blueprint(const Blueprint& bp)
inline CORSRules& CORSRules::blueprint(const Blueprint& bp) const
{
return handler_->blueprint(bp);
}
inline CORSRules& CORSRules::global()
inline CORSRules& CORSRules::global() const
{
return handler_->global();
}

View File

@@ -204,7 +204,7 @@ namespace crow
}
// Check whether this session is already present
bool exists() { return bool(node); }
bool exists() const { return bool(node); }
// Get a value by key or fallback if it doesn't exist or is of another type
template<typename F>
@@ -235,7 +235,7 @@ namespace crow
node->entries[key].set(std::move(value));
}
bool contains(const std::string& key)
bool contains(const std::string& key) const
{
if (!node) return false;
return node->entries.find(key) != node->entries.end();
@@ -275,7 +275,7 @@ namespace crow
}
// Get a list of keys present in session
std::vector<std::string> keys()
std::vector<std::string> keys() const
{
if (!node) return {};
rc_lock l(node->mutex);
@@ -288,7 +288,7 @@ namespace crow
// Delay expiration by issuing another cookie with an updated expiration time
// and notifying the store
void refresh_expiration()
void refresh_expiration() const
{
if (!node) return;
node->requested_refresh = true;

View File

@@ -54,7 +54,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
invalid_template_exception(const std::string& msg_):
msg("crow::mustache error: " + msg_)
{}
virtual const char* what() const throw() override
const char* what() const throw() override
{
return msg.c_str();
}

View File

@@ -40,7 +40,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
{
using MwContainer = typename App::mw_container_t;
static_assert(black_magic::has_type<MW, MwContainer>::value, "Middleware must be present in app");
static_assert(std::is_base_of<crow::ILocalMiddleware, MW>::value, "Middleware must extend ILocalMiddleware");
static_assert(std::is_base_of_v<crow::ILocalMiddleware, MW>, "Middleware must extend ILocalMiddleware");
int idx = black_magic::tuple_index<MW, MwContainer>::value;
indices_.push_back(idx);
push<App, Middlewares...>();
@@ -104,7 +104,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
added_ = true;
}
bool is_added()
bool is_added() const
{
return added_;
}
@@ -135,7 +135,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
}
#endif
uint32_t get_methods()
uint32_t get_methods() const
{
return methods_;
}
@@ -252,7 +252,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
struct Wrapped
{
template<typename... Args>
void set_(Func f, typename std::enable_if<!std::is_same<typename std::tuple_element<0, std::tuple<Args..., void>>::type, const request&>::value, int>::type = 0)
void set_(Func f, std::enable_if_t<!std::is_same_v<std::tuple_element_t<0, std::tuple<Args..., void>>, const request&>, int> = 0)
{
handler_ = ([f = std::move(f)](const request&, response& res, Args... args) {
res = response(f(args...));
@@ -278,10 +278,10 @@ namespace crow // NOTE: Already documented in "crow/app.h"
};
template<typename... Args>
void set_(Func f, typename std::enable_if<
std::is_same<typename std::tuple_element<0, std::tuple<Args..., void>>::type, const request&>::value &&
!std::is_same<typename std::tuple_element<1, std::tuple<Args..., void, void>>::type, response&>::value,
int>::type = 0)
void set_(Func f, std::enable_if_t<
std::is_same_v<std::tuple_element_t<0, std::tuple<Args..., void>>, const request&> &&
!std::is_same_v<std::tuple_element_t<1, std::tuple<Args..., void, void>>, response&>,
int> = 0)
{
handler_ = req_handler_wrapper<Args...>(std::move(f));
/*handler_ = (
@@ -293,10 +293,10 @@ namespace crow // NOTE: Already documented in "crow/app.h"
}
template<typename... Args>
void set_(Func f, typename std::enable_if<
std::is_same<typename std::tuple_element<0, std::tuple<Args..., void>>::type, const request&>::value &&
std::is_same<typename std::tuple_element<1, std::tuple<Args..., void, void>>::type, response&>::value,
int>::type = 0)
void set_(Func f, std::enable_if_t<
std::is_same_v<std::tuple_element_t<0, std::tuple<Args..., void>>, const request&> &&
std::is_same_v<std::tuple_element_t<1, std::tuple<Args..., void, void>>, response&>,
int> = 0)
{
handler_ = std::move(f);
}
@@ -305,21 +305,21 @@ namespace crow // NOTE: Already documented in "crow/app.h"
struct handler_type_helper
{
using type = std::function<void(const crow::request&, crow::response&, Args...)>;
using args_type = black_magic::S<typename black_magic::promote_t<Args>...>;
using args_type = black_magic::S<black_magic::promote_t<Args>...>;
};
template<typename... Args>
struct handler_type_helper<const request&, Args...>
{
using type = std::function<void(const crow::request&, crow::response&, Args...)>;
using args_type = black_magic::S<typename black_magic::promote_t<Args>...>;
using args_type = black_magic::S<black_magic::promote_t<Args>...>;
};
template<typename... Args>
struct handler_type_helper<const request&, response&, Args...>
{
using type = std::function<void(const crow::request&, crow::response&, Args...)>;
using args_type = black_magic::S<typename black_magic::promote_t<Args>...>;
using args_type = black_magic::S<black_magic::promote_t<Args>...>;
};
typename handler_type_helper<ArgsWrapped...>::type handler_;
@@ -348,10 +348,10 @@ namespace crow // NOTE: Already documented in "crow/app.h"
CatchallRule() {}
template<typename Func>
typename std::enable_if<black_magic::CallHelper<Func, black_magic::S<>>::value, void>::type
std::enable_if_t<black_magic::CallHelper<Func, black_magic::S<>>::value, void>
operator()(Func&& f)
{
static_assert(!std::is_same<void, decltype(f())>::value,
static_assert(!std::is_same_v<void, decltype(f())>,
"Handler function cannot have void return type; valid return types: string, int, crow::response, crow::returnable");
handler_ = ([f = std::move(f)](const request&, response& res) {
@@ -361,13 +361,13 @@ namespace crow // NOTE: Already documented in "crow/app.h"
}
template<typename Func>
typename std::enable_if<
std::enable_if_t<
!black_magic::CallHelper<Func, black_magic::S<>>::value &&
black_magic::CallHelper<Func, black_magic::S<crow::request>>::value,
void>::type
void>
operator()(Func&& f)
{
static_assert(!std::is_same<void, decltype(f(std::declval<crow::request>()))>::value,
static_assert(!std::is_same_v<void, decltype(f(std::declval<crow::request>()))>,
"Handler function cannot have void return type; valid return types: string, int, crow::response, crow::returnable");
handler_ = ([f = std::move(f)](const request& req, response& res) {
@@ -377,14 +377,14 @@ namespace crow // NOTE: Already documented in "crow/app.h"
}
template<typename Func>
typename std::enable_if<
std::enable_if_t<
!black_magic::CallHelper<Func, black_magic::S<>>::value &&
!black_magic::CallHelper<Func, black_magic::S<crow::request>>::value &&
black_magic::CallHelper<Func, black_magic::S<crow::response&>>::value,
void>::type
void>
operator()(Func&& f)
{
static_assert(std::is_same<void, decltype(f(std::declval<crow::response&>()))>::value,
static_assert(std::is_same_v<void, decltype(f(std::declval<crow::response&>()))>,
"Handler function with response argument should have void return type");
handler_ = ([f = std::move(f)](const request&, response& res) {
f(res);
@@ -392,20 +392,20 @@ namespace crow // NOTE: Already documented in "crow/app.h"
}
template<typename Func>
typename std::enable_if<
std::enable_if_t<
!black_magic::CallHelper<Func, black_magic::S<>>::value &&
!black_magic::CallHelper<Func, black_magic::S<crow::request>>::value &&
!black_magic::CallHelper<Func, black_magic::S<crow::response&>>::value,
void>::type
void>
operator()(Func&& f)
{
static_assert(std::is_same<void, decltype(f(std::declval<crow::request>(), std::declval<crow::response&>()))>::value,
static_assert(std::is_same_v<void, decltype(f(std::declval<crow::request>(), std::declval<crow::response&>()))>,
"Handler function with response argument should have void return type");
handler_ = std::move(f);
}
/// @endcond
bool has_handler()
bool has_handler() const
{
return (handler_ != nullptr);
}
@@ -656,7 +656,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
void operator()(std::string name, Func&& f)
{
name_ = std::move(name);
(*this).template operator()<Func>(std::forward(f));
this->operator()<Func>(std::forward(f));
}
private:
@@ -756,8 +756,8 @@ namespace crow // NOTE: Already documented in "crow/app.h"
Trie()
{}
/// Check whether or not the trie is empty.
bool is_empty()
/// Check whether the trie is empty.
bool is_empty() const
{
return head_.children.empty();
}
@@ -772,7 +772,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
private:
void optimizeNode(Node& node)
static void optimizeNode(Node& node)
{
if (node.children.empty())
return;
@@ -795,7 +795,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
}
}
void debug_node_print(const Node& node, size_t level)
static void debug_node_print(const Node& node, size_t level)
{
if (node.param != ParamType::MAX)
{
@@ -837,7 +837,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
}
public:
void debug_print()
void debug_print() const
{
CROW_LOG_DEBUG << "└➙ ROOT";
for (const auto& child : head_.children)
@@ -852,7 +852,11 @@ namespace crow // NOTE: Already documented in "crow/app.h"
}
//Rule_index, Blueprint_index, routing_params
routing_handle_result find(const std::string& req_url, const Node& node, size_t pos = 0, routing_params* params = nullptr, std::vector<size_t>* blueprints = nullptr) const
routing_handle_result find(const std::string& req_url,
const Node& node,
size_t pos = 0,
routing_params* params = nullptr,
std::vector<size_t>* blueprints = nullptr) const
{
//start params as an empty struct
routing_params empty;
@@ -1138,7 +1142,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
all_rules_ = other.all_rules_;
}
*/
Blueprint(Blueprint&& value)
Blueprint(Blueprint&& value) noexcept
{
*this = std::move(value);
}
@@ -1157,12 +1161,12 @@ namespace crow // NOTE: Already documented in "crow/app.h"
return *this;
}
bool operator==(const Blueprint& value)
bool operator==(const Blueprint& value) const
{
return value.prefix() == prefix_;
}
bool operator!=(const Blueprint& value)
bool operator!=(const Blueprint& value) const
{
return value.prefix() != prefix_;
}
@@ -1182,7 +1186,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
added_ = true;
}
bool is_added()
bool is_added() const
{
return added_;
}
@@ -1301,7 +1305,10 @@ namespace crow // NOTE: Already documented in "crow/app.h"
internal_add_rule_object(rule, ruleObject, INVALID_BP_ID, blueprints_);
}
void internal_add_rule_object(const std::string& rule, BaseRule* ruleObject, const size_t& BP_index, std::vector<Blueprint*>& blueprints)
void internal_add_rule_object(const std::string& rule,
BaseRule* ruleObject,
const size_t& BP_index,
const std::vector<Blueprint*>& blueprints)
{
bool has_trailing_slash = false;
std::string rule_without_trailing_slash;
@@ -1341,7 +1348,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
throw std::runtime_error("blueprint \"" + blueprint.prefix_ + "\" already exists in router");
}
void get_recursive_child_methods(Blueprint* blueprint, std::vector<HTTPMethod>& methods)
static void get_recursive_child_methods(const Blueprint* const blueprint, std::vector<HTTPMethod>& methods)
{
//we only need to deal with children if the blueprint has absolutely no methods (meaning its index won't be added to the trie)
if (blueprint->static_dir_.empty() && blueprint->all_rules_.empty())
@@ -1370,7 +1377,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
validate_bp(blueprints_, blueprint_mw);
}
void validate_bp(std::vector<Blueprint*> blueprints, detail::middleware_indices& current_mw)
void validate_bp(const std::vector<Blueprint*>& blueprints, detail::middleware_indices& current_mw)
{
for (unsigned i = 0; i < blueprints.size(); i++)
{
@@ -1566,7 +1573,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
return EMPTY;
}
std::unique_ptr<routing_handle_result> handle_initial(request& req, response& res)
std::unique_ptr<routing_handle_result> handle_initial(const request& req, response& res)
{
HTTPMethod method_actual = req.method;
@@ -1740,7 +1747,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
}
template<typename App>
typename std::enable_if<std::tuple_size<typename App::mw_container_t>::value != 0, void>::type
std::enable_if_t<std::tuple_size_v<typename App::mw_container_t> != 0, void>
handle_rule(BaseRule& rule, crow::request& req, crow::response& res, const crow::routing_params& rp)
{
if (!rule.mw_indices_.empty())
@@ -1766,7 +1773,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
detail::after_handlers_call_helper<
decltype(crit_bwd),
std::tuple_size<typename App::mw_container_t>::value - 1,
std::tuple_size_v<typename App::mw_container_t> - 1,
typename App::context_t,
typename App::mw_container_t>(crit_bwd, container, ctx, req, res);
glob_completion_handler();
@@ -1776,7 +1783,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
}
template<typename App>
typename std::enable_if<std::tuple_size<typename App::mw_container_t>::value == 0, void>::type
std::enable_if_t<std::tuple_size_v<typename App::mw_container_t> == 0, void>
handle_rule(BaseRule& rule, crow::request& req, crow::response& res, const crow::routing_params& rp)
{
rule.handle(req, res, rp);

View File

@@ -131,7 +131,7 @@ namespace crow
return socket_;
}
stream_protocol::endpoint remote_endpoint()
stream_protocol::endpoint remote_endpoint() const
{
return socket_.local_endpoint();
}
@@ -141,8 +141,7 @@ namespace crow
return "";
}
bool is_open()
{
bool is_open() const {
return socket_.is_open();
}
@@ -188,13 +187,12 @@ namespace crow
ssl_socket_(new ssl_socket_t(io_context, *ctx))
{}
asio::ssl::stream<tcp::socket>& socket()
asio::ssl::stream<tcp::socket>& socket() const
{
return *ssl_socket_;
}
tcp::socket::lowest_layer_type&
raw_socket()
tcp::socket::lowest_layer_type& raw_socket() const
{
return ssl_socket_->lowest_layer();
}

View File

@@ -75,10 +75,10 @@ namespace crow // NOTE: Already documented in "crow/app.h"
virtual ~connection() = default;
void userdata(void* u) { userdata_ = u; }
void* userdata() { return userdata_; }
const void* userdata() const { return userdata_; }
private:
void* userdata_;
void* userdata_{nullptr};
};
// Modified version of the illustration in RFC6455 Section-5.2
@@ -595,13 +595,13 @@ namespace crow // NOTE: Already documented in "crow/app.h"
}
/// Check if the FIN bit is set.
bool is_FIN()
bool is_FIN() const
{
return mini_header_ & 0x8000;
}
/// Extract the opcode from the header.
int opcode()
int opcode() const
{
return (mini_header_ & 0x0f00) >> 8;
}