Skip to content

Commit baa35ee

Browse files
committed
runtime: Use fable namespace for fable types
This has a few advantages: 1. Users know where types come from, which aids them in finding documentation and further information. It encourages them to use other facilities from the library. The fable types are not an implementation detail. 2. It allows selective header inclusion and use of forward declarations to cut down compile times.
1 parent a98ef5a commit baa35ee

File tree

10 files changed

+179
-67
lines changed

10 files changed

+179
-67
lines changed

runtime/include/cloe/cloe_fwd.hpp

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2023 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+
*/
18+
/**
19+
* \file cloe/cloe_fwd.hpp
20+
*
21+
* This file provides forward declarations.
22+
*/
23+
24+
#pragma once
25+
26+
#include <chrono> // for nanoseconds
27+
#include <functional> // for function<>
28+
#include <memory> // for unique_ptr<>, shared_ptr<>
29+
30+
#include <spdlog/fwd.h> // for spdlog namespace
31+
#include <fable/fable_fwd.hpp> // for fable namespace
32+
33+
namespace cloe {
34+
35+
// from core/duration.hpp
36+
using Duration = std::chrono::nanoseconds;
37+
38+
// from core/error.hpp
39+
class Error;
40+
class ConcludedError;
41+
42+
// from core/logger.hpp
43+
using Logger = std::shared_ptr<spdlog::logger>;
44+
using LogLevel = spdlog::level::level_enum;
45+
46+
// from entity.hpp
47+
class Entity;
48+
49+
// from sync.hpp
50+
class Sync;
51+
52+
// from handler.hpp
53+
class Request;
54+
class Response;
55+
enum class ContentType;
56+
enum class RequestMethod;
57+
enum class StatusCode;
58+
using Handler = std::function<void(const Request&, Response&)>;
59+
60+
// from trigger.hpp
61+
class Trigger;
62+
using TriggerPtr = std::unique_ptr<Trigger>;
63+
class TriggerError;
64+
template <typename T>
65+
class TriggerFactory;
66+
class TriggerRegistrar;
67+
68+
class Callback;
69+
enum class Source;
70+
71+
class Event;
72+
using EventPtr = std::unique_ptr<Event>;
73+
using EventFactory = TriggerFactory<Event>;
74+
75+
class Action;
76+
using ActionPtr = std::unique_ptr<Action>;
77+
using ActionFactory = TriggerFactory<Action>;
78+
79+
// from registrar.hpp
80+
enum class HandlerType;
81+
class Registrar;
82+
83+
// from model.hpp
84+
class Model;
85+
class ModelFactory;
86+
87+
// from component.hpp
88+
class Component;
89+
class ComponentFactory;
90+
91+
// from vehicle.hpp
92+
class Vehicle;
93+
94+
// from controller.hpp
95+
class Controller;
96+
class ControllerFactory;
97+
98+
// from simulator.hpp
99+
class Simulator;
100+
class SimulatorFactory;
101+
102+
} // namespace cloe

runtime/include/cloe/component.hpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@
3232
#include <type_traits> // for decay
3333
#include <utility> // for move
3434

35+
#include <fable/fable_fwd.hpp> // for Json
36+
3537
#include <cloe/model.hpp> // for Model, ModelFactory
3638
#include <cloe/sync.hpp> // for Sync
37-
#include <fable/enum.hpp> // for ENUM_SERIALIZATION
3839

