$darkmode
entity.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 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  */
23 #pragma once
24 
25 #include <string> // for string
26 
27 #include <cloe/core.hpp> // for Error, Logger
28 #include <fable/json.hpp> // for Json
29 
30 namespace cloe {
31 
35 class InvalidNameError : public Error {
36  public:
37  explicit InvalidNameError(std::string name)
38  : Error("name is invalid: " + name), name_(std::move(name)) {}
39  virtual ~InvalidNameError() noexcept = default;
40 
41  const std::string& name() const { return name_; }
42 
43  private:
44  std::string name_;
45 };
46 
50 class Entity {
51  public:
52  explicit Entity(std::string name) {
53  set_name(std::move(name));
54  set_description("");
55  }
56 
57  Entity(std::string name, std::string desc) {
58  set_name(std::move(name));
59  set_description(std::move(desc));
60  }
61 
62  virtual ~Entity() noexcept = default;
63 
67  const std::string& name() const { return name_; }
68 
83  void set_name(std::string name);
84 
90  const std::string& description() const { return desc_; }
91 
95  void set_description(std::string desc) { desc_ = std::move(desc); }
96 
97  protected:
101  virtual Logger logger() const { return logger::get(name()); }
102 
103  // Note: moving the definition from the inline into this declaration here
104  // will result in cloe/trigger.hpp not compiling because:
105  //
106  // no matching function for call to 'to_json(fable::Json, const Entity&)'
107  //
108  // I have no idea why it works in many other instances, but not here. If you,
109  // my dear reader, know why, please open a pull request!
110  friend void to_json(fable::Json& j, const Entity& e);
111 
112  protected:
113  std::string name_;
114  std::string desc_;
115 };
116 
120 inline void to_json(fable::Json& j, const Entity& e) {
121  j["name"] = e.name();
122  if (!e.desc_.empty()) {
123  j["description"] = e.description();
124  }
125 }
126 
127 } // namespace cloe
Definition: entity.hpp:50
virtual Logger logger() const
Definition: entity.hpp:101
friend void to_json(fable::Json &j, const Entity &e)
Definition: entity.hpp:120
void set_name(std::string name)
Definition: entity.cpp:30
const std::string & description() const
Definition: entity.hpp:90
const std::string & name() const
Definition: entity.hpp:67
void set_description(std::string desc)
Definition: entity.hpp:95
Definition: error.hpp:35
Definition: entity.hpp:35
nlohmann::json Json
Definition: fable_fwd.hpp:35