YARP
Yet Another Robot Platform
SharedLibraryFactory.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
10 
11 #include <yarp/os/Os.h>
12 
13 #include <string>
14 
16  status(STATUS_NONE),
17  returnValue(0),
18  rct(1)
19 {
20  memset(&api, 0, sizeof(SharedLibraryClassApi));
21 }
22 
24  const char* fn_name) :
25  status(STATUS_NONE),
26  returnValue(0),
27  rct(1)
28 {
29  open(dll_name, fn_name);
30 }
31 
33 
34 bool yarp::os::SharedLibraryFactory::open(const char* dll_name, const char* fn_name)
35 {
36  returnValue = 0;
37  name = "";
38  className = "";
39  baseClassName = "";
40  status = STATUS_NONE;
41  error = "";
42  api.startCheck = 0;
43  if (!lib.open(dll_name)) {
44  if (yarp::os::stat(dll_name) != 0) {
45  status = STATUS_LIBRARY_NOT_FOUND;
46  } else {
47  status = STATUS_LIBRARY_NOT_LOADED;
48  }
49  error = lib.error();
50  return false;
51  }
52  void* fn = lib.getSymbol((fn_name != nullptr) ? fn_name : YARP_DEFAULT_FACTORY_NAME);
53  if (fn == nullptr) {
54  status = STATUS_FACTORY_NOT_FOUND;
55  error = lib.error();
56  lib.close();
57  return false;
58  }
59  if (!useFactoryFunction(fn)) {
60  status = STATUS_FACTORY_NOT_FUNCTIONAL;
61  error = "YARP hook in shared library misbehaved";
62  return false;
63  }
64  status = STATUS_OK;
65  name = dll_name;
66 
67  char buf[256];
68  api.getClassName(buf, 256);
69  className = buf;
70  api.getBaseClassName(buf, 256);
71  baseClassName = buf;
72 
73  return true;
74 }
75 
77 {
78  if (returnValue != yarp::os::createVocab('Y', 'A', 'R', 'P')) {
79  return false;
80  }
81  if (api.startCheck != yarp::os::createVocab('Y', 'A', 'R', 'P')) {
82  return false;
83  }
84  if (api.structureSize != sizeof(SharedLibraryClassApi)) {
85  return false;
86  }
87  if (api.systemVersion != 5) {
88  return false;
89  }
90  if (api.endCheck != yarp::os::createVocab('P', 'L', 'U', 'G')) {
91  return false;
92  }
93  return true;
94 }
95 
97 {
98  return status;
99 }
100 
102 {
103  return error;
104 }
105 
107 {
108  return api;
109 }
110 
112 {
113  return rct;
114 }
115 
116 
118 {
119  rct++;
120  return rct;
121 }
122 
124 {
125  rct--;
126  return rct;
127 }
128 
130 {
131  return name;
132 }
133 
135 {
136  return className;
137 }
138 
140 {
141  return baseClassName;
142 }
143 
145 {
146  api.startCheck = 0;
147  if (factory == nullptr) {
148  return false;
149  }
150  returnValue = ((int (*)(void* ptr, int len))factory)(&api, sizeof(SharedLibraryClassApi));
151  return isValid();
152 }
yarp::os::SharedLibraryFactory::getError
std::string getError() const
Get the latest error of the factory.
Definition: SharedLibraryFactory.cpp:101
yarp::os::createVocab
constexpr yarp::conf::vocab32_t createVocab(char a, char b=0, char c=0, char d=0)
Definition: Vocab.h:22
yarp::os::Time::isValid
bool isValid()
Check if time is valid (non-zero).
Definition: Time.cpp:317
yarp::os::SharedLibraryFactory::removeRef
int removeRef()
Decrement the reference count of this factory.
Definition: SharedLibraryFactory.cpp:123
yarp::os::SharedLibraryFactory::open
bool open(const char *dll_name, const char *fn_name=nullptr)
Configure the factory.
Definition: SharedLibraryFactory.cpp:34
YARP_DEFAULT_FACTORY_NAME
#define YARP_DEFAULT_FACTORY_NAME
Definition: SharedLibraryClassApi.h:145
yarp::os::SharedLibraryFactory::SharedLibraryFactory
SharedLibraryFactory()
Constructor for unconfigured factory.
Definition: SharedLibraryFactory.cpp:15
yarp::os::SharedLibraryFactory::getName
std::string getName() const
Get the name associated with this factory.
Definition: SharedLibraryFactory.cpp:129
yarp::os::SharedLibraryClassApi
Collection of hooks for creating/destroying a plugin.
Definition: SharedLibraryClassApi.h:36
Os.h
yarp::os::SharedLibraryFactory::getStatus
int getStatus() const
Get the status of the factory.
Definition: SharedLibraryFactory.cpp:96
yarp::os::SharedLibraryFactory::getReferenceCount
int getReferenceCount() const
Get the current reference count of this factory.
Definition: SharedLibraryFactory.cpp:111
yarp::os::SharedLibraryFactory::getClassName
std::string getClassName() const
Get the type associated with this factory.
Definition: SharedLibraryFactory.cpp:134
yarp::os::stat
int stat(const char *path)
Portable wrapper for the stat() function.
Definition: Os.cpp:88
yarp::os::SharedLibraryFactory::useFactoryFunction
bool useFactoryFunction(void *factory)
Specify function to use as factory.
Definition: SharedLibraryFactory.cpp:144
yarp::os::SharedLibraryFactory::getBaseClassName
std::string getBaseClassName() const
Get the base type associated with this factory.
Definition: SharedLibraryFactory.cpp:139
yarp::os::SharedLibraryFactory::~SharedLibraryFactory
virtual ~SharedLibraryFactory()
Destructor.
SharedLibraryFactory.h
yarp::os::SharedLibraryFactory::isValid
bool isValid() const
Check if factory is configured and present.
Definition: SharedLibraryFactory.cpp:76
yarp::os::SharedLibraryFactory::getApi
const SharedLibraryClassApi & getApi() const
Get the factory API, which has creation/deletion methods.
Definition: SharedLibraryFactory.cpp:106
yarp::os::SharedLibraryFactory::addRef
int addRef()
Increment the reference count of this factory.
Definition: SharedLibraryFactory.cpp:117