3940
/**
4041
* This macro defines a ComponentFactory named xFactoryType and with the
@@ -59,7 +60,7 @@
5960
return std::make_unique<std::decay<decltype(*this)>::type>(*this); \
6061
} \
6162
std::unique_ptr<::cloe::Component> make( \
62-
const ::cloe::Conf&, std::vector<std::shared_ptr<::cloe::Component>>) const override; \
63+
const ::fable::Conf&, std::vector<std::shared_ptr<::cloe::Component>>) const override; \
6364
\
6465
protected: \
6566
::cloe::Schema schema_impl() override { return config_.schema(); } \
@@ -79,7 +80,7 @@
7980
*/
8081
#define DEFINE_COMPONENT_FACTORY_MAKE(xFactoryType, xComponentType, xInputType) \
8182
std::unique_ptr<::cloe::Component> xFactoryType::make( \
82-
const ::cloe::Conf& c, std::vector<std::shared_ptr<::cloe::Component>> comp) const { \
83+
const ::fable::Conf& c, std::vector<std::shared_ptr<::cloe::Component>> comp) const { \
8384
decltype(config_) conf{config_}; \
8485
assert(comp.size() == 1); \
8586
if (!c->is_null()) { \
@@ -137,7 +138,7 @@ namespace cloe {
137138
*
138139
* - `uint64_t id() const`
139140
* - `T* as()`
140-
* - `Json active_state() const = 0`
141+
* - `fable::Json active_state() const = 0`
141142
*/
142143
class Component : public Model {
143144
public:
@@ -169,7 +170,7 @@ class Component : public Model {
169170
/**
170171
* Return the JSON representation of the component.
171172
*/
172-
virtual Json active_state() const = 0;
173+
virtual fable::Json active_state() const = 0;
173174

174175
/**
175176
* Clear any cache that may be accumulated during a step.
@@ -190,7 +191,7 @@ class Component : public Model {
190191
void abort() override {}
191192

192193
protected:
193-
friend void to_json(Json& j, const Component& c) {
194+
friend void to_json(fable::Json& j, const Component& c) {
194195
j = c.active_state();
195196
j["id"] = c.id();
196197
j["name"] = c.name();
@@ -236,7 +237,7 @@ class ComponentFactory : public ModelFactory {
236237
*
237238
* - This method may throw Error.
238239
*/
239-
virtual std::unique_ptr<Component> make(const Conf& c,
240+
virtual std::unique_ptr<Component> make(const fable::Conf& c,
240241
std::vector<std::shared_ptr<Component>>) const = 0;
241242
};
242243

runtime/include/cloe/controller.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
return std::make_unique<std::decay<decltype(*this)>::type>(*this); \
5454
} \
5555
\
56-
std::unique_ptr<::cloe::Controller> make(const ::cloe::Conf&) const override; \
56+
std::unique_ptr<::cloe::Controller> make(const ::fable::Conf&) const override; \
5757
\
5858
protected: \
5959
::cloe::Schema schema_impl() override { return config_.schema(); } \
@@ -71,7 +71,7 @@
7171
* xControllerType(const std::string&, const xConfigType&)
7272
*/
7373
#define DEFINE_CONTROLLER_FACTORY_MAKE(xFactoryType, xControllerType) \
74-
std::unique_ptr<::cloe::Controller> xFactoryType::make(const ::cloe::Conf& c) const { \
74+
std::unique_ptr<::cloe::Controller> xFactoryType::make(const ::fable::Conf& c) const { \
7575
decltype(config_) conf{config_}; \
7676
if (!c->is_null()) { \
7777
conf.from_conf(c); \
@@ -192,7 +192,7 @@ class ControllerFactory : public ModelFactory {
192192
*
193193
* - This method may throw Error.
194194
*/
195-
virtual std::unique_ptr<Controller> make(const Conf& c) const = 0;
195+
virtual std::unique_ptr<Controller> make(const fable::Conf& c) const = 0;
196196
};
197197

198198
} // namespace cloe

runtime/include/cloe/entity.hpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424

2525
#include <string> // for string
2626

27-
#include <cloe/core.hpp> // for Json, Error, Logger
27+
#include <cloe/core.hpp> // for Error, Logger
28+
#include <fable/json.hpp> // for Json
2829

2930
namespace cloe {
3031

@@ -99,11 +100,11 @@ class Entity {
99100
// Note: moving the definition from the inline into this declaration here
100101
// will result in cloe/trigger.hpp not compiling because:
101102
//
102-
// no matching function for call to 'to_json(Json, const Entity&)'
103+
// no matching function for call to 'to_json(fable::Json, const Entity&)'
103104
//
104105
// I have no idea why it works in many other instances, but not here. If you,
105106
// my dear reader, know why, please open a pull request!
106-
friend void to_json(Json& j, const Entity& e);
107+
friend void to_json(fable::Json& j, const Entity& e);
107108

108109
protected:
109110
std::string name_;
@@ -113,7 +114,7 @@ class Entity {
113114
/**
114115
* Return JSON representation of an Entity.
115116
*/
116-
inline void to_json(Json& j, const Entity& e) {
117+
inline void to_json(fable::Json& j, const Entity& e) {
117118
j["name"] = e.name();
118119
if (!e.desc_.empty()) {
119120
j["description"] = e.description();

runtime/include/cloe/handler.hpp

+20-19
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@
3636
#pragma once
3737

3838
#include <functional> // for function
39-
#include <map> // for map, map<>::mapped_type
40-
#include <string> // for string, basic_string
39+
#include <map> // for map
40+
#include <string> // for string
4141
#include <utility> // for pair
4242

43-
#include <cloe/core.hpp> // for Confable, Json
43+
#include <fable/confable.hpp> // for Confable
44+
#include <fable/json.hpp> // for Json
4445

4546
namespace cloe {
4647

@@ -148,7 +149,7 @@ class Request {
148149
/**
149150
* Helper method that tries to convert the body to a JSON object.
150151
*/
151-
virtual Json as_json() const { return fable::parse_json(body()); }
152+
virtual fable::Json as_json() const { return fable::parse_json(body()); }
152153
};
153154

154155
/**
@@ -274,7 +275,7 @@ class Response {
274275
* - When NDEBUG is set, then the serialization of JSON to string does not
275276
* pretty print.
276277
*/
277-
void set_body(const Json& js) {
278+
void set_body(const fable::Json& js) {
278279
#ifdef NDEBUG
279280
this->set_body(js.dump(), ContentType::JSON);
280281
#else
@@ -285,28 +286,28 @@ class Response {
285286
/**
286287
* Write is an alias for `set_body(Json)`, for historical reasons.
287288
*/
288-
void write(const Json& js) { this->set_body(js); }
289+
void write(const fable::Json& js) { this->set_body(js); }
289290

290291
/**
291292
* Use bad_request when the method is correct, but the body content is not
292293
* correct.
293294
*/
294-
void bad_request(const Json& js) { this->error(StatusCode::BAD_REQUEST, js); }
295+
void bad_request(const fable::Json& js) { this->error(StatusCode::BAD_REQUEST, js); }
295296

296297
/**
297298
* Use not_found when the resource in question is not available.
298299
*/
299-
void not_found(const Json& js) { this->error(StatusCode::NOT_FOUND, js); }
300+
void not_found(const fable::Json& js) { this->error(StatusCode::NOT_FOUND, js); }
300301

301302
/**
302303
* Use not_allowed when the method (GET, POST, PUT, DELETE) is not allowed.
303304
*
304305
* Specify in `allow` which method is allowed:
305306
* ```
306-
* r.not_allowed(RequestMethod::POST, Json{{"error", "try something else"}});
307+
* r.not_allowed(RequestMethod::POST, fable::Json{{"error", "try something else"}});
307308
* ```
308309
*/
309-
void not_allowed(const RequestMethod& allow, const Json& js) {
310+
void not_allowed(const RequestMethod& allow, const fable::Json& js) {
310311
this->set_status(StatusCode::NOT_ALLOWED);
311312
this->set_header("Allow", as_cstr(allow));
312313
this->set_body(js);
@@ -316,14 +317,14 @@ class Response {
316317
* Use not_implemented when the functionality represented by the endpoint
317318
* is not implemented yet.
318319
*/
319-
void not_implemented(const Json& js) { this->error(StatusCode::NOT_IMPLEMENTED, js); }
320+
void not_implemented(const fable::Json& js) { this->error(StatusCode::NOT_IMPLEMENTED, js); }
320321

321322
/**
322323
* Use server_error when an internal error occurred, such as a panic.
323324
*/
324-
void server_error(const Json& js) { this->error(StatusCode::SERVER_ERROR, js); }
325+
void server_error(const fable::Json& js) { this->error(StatusCode::SERVER_ERROR, js); }
325326

326-
void error(StatusCode code, const Json& js) {
327+
void error(StatusCode code, const fable::Json& js) {
327328
this->set_body(js);
328329
this->set_status(code);
329330
}
@@ -374,11 +375,11 @@ class Redirect {
374375
*/
375376
class StaticJson {
376377
public:
377-
StaticJson(Json j) : data_(j) {} // NOLINT
378+
StaticJson(fable::Json j) : data_(j) {} // NOLINT
378379
void operator()(const cloe::Request&, cloe::Response& r) { r.write(data_); }
379380

380381
private:
381-
const Json data_;
382+
const fable::Json data_;
382383
};
383384

384385
/**
@@ -387,14 +388,14 @@ class StaticJson {
387388
* - It requires a pointer as input, as it assumes that the data will change.
388389
* - The type of the pointer must have the associated `to_json` function
389390
* implemented. In other words, it must be directly convertible to a
390-
* `cloe::Json`.
391+
* `fable::Json`.
391392
*/
392393
template <typename T>
393394
class ToJson {
394395
public:
395396
explicit ToJson(const T* ptr) : ptr_(ptr) {}
396397
void operator()(const cloe::Request&, cloe::Response& r) {
397-
Json j;
398+
fable::Json j;
398399
to_json(j, *ptr_);
399400
r.set_body(j);
400401
}
@@ -417,12 +418,12 @@ class ToJson {
417418
*/
418419
class FromConf {
419420
public:
420-
explicit FromConf(Confable* ptr, bool query_map_as_json = true)
421+
explicit FromConf(fable::Confable* ptr, bool query_map_as_json = true)
421422
: ptr_(ptr), convert_(query_map_as_json) {}
422423
void operator()(const cloe::Request& q, cloe::Response& r);
423424

424425
private:
425-
Confable* ptr_;
426+
fable::Confable* ptr_;
426427
bool convert_;
427428
};
428429

runtime/include/cloe/model.hpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@
4848

4949
#pragma once
5050

51-
#include <cloe/core.hpp> // for Duration, Error, Confable
51+
#include <fable/confable.hpp> // for Confable
52+
53+
#include <cloe/core.hpp> // for Duration, Error
5254
#include <cloe/entity.hpp> // for Entity
5355

5456
namespace cloe {
@@ -409,7 +411,7 @@ class Model : public Entity {
409411
* The ModelFactory class serves as a base class for all other factory classes
410412
* that make models.
411413
*/
412-
class ModelFactory : public Entity, public Confable {
414+
class ModelFactory : public Entity, public fable::Confable {
413415
public:
414416
using Entity::Entity;
415417
virtual ~ModelFactory() = default;

runtime/include/cloe/registrar.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include <cloe/handler.hpp> // for Handler
3030
#include <cloe/trigger.hpp> // for ActionFactory, EventFactory, Callback, ...
31+
#include <fable/json.hpp> // for Json
3132

3233
namespace cloe {
3334

@@ -46,7 +47,7 @@ class DirectCallback : public Callback {
4647
bool empty() const { return triggers_.empty(); }
4748

4849
void emplace(TriggerPtr&& t, const Sync&) override { triggers_.emplace_back(std::move(t)); }
49-
void to_json(Json& j) const override { j = triggers_; }
50+
void to_json(fable::Json& j) const override { j = triggers_; }
5051

5152
void trigger(const Sync& sync, const Ctx&... args) {
5253
auto it = triggers_.begin();

0 commit comments

Comments
 (0)