Skip to content

Commit 58382a6

Browse files
committed
engine: Fix some findings from clang-tidy
1 parent 1cf804f commit 58382a6

File tree

8 files changed

+85
-30
lines changed

8 files changed

+85
-30
lines changed

engine/src/config.hpp

+12
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,20 @@
4949
#define CLOE_SIMULATION_UUID_VAR "CLOE_SIMULATION_UUID"
5050
#endif
5151

52+
#ifndef CLOE_LUA_DEBUGGER_PORT
53+
#define CLOE_LUA_DEBUGGER_PORT 21110
54+
#endif
55+
5256
// The environment variable from which additional plugins should
5357
// be loaded. Takes the same format as PATH.
5458
#ifndef CLOE_PLUGIN_PATH
5559
#define CLOE_PLUGIN_PATH "CLOE_PLUGIN_PATH"
5660
#endif
61+
62+
#ifndef CLOE_TRIGGER_PATH_DELIMITER
63+
#define CLOE_TRIGGER_PATH_DELIMITER "/"
64+
#endif
65+
66+
#ifndef CLOE_SIGNAL_PATH_DELIMITER
67+
#define CLOE_SIGNAL_PATH_DELIMITER "/"
68+
#endif

engine/src/coordinator.hpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ class TriggerUnknownAction : public cloe::TriggerInvalid {
4949
public:
5050
TriggerUnknownAction(const std::string& key, const cloe::Conf& c)
5151
: TriggerInvalid(c, "unknown action: " + key), key_(key) {}
52-
virtual ~TriggerUnknownAction() noexcept = default;
52+
~TriggerUnknownAction() noexcept override = default;
5353

5454
/**
5555
* Return key that is unknown.
5656
*/
57-
const char* key() const { return key_.c_str(); }
57+
[[nodiscard]] const char* key() const { return key_.c_str(); }
5858

5959
private:
6060
std::string key_;
@@ -73,7 +73,7 @@ class TriggerUnknownEvent : public cloe::TriggerInvalid {
7373
/**
7474
* Return key that is unknown.
7575
*/
76-
const char* key() const { return key_.c_str(); }
76+
[[nodiscard]] const char* key() const { return key_.c_str(); }
7777

7878
private:
7979
std::string key_;
@@ -107,15 +107,15 @@ class Coordinator {
107107
void register_event(const std::string& key, cloe::EventFactoryPtr&& ef,
108108
std::shared_ptr<cloe::Callback> storage);
109109

110-
sol::table register_lua_table(const std::string& field);
110+
[[nodiscard]] sol::table register_lua_table(const std::string& field);
111111

112-
cloe::DataBroker* data_broker() const { return db_; }
112+
[[nodiscard]] cloe::DataBroker* data_broker() const { return db_; }
113113

114114
std::shared_ptr<cloe::TriggerRegistrar> trigger_registrar(cloe::Source s);
115115

116116
void enroll(cloe::Registrar& r);
117117

118-
cloe::Logger logger() const { return cloe::logger::get("cloe"); }
118+
[[nodiscard]] cloe::Logger logger() const { return cloe::logger::get("cloe"); }
119119

120120
/**
121121
* Process any incoming triggers, clear the buffer, and trigger time-based
@@ -130,11 +130,11 @@ class Coordinator {
130130
void execute_action_from_lua(const cloe::Sync& sync, const sol::object& obj);
131131

132132
protected:
133-
cloe::ActionPtr make_action(const sol::object& lua) const;
134-
cloe::ActionPtr make_action(const cloe::Conf& c) const;
135-
cloe::EventPtr make_event(const cloe::Conf& c) const;
136-
cloe::TriggerPtr make_trigger(cloe::Source s, const cloe::Conf& c) const;
137-
cloe::TriggerPtr make_trigger(const sol::table& tbl) const;
133+
[[nodiscard]] cloe::ActionPtr make_action(const sol::object& lua) const;
134+
[[nodiscard]] cloe::ActionPtr make_action(const cloe::Conf& c) const;
135+
[[nodiscard]] cloe::EventPtr make_event(const cloe::Conf& c) const;
136+
[[nodiscard]] cloe::TriggerPtr make_trigger(cloe::Source s, const cloe::Conf& c) const;
137+
[[nodiscard]] cloe::TriggerPtr make_trigger(const sol::table& tbl) const;
138138
void queue_trigger(cloe::Source s, const cloe::Conf& c) { queue_trigger(make_trigger(s, c)); }
139139
void queue_trigger(cloe::TriggerPtr&& tp);
140140
void store_trigger(cloe::TriggerPtr&& tp, const cloe::Sync& sync);

engine/src/error_handler.hpp

+23-1
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,21 @@
1818

1919
#pragma once
2020

21-
#include <iostream>
21+
#include <ostream> // for ostream
22+
#include <sstream> // for stringstream
2223

2324
#include <cloe/core/error.hpp> // for Error
2425
#include <fable/error.hpp> // for ConfError, SchemaError
2526
#include <fable/utility.hpp> // for indent_string, pretty_print
2627

2728
namespace cloe {
2829

30+
/**
31+
* Format various kinds of error so that they are easy to read.
32+
*
33+
* \param exception error to format
34+
* \return formatted string, ready for printing
35+
*/
2936
inline std::string format_error(const std::exception& exception) {
3037
std::stringstream buf;
3138
if (const auto* err = dynamic_cast<const fable::SchemaError*>(&exception); err) {
@@ -44,6 +51,21 @@ inline std::string format_error(const std::exception& exception) {
4451
return buf.str();
4552
}
4653

54+
/**
55+
* Run a function and print any exception nicely to the ostream provided.
56+
*
57+
* This essentially replaces:
58+
*
59+
* try { ... }
60+
* catch (cloe::ConcludedError&) { ... }
61+
* catch (std::exception&) { ... }
62+
*
63+
* with a single line.
64+
*
65+
* \param out stream to write error message to (e.g. std::cerr)
66+
* \param f function to run
67+
* \return return value of f
68+
*/
4769
template <typename Func>
4870
auto conclude_error(std::ostream& out, Func f) -> decltype(f()) {
4971
try {

engine/src/lua_stack_test.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/*
2+
* Copyright 2024 Robert Bosch GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
118

219
#include <gtest/gtest.h>
320
#include <string>

engine/src/main_commands.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <string>
2525
#include <vector>
2626

27+
#include "config.hpp"
2728
#include "lua_setup.hpp"
2829
#include "stack_factory.hpp"
2930

@@ -77,7 +78,7 @@ struct RunOptions {
7778
bool report_progress = true;
7879

7980
bool debug_lua = false;
80-
int debug_lua_port = 21110;
81+
int debug_lua_port = CLOE_LUA_DEBUGGER_PORT;
8182
};
8283

8384
int run(const RunOptions& opt, const std::vector<std::string>& filepaths);

engine/src/server.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ namespace engine {
3636

3737
class ServerRegistrarImpl : public ServerRegistrar {
3838
public:
39-
ServerRegistrarImpl(
40-
oak::Registrar static_reg, oak::ProxyRegistrar<cloe::HandlerType> api_reg)
41-
: static_registrar_(static_reg), api_registrar_(api_reg) {}
39+
ServerRegistrarImpl(const oak::Registrar& static_reg, oak::ProxyRegistrar<cloe::HandlerType> api_reg)
40+
: static_registrar_(static_reg), api_registrar_(std::move(api_reg)) {}
4241

4342
std::unique_ptr<ServerRegistrar> clone() const override {
4443
return std::make_unique<ServerRegistrarImpl>(static_registrar_, api_registrar_);
@@ -62,7 +61,7 @@ class ServerRegistrarImpl : public ServerRegistrar {
6261
}
6362

6463
void register_api_handler(const std::string& endpoint, cloe::HandlerType t,
65-
cloe::Handler h) override {
64+
cloe::Handler h) override {
6665
api_registrar_.register_handler(endpoint, t, h);
6766
}
6867

engine/src/server.hpp

+12-8
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ class ServerRegistrar {
4141
public:
4242
virtual ~ServerRegistrar() = default;
4343

44-
virtual std::unique_ptr<ServerRegistrar> clone() const = 0;
44+
[[nodiscard]] virtual std::unique_ptr<ServerRegistrar> clone() const = 0;
4545

46-
virtual std::unique_ptr<ServerRegistrar> with_prefix(const std::string& static_prefix,
46+
[[nodiscard]] virtual std::unique_ptr<ServerRegistrar> with_prefix(const std::string& static_prefix,
4747
const std::string& api_prefix) const = 0;
4848

4949
virtual void register_static_handler(const std::string& endpoint, cloe::Handler h) = 0;
@@ -59,25 +59,29 @@ class ServerRegistrar {
5959
*/
6060
class Server {
6161
public:
62-
Server(const cloe::ServerConf& config) : config_(config) {}
62+
Server(const Server&) = default;
63+
Server(Server&&) = delete;
64+
Server& operator=(const Server&) = default;
65+
Server& operator=(Server&&) = delete;
66+
Server(cloe::ServerConf config) : config_(std::move(config)) {}
6367
virtual ~Server() = default;
6468

6569
/**
6670
* Return the server configuration.
6771
*/
68-
const cloe::ServerConf& config() const { return config_; }
72+
[[nodiscard]] const cloe::ServerConf& config() const { return config_; }
6973

7074
/**
7175
* Return whether the server is alive and listening for requests.
7276
*/
73-
virtual bool is_listening() const = 0;
77+
[[nodiscard]] virtual bool is_listening() const = 0;
7478

7579
/**
7680
* Return whether the server is currently streaming buffer data to a file.
7781
*
7882
* If it is, expect performance to be bad.
7983
*/
80-
virtual bool is_streaming() const = 0;
84+
[[nodiscard]] virtual bool is_streaming() const = 0;
8185

8286
/**
8387
* Start the web server.
@@ -104,7 +108,7 @@ class Server {
104108
* Return a new ServerRegistrar that lets you register static content and
105109
* API endpoints with the web server.
106110
*/
107-
virtual std::unique_ptr<ServerRegistrar> server_registrar() = 0;
111+
[[nodiscard]] virtual std::unique_ptr<ServerRegistrar> server_registrar() = 0;
108112

109113
/**
110114
* Refresh and/or start streaming api data to a file.
@@ -124,7 +128,7 @@ class Server {
124128
*
125129
* \return Lock guard
126130
*/
127-
virtual Defer lock() = 0;
131+
[[nodiscard]] virtual Defer lock() = 0;
128132

129133
protected:
130134
cloe::Logger logger() const { return cloe::logger::get("cloe"); }

oak/include/oak/server.hpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,19 @@ class Server {
7171
*/
7272
void set_address(const std::string& addr) { listen_addr_ = addr; }
7373

74-
const std::string& address() const { return listen_addr_; }
74+
[[nodiscard]] const std::string& address() const { return listen_addr_; }
7575

7676
/**
7777
* Set the port on which to listen.
7878
*/
7979
void set_port(int port) { listen_port_ = port; }
8080

81-
int port() const { return listen_port_; }
81+
[[nodiscard]] int port() const { return listen_port_; }
8282

8383
/**
8484
* Returns whether the server has started and is currently listening.
8585
*/
86-
bool is_listening() const { return listening_; }
86+
[[nodiscard]] bool is_listening() const { return listening_; }
8787

8888
/**
8989
* Start the server.
@@ -93,7 +93,7 @@ class Server {
9393
/**
9494
* Return endpoint data in json format.
9595
*/
96-
fable::Json endpoints_to_json(const std::vector<std::string>& endpoints) const;
96+
[[nodiscard]] fable::Json endpoints_to_json(const std::vector<std::string>& endpoints) const;
9797

9898
/**
9999
* Stop the server.
@@ -103,7 +103,7 @@ class Server {
103103
/**
104104
* Return a list of all registered endpoints.
105105
*/
106-
std::vector<std::string> endpoints() const;
106+
[[nodiscard]] std::vector<std::string> endpoints() const;
107107

108108
protected:
109109
friend StaticRegistrar;

0 commit comments

Comments
 (0)