Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] Value/Searchable classes separation #2891

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/os/bottle/bottle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ int main(int argc, char* argv[])
printf("pos group check: %s\n", b6.findGroup("pos").toString().c_str());
// see documentation for Bottle::findGroup
printf("nested check: %s\n", b6.find("nested").toString().c_str());
printf("nested height check: %d\n", b6.find("nested").find("height").asInt32());
printf("nested height check: %d\n", b6.find("nested").asSearchable()->find("height").asInt32());


printf("\n");
Expand Down
2 changes: 1 addition & 1 deletion src/devices/ServerInertial/ServerInertial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ bool ServerInertial::openAndAttachSubDevice(yarp::os::Property& prop)
p.put("device",subdevice.toString());
IMU_polydriver->open(p);
} else {
IMU_polydriver->open(subdevice);
IMU_polydriver->open(*subdevice.asSearchable());
}

if (!IMU_polydriver->isValid())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ int Companion::cmdPlugin(int argc, char *argv[])
for (size_t i=0; i<lst.size(); i++) {
Value& options = lst.get(i);
std::string name = options.asList()->get(0).toString();
std::string path = options.check("path", Value("unknown path")).asString();
std::string type = options.check("type", Value("unknown type")).asString();
std::string path = options.asSearchable()->check("path", Value("unknown path")).asString();
std::string type = options.asSearchable()->check("type", Value("unknown type")).asString();
if (type == "shared") {
yCInfo(COMPANION, " %15s:\t%s", name.c_str(), path.c_str());
}
Expand All @@ -165,7 +165,7 @@ int Companion::cmdPlugin(int argc, char *argv[])
Bottle lst = selector.getSelectedPlugins();
for (size_t i=0; i<lst.size(); i++) {
Value& options = lst.get(i);
std::string name = options.check("name", Value("untitled")).asString();
std::string name = options.asSearchable()->check("name", Value("untitled")).asString();
yCInfo(COMPANION, "%s", name.c_str());
}
return 0;
Expand All @@ -180,11 +180,11 @@ int Companion::cmdPlugin(int argc, char *argv[])
bool ok = true;
for (size_t i=0; i<lst.size(); i++) {
Value& options = lst.get(i);
std::string name = options.check("name", Value("untitled")).asString();
std::string type = options.check("type", Value("unknown type")).asString();
std::string name = options.asSearchable()->check("name", Value("untitled")).asString();
std::string type = options.asSearchable()->check("type", Value("unknown type")).asString();
yCInfo(COMPANION);
yCInfo(COMPANION, "%s %s", type.c_str(), name.c_str());
yCInfo(COMPANION, " * ini file: %s", options.find("inifile").toString().c_str());
yCInfo(COMPANION, " * ini file: %s", options.asSearchable()->find("inifile").toString().c_str());
options.asList()->pop();
yCInfo(COMPANION, " * config: %s", options.toString().c_str());
YarpPluginSettings settings;
Expand All @@ -195,7 +195,7 @@ YARP_DISABLE_DEPRECATED_WARNING
YARP_WARNING_POP
#endif // YARP_NO_DEPRECATED
settings.setSelector(selector);
settings.readFromSearchable(options, name);
settings.readFromSearchable(*options.asSearchable(), name);
ok &= plugin_test(settings);
}
return ok ? 0 : 1;
Expand Down
8 changes: 4 additions & 4 deletions src/libYARP_dev/src/yarp/dev/Drivers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ class Drivers::Private : public YarpPluginSelector {
Bottle lst = getSelectedPlugins();
for (size_t i=0; i<lst.size(); i++) {
Value& prop = lst.get(i);
std::string name = prop.check("name",Value("untitled")).asString();
std::string name = prop.asSearchable()->check("name",Value("untitled")).asString();
if (done.check(name)) {
continue;
}

SharedLibraryFactory lib;
YarpPluginSettings settings;
settings.setSelector(*this);
settings.readFromSearchable(prop,name);
settings.readFromSearchable(*prop.asSearchable(),name);
settings.open(lib);
std::string location = lib.getName();
if (location.empty()) {
Expand All @@ -100,8 +100,8 @@ class Drivers::Private : public YarpPluginSelector {
continue;
}

std::string cxx = prop.check("cxx",Value("unknown")).asString();
std::string wrapper = prop.check("wrapper",Value("unknown")).asString();
std::string cxx = prop.asSearchable()->check("cxx",Value("unknown")).asString();
std::string wrapper = prop.asSearchable()->check("wrapper",Value("unknown")).asString();
s += "Device \"";
s += name;
s += "\"";
Expand Down
8 changes: 4 additions & 4 deletions src/libYARP_manager/src/yarp/manager/xmlapploader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,8 @@ Application* XmlAppLoader::parsXml(const char* szFile)
for(size_t i=1; i<pos.size(); i++)
{
GyPoint pt;
pt.x = pos.get(i).find("x").asFloat64();
pt.y = pos.get(i).find("y").asFloat64();
pt.x = pos.get(i).asSearchable()->find("x").asFloat64();
pt.y = pos.get(i).asSearchable()->find("y").asFloat64();
model.points.push_back(pt);
}
arbitrator.setModelBase(model);
Expand Down Expand Up @@ -682,8 +682,8 @@ Application* XmlAppLoader::parsXml(const char* szFile)
for(size_t i=1; i<pos.size(); i++)
{
GyPoint pt;
pt.x = pos.get(i).find("x").asFloat64();
pt.y = pos.get(i).find("y").asFloat64();
pt.x = pos.get(i).asSearchable()->find("x").asFloat64();
pt.y = pos.get(i).asSearchable()->find("y").asFloat64();
model.points.push_back(pt);
}
connection.setModelBase(model);
Expand Down
12 changes: 6 additions & 6 deletions src/libYARP_manager/src/yarp/manager/yarpbroker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,14 +703,14 @@ bool YarpBroker::getAllProcesses(const char* server,
{
Process proc;
std::string sprc;
if (response.get(i).check("pid")) {
proc.pid = response.get(i).find("pid").asInt32();
if (response.get(i).asSearchable()->check("pid")) {
proc.pid = response.get(i).asSearchable()->find("pid").asInt32();
}
if (response.get(i).check("cmd")) {
sprc = response.get(i).find("cmd").asString();
if (response.get(i).asSearchable()->check("cmd")) {
sprc = response.get(i).asSearchable()->find("cmd").asString();
}
if (response.get(i).check("env") && response.get(i).find("env").asString().length()) {
sprc.append("; ").append(response.get(i).find("env").asString());
if (response.get(i).asSearchable()->check("env") && response.get(i).asSearchable()->find("env").asString().length()) {
sprc.append("; ").append(response.get(i).asSearchable()->find("env").asString());
}
proc.command = sprc;
processes.push_back(proc);
Expand Down
6 changes: 3 additions & 3 deletions src/libYARP_os/src/yarp/os/Carriers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ bool Carriers::Private::scanForCarrier(const Bytes& header)
selector.scan();
Bottle lst = selector.getSelectedPlugins();
for (size_t i = 0; i < lst.size(); i++) {
if (checkForCarrier(header, lst.get(i))) {
if (checkForCarrier(header, *lst.get(i).asSearchable())) {
return true;
}
}
Expand Down Expand Up @@ -333,15 +333,15 @@ Bottle Carriers::listCarriers()
Bottle plugins = instance.mPriv->getSelectedPlugins();
for (size_t i = 0; i < plugins.size(); i++) {
Value& options = plugins.get(i);
std::string name = options.check("name", Value("untitled")).asString();
std::string name = options.asSearchable()->check("name", Value("untitled")).asString();
if (done.check(name)) {
continue;
}

SharedLibraryFactory lib;
YarpPluginSettings settings;
settings.setSelector(*instance.mPriv);
settings.readFromSearchable(options, name);
settings.readFromSearchable(*options.asSearchable(), name);
settings.open(lib);
if (lib.getName().empty()) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/libYARP_os/src/yarp/os/NullConnectionReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,5 @@ void yarp::os::NullConnectionReader::requestDrop()

const yarp::os::Searchable& yarp::os::NullConnectionReader::getConnectionModifiers() const
{
return blank;
return *blank.asSearchable();
}
36 changes: 11 additions & 25 deletions src/libYARP_os/src/yarp/os/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ using namespace yarp::os::impl;

Value::Value() :
Portable(),
Searchable(),
proxy(nullptr)
{
}

Value::Value(std::int32_t x, bool isVocab32) :
Portable(),
Searchable(),
proxy(nullptr)
{
if (!isVocab32) {
Expand All @@ -36,15 +34,13 @@ Value::Value(std::int32_t x, bool isVocab32) :

Value::Value(yarp::conf::float64_t x) :
Portable(),
Searchable(),
proxy(nullptr)
{
setProxy(static_cast<Storable*>(makeFloat64(x)));
}

Value::Value(const std::string& str, bool isVocab32) :
Portable(),
Searchable(),
proxy(nullptr)
{
if (!isVocab32) {
Expand All @@ -56,15 +52,13 @@ Value::Value(const std::string& str, bool isVocab32) :

Value::Value(void* data, int length) :
Portable(),
Searchable(),
proxy(nullptr)
{
setProxy(static_cast<Storable*>(makeBlob(data, length)));
}

Value::Value(const Value& alt) :
Portable(),
Searchable(alt),
proxy(nullptr)
{
setProxy(static_cast<Storable*>(alt.clone()));
Expand Down Expand Up @@ -251,11 +245,21 @@ Property* Value::asDict() const

Searchable* Value::asSearchable() const
{
ok();
/*
ok();
if (proxy->isDict()) {
return proxy->asDict();
}
return proxy->asList();
*/
ok();
if (proxy->isDict()) {
return proxy->asDict();
}
if (proxy->isList()) {
return proxy->asList();
}
return nullptr;
}

const char* Value::asBlob() const
Expand Down Expand Up @@ -318,24 +322,6 @@ bool Value::write(ConnectionWriter& connection) const
return proxy->write(connection);
}

bool Value::check(const std::string& key) const
{
ok();
return proxy->check(key);
}

Value& Value::find(const std::string& key) const
{
ok();
return proxy->find(key);
}

Bottle& Value::findGroup(const std::string& key) const
{
ok();
return proxy->findGroup(key);
}

bool Value::operator==(const Value& alt) const
{
ok();
Expand Down
29 changes: 13 additions & 16 deletions src/libYARP_os/src/yarp/os/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,23 @@ class Storable;

namespace yarp::os {

class stringConvertible
{
public:
virtual std::string toString() const = 0;
};
/**
* A single value (typically within a Bottle). Values can be integers, strings,
* doubles (floating-point numbers), lists, vocabulary, or blobs
* (unformatted binary data). This set is carefully chosen to have
* good text and binary representations both for network transmission
* and human viewing/generation. Lists are represented as a nested
* Bottle object. Value objects are Searchable - but you won't
* find anything in them unless they are actually a list.
* Bottle object.
*
*/
class YARP_os_API Value : public Portable, public Searchable
class YARP_os_API Value : public Portable, stringConvertible
{
public:
using Searchable::check;
using Searchable::findGroup;

/**
* Construct a list Value
Expand Down Expand Up @@ -178,6 +180,12 @@ class YARP_os_API Value : public Portable, public Searchable
*/
virtual bool isBlob() const;

/**
* Checks if value is null.
* @return true iff value is null.
*/
virtual bool isNull() const;

/**
* Get boolean value.
* @return boolean value if value is indeed a boolean.
Expand Down Expand Up @@ -306,15 +314,6 @@ class YARP_os_API Value : public Portable, public Searchable
// documented in Portable
bool write(ConnectionWriter& connection) const override;

// documented in Searchable
bool check(const std::string& key) const override;

// documented in Searchable
Value& find(const std::string& key) const override;

// documented in Searchable
Bottle& findGroup(const std::string& key) const override;

/**
* Equality test.
* @param alt the value to compare against
Expand Down Expand Up @@ -357,8 +356,6 @@ class YARP_os_API Value : public Portable, public Searchable
*/
virtual std::int32_t getCode() const;

bool isNull() const override;

virtual bool isLeaf() const;

/**
Expand Down
4 changes: 4 additions & 0 deletions src/libYARP_os/src/yarp/os/YarpNameSpace.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ class YARP_os_API YarpNameSpace : public NameSpace
ContactStyle style;
ok = NetworkBase::writeToNameServer(cmd, reply, style);
}
////
std::string ss= reply.toString();
fprintf(stderr, ss.c_str());
////
bool fail = (reply.get(0).toString() == "fail") || !ok;
if (fail) {
if (!style.quiet) {
Expand Down
6 changes: 3 additions & 3 deletions src/libYARP_os/src/yarp/os/YarpPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ bool YarpPluginSettings::open(SharedLibraryFactory& factory)
// and a proper name for the DLL
Bottle paths = selector->getSearchPath();
for (size_t i = 0; i < paths.size(); i++) {
Searchable& options = paths.get(i);
std::string path = options.find("path").asString();
std::string ext = options.find("extension").asString();
Searchable* options = paths.get(i).asSearchable();
std::string path = options->find("path").asString();
std::string ext = options->find("extension").asString();
std::string basename = (dll_name.find('.') != std::string::npos) ? name : dll_name;
std::string fn = (fn_name.empty()) ? name : fn_name;

Expand Down
4 changes: 2 additions & 2 deletions src/libYARP_os/src/yarp/os/YarpPluginSelector.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ class YARP_os_API YarpPluginSelector
const yarp::os::Bottle lst = selector.getSelectedPlugins();
for (size_t i = 0; i < lst.size(); i++) {
const yarp::os::Value& options = lst.get(i);
if (name == options.check("name", yarp::os::Value("untitled")).asString()) {
if (name == options.asSearchable()->check("name", yarp::os::Value("untitled")).asString()) {
if (!type.empty()) {
return true;
}
if (type == options.check("type", yarp::os::Value("untitled")).asString()) {
if (type == options.asSearchable()->check("type", yarp::os::Value("untitled")).asString()) {
return true;
}
}
Expand Down
22 changes: 0 additions & 22 deletions src/libYARP_os/src/yarp/os/impl/Storable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,28 +117,6 @@ Storable* Storable::createByCode(std::int32_t id)
return storable;
}

Value& Storable::find(const std::string& key) const
{
YARP_UNUSED(key);
return BottleImpl::getNull();
}

Bottle& Storable::findGroup(const std::string& key) const
{
YARP_UNUSED(key);
return Bottle::getNullBottle();
}

bool Storable::check(const std::string& key) const
{
Bottle& val = findGroup(key);
if (!val.isNull()) {
return true;
}
Value& val2 = find(key);
return !val2.isNull();
}

bool Storable::operator==(const Value& alt) const
{
return toString() == alt.toString();
Expand Down
Loading