YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
SharedLibraryFactory.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
7
8#include <yarp/os/Os.h>
9
10#include <string>
11
13 status(STATUS_NONE),
14 returnValue(0),
15 rct(1)
16{
17 memset(&api, 0, sizeof(SharedLibraryClassApi));
18}
19
21 const char* fn_name) :
22 status(STATUS_NONE),
23 returnValue(0),
24 rct(1)
25{
26 open(dll_name, fn_name);
27}
28
30
31bool yarp::os::SharedLibraryFactory::open(const char* dll_name, const char* fn_name)
32{
33 returnValue = 0;
34 name = "";
35 className = "";
36 baseClassName = "";
37 status = STATUS_NONE;
38 error = "";
39 api.startCheck = 0;
40 if (!lib.open(dll_name)) {
41 if (yarp::os::stat(dll_name) != 0) {
42 status = STATUS_LIBRARY_NOT_FOUND;
43 } else {
44 status = STATUS_LIBRARY_NOT_LOADED;
45 }
46 error = lib.error();
47 return false;
48 }
49 void* fn = lib.getSymbol((fn_name != nullptr) ? fn_name : YARP_DEFAULT_FACTORY_NAME);
50 if (fn == nullptr) {
51 status = STATUS_FACTORY_NOT_FOUND;
52 error = lib.error();
53 lib.close();
54 return false;
55 }
56 if (!useFactoryFunction(fn)) {
57 status = STATUS_FACTORY_NOT_FUNCTIONAL;
58 error = "YARP hook in shared library misbehaved";
59 return false;
60 }
61 status = STATUS_OK;
62 name = dll_name;
63
64 char buf[256];
65 api.getClassName(buf, 256);
66 className = buf;
67 api.getBaseClassName(buf, 256);
68 baseClassName = buf;
69
70 return true;
71}
72
74{
75 if (returnValue != yarp::os::createVocab32('Y', 'A', 'R', 'P')) {
76 return false;
77 }
78 if (api.startCheck != yarp::os::createVocab32('Y', 'A', 'R', 'P')) {
79 return false;
80 }
81 if (api.structureSize != sizeof(SharedLibraryClassApi)) {
82 return false;
83 }
84 if (api.systemVersion != 5) {
85 return false;
86 }
87 if (api.endCheck != yarp::os::createVocab32('P', 'L', 'U', 'G')) {
88 return false;
89 }
90 return true;
91}
92
94{
95 return status;
96}
97
99{
100 return error;
101}
102
107
109{
110 return rct;
111}
112
113
115{
116 rct++;
117 return rct;
118}
119
121{
122 rct--;
123 return rct;
124}
125
127{
128 return name;
129}
130
132{
133 return className;
134}
135
137{
138 return baseClassName;
139}
140
142{
143 api.startCheck = 0;
144 if (factory == nullptr) {
145 return false;
146 }
147 returnValue = ((int (*)(void* ptr, int len))factory)(&api, sizeof(SharedLibraryClassApi));
148 return isValid();
149}
#define YARP_DEFAULT_FACTORY_NAME
A mini-server for performing network communication in the background.
void close() override
Stop port activity.
int addRef()
Increment the reference count of this factory.
std::string getBaseClassName() const
Get the base type associated with this factory.
int getReferenceCount() const
Get the current reference count of this factory.
SharedLibraryFactory()
Constructor for unconfigured factory.
bool useFactoryFunction(void *factory)
Specify function to use as factory.
virtual ~SharedLibraryFactory()
Destructor.
std::string getClassName() const
Get the type associated with this factory.
std::string getName() const
Get the name associated with this factory.
bool isValid() const
Check if factory is configured and present.
bool open(const char *dll_name, const char *fn_name=nullptr)
Configure the factory.
const SharedLibraryClassApi & getApi() const
Get the factory API, which has creation/deletion methods.
int getStatus() const
Get the status of the factory.
int removeRef()
Decrement the reference count of this factory.
std::string getError() const
Get the latest error of the factory.
DumpObj * factory(Bottle &obj)
Definition main.cpp:82
int stat(const char *path)
Portable wrapper for the stat() function.
Definition Os.cpp:78
constexpr yarp::conf::vocab32_t createVocab32(char a, char b=0, char c=0, char d=0)
Create a vocab from chars.
Definition Vocab.h:27
Collection of hooks for creating/destroying a plugin.