$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 #ifndef CLOE_ENTITY_HPP_
25 #define CLOE_ENTITY_HPP_
26 
27 #include <string> // for string
28 
29 #include <cloe/core.hpp> // for Json, Error, Logger
30 
31 namespace cloe {
32 
36 class InvalidNameError : public Error {
37  public:
38  explicit InvalidNameError(const std::string& name)
39  : Error("name is invalid: " + name), name_(name) {}
40  virtual ~InvalidNameError() noexcept = default;
41 
42  const std::string& name() const { return name_; }
43 
44  private:
45  std::string name_;
46 };
47 
51 class Entity {
52  public:
53  explicit Entity(const std::string& name) {
54  set_name(name);
55  set_description("");
56  }
57 
58  Entity(const std::string& name, const std::string& desc) {
59  set_name(name);
60  set_description(desc);
61  }
62 
63  virtual ~Entity() noexcept = default;
64 
68  std::string name() const { return name_; }
69 
84  void set_name(const std::string& name);
85 
91  std::string description() const { return desc_; }
92 
93  void set_description(const std::string& desc) { desc_ = desc; }
94 
95  protected:
99  virtual Logger logger() const { return logger::get(name()); }
100 
101  // Note: moving the definition from the inline into this declaration here
102  // will result in cloe/trigger.hpp not compiling because:
103  //
104  // no matching function for call to 'to_json(Json, const Entity&)'
105  //
106  // I have no idea why it works in many other instances, but not here. If you,
107  // my dear reader, know why, please open a pull request!
108  friend void to_json(Json& j, const Entity& e);
109 
110  protected:
111  std::string name_;
112  std::string desc_;
113 };
114 
118 inline void to_json(Json& j, const Entity& e) {
119  j["name"] = e.name();
120  if (!e.desc_.empty()) {
121  j["description"] = e.description();
122  }
123 }
124 
125 } // namespace cloe
126 
127 #endif // CLOE_ENTITY_HPP_
Definition: entity.hpp:36
Definition: error.hpp:35
Definition: coordinator.hpp:36
Definition: enum_test.cpp:29
Definition: entity.hpp:51
std::string name() const
Definition: entity.hpp:68
std::string description() const
Definition: entity.hpp:91