YARP
Yet Another Robot Platform
Searchable.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-FileCopyrightText: 2006, 2008 Arjan Gijsberts
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #include <yarp/os/Searchable.h>
9 
10 #include <yarp/os/Value.h>
11 
12 using namespace yarp::os;
13 
14 #ifndef DOXYGEN_SHOULD_SKIP_THIS
15 yarp::os::SearchReport::SearchReport() :
16  key("?"),
17  value(""),
18  isFound(false),
19  isGroup(false),
20  isComment(false),
21  isDefault(false)
22 {
23 }
24 
25 yarp::os::SearchMonitor::~SearchMonitor() = default;
26 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
27 
28 
30  monitor(nullptr)
31 {
32 }
33 
35 
36 bool yarp::os::Searchable::check(const std::string& key,
37  yarp::os::Value*& result,
38  const std::string& comment) const
39 {
40  if (getMonitor() != nullptr && !comment.empty()) {
41  SearchReport report;
42  report.key = key;
43  report.value = comment;
44  report.isComment = true;
45  reportToMonitor(report);
46  }
47  yarp::os::Value& bit = find(key);
48  bool ok = !(bit.isNull());
49  if (ok) {
50  result = &bit;
51  }
52  return ok;
53 }
54 
56  const yarp::os::Value& fallback,
57  const std::string& comment) const
58 {
59  if (getMonitor() != nullptr && !comment.empty()) {
60  yarp::os::SearchReport report;
61  report.key = key;
62  report.value = comment;
63  report.isComment = true;
64  reportToMonitor(report);
65  }
66  if (getMonitor() != nullptr) {
67  yarp::os::SearchReport report;
68  report.key = key;
69  report.value = fallback.toString();
70  report.isDefault = true;
71  reportToMonitor(report);
72  }
73  yarp::os::Value& bit = find(key);
74  bool ok = !(bit.isNull());
75  if (ok) {
76  return bit;
77  }
78  return fallback;
79 }
80 
81 bool yarp::os::Searchable::check(const std::string& key,
82  const std::string& comment) const
83 {
84  if (getMonitor() != nullptr && !comment.empty()) {
85  yarp::os::SearchReport report;
86  report.key = key;
87  report.value = comment;
88  report.isComment = true;
89  reportToMonitor(report);
90  }
91  return check(key);
92 }
93 
95  const std::string& comment) const
96 {
97  if (getMonitor() != nullptr && !comment.empty()) {
98  yarp::os::SearchReport report;
99  report.key = key;
100  report.value = comment;
101  report.isComment = true;
102  reportToMonitor(report);
103  }
104  return findGroup(key);
105 }
106 
108 {
109  return false;
110 }
111 
112 #ifndef DOXYGEN_SHOULD_SKIP_THIS
113 
114 void yarp::os::Searchable::setMonitor(yarp::os::SearchMonitor* monitor, const char* context)
115 {
116  this->monitor = monitor;
117  this->monitorContext = context;
118 }
119 
120 yarp::os::SearchMonitor* yarp::os::Searchable::getMonitor() const
121 {
122  return monitor;
123 }
124 
125 std::string yarp::os::Searchable::getMonitorContext() const
126 {
127  return monitorContext;
128 }
129 
130 void yarp::os::Searchable::reportToMonitor(const yarp::os::SearchReport& report) const
131 {
132  if (monitor != nullptr) {
133  monitor->report(report, monitorContext.c_str());
134  }
135 }
136 
137 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
virtual bool isNull() const
Checks if the object is invalid.
Definition: Searchable.cpp:107
virtual ~Searchable()
Destructor.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
Searchable()
Default constructor.
Definition: Searchable.cpp:29
A single value (typically within a Bottle).
Definition: Value.h:43
std::string toString() const override
Return a standard text representation of the content of the object.
Definition: Value.cpp:356
bool isNull() const override
Checks if the object is invalid.
Definition: Value.cpp:380
An interface to the operating system, including Port based communication.
std::string findGroup(const robotinterface::ParamList &list, const std::string &name)
Definition: Types.cpp:47