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