YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
SharedLibraryClassApi.h
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
6#ifndef YARP_OS_SHAREDLIBRARYCLASSAPI_H
7#define YARP_OS_SHAREDLIBRARYCLASSAPI_H
8
9#include <yarp/conf/system.h>
10
11#include <yarp/os/Vocab.h>
12
13#include <cstring>
14
15// Be careful loading C++ classes from DLLs. Generally you
16// need an exact or very close match between compilers used
17// to compile those DLLs and your own code.
18
19#define YARP_SHAREDLIBRARYCLASSAPI_PADDING (30 - 2 * (YARP_POINTER_SIZE / 4))
20
21namespace yarp::os {
22
23extern "C" {
24
32{
33public:
34 NetInt32 startCheck; // Constant: this should be 'Y' 'A' 'R' 'P'.
35 // Don't touch anything further if it isn't.
36 NetInt32 structureSize; // size of the SharedLibraryClassApi.
37 // If this doesn't match what you expect,
38 // Don't touch anything further if it isn't.
39 NetInt32 systemVersion; // Overall version of plugin system.
40 // This does *not* cover compiler version etc.
41 void* (*create)(); // Instantiate a plugin object.
42 void (*destroy)(void* obj); // Destroy a plugin object.
43 int (*getVersion)(char* ver, int len); // Plugin-related version.
44 int (*getAbi)(char* abi, int len); // Compiler-related version.
45 int (*getClassName)(char* name, int len); // Name of plugin (subclass).
46 int (*getBaseClassName)(char* name, int len); // Name superclass.
48 NetInt32 endCheck; // Constant: should be 'P' 'L' 'U' 'G'.
49};
51
52} // extern "C"
53
54} // namespace yarp::os
55
56#define YARP_SHARED_CLASS_FN extern "C" YARP_EXPORT
57
74#define YARP_DEFINE_SHARED_SUBCLASS(factoryname, classname, basename) \
75 YARP_SHARED_CLASS_FN void* factoryname##_create() \
76 { \
77 classname* cn = new classname; \
78 basename* bn = dynamic_cast<basename*>(cn); \
79 if (!bn) \
80 delete cn; \
81 return static_cast<void*>(bn); \
82 } \
83 YARP_SHARED_CLASS_FN void factoryname##_destroy(void* obj) \
84 { \
85 classname* cn = dynamic_cast<classname*>(static_cast<basename*>(obj)); \
86 if (cn) \
87 delete cn; \
88 } \
89 YARP_SHARED_CLASS_FN int factoryname##_getVersion(char* ver, int len) \
90 { \
91 return 0; \
92 } \
93 YARP_SHARED_CLASS_FN int factoryname##_getAbi(char* abi, int len) \
94 { \
95 return 0; \
96 } \
97 YARP_SHARED_CLASS_FN int factoryname##_getClassName(char* name, int len) \
98 { \
99 char cname[] = #classname; \
100 strncpy(name, cname, len); \
101 return strlen(cname) + 1; \
102 } \
103 YARP_SHARED_CLASS_FN int factoryname##_getBaseClassName(char* name, int len) \
104 { \
105 char cname[] = #basename; \
106 strncpy(name, cname, len); \
107 return strlen(cname) + 1; \
108 } \
109 YARP_SHARED_CLASS_FN int factoryname(void* api, int len) \
110 { \
111 struct yarp::os::SharedLibraryClassApi* sapi = (struct yarp::os::SharedLibraryClassApi*)api; \
112 if (len < (int)sizeof(yarp::os::SharedLibraryClassApi)) \
113 return -1; \
114 sapi->startCheck = yarp::os::createVocab32('Y', 'A', 'R', 'P'); \
115 sapi->structureSize = sizeof(yarp::os::SharedLibraryClassApi); \
116 sapi->systemVersion = 5; \
117 sapi->create = factoryname##_create; \
118 sapi->destroy = factoryname##_destroy; \
119 sapi->getVersion = factoryname##_getVersion; \
120 sapi->getAbi = factoryname##_getAbi; \
121 sapi->getClassName = factoryname##_getClassName; \
122 sapi->getBaseClassName = factoryname##_getBaseClassName; \
123 for (int i = 0; i < YARP_SHAREDLIBRARYCLASSAPI_PADDING; i++) { \
124 sapi->roomToGrow[i] = 0; \
125 } \
126 sapi->endCheck = yarp::os::createVocab32('P', 'L', 'U', 'G'); \
127 return sapi->startCheck; \
128 }
129// The double cast in the _create() and _destroy() functions are
130// required to ensure that everything works when `basename` is not the
131// first inherited class:
132// _create() will return a valid `basename` or a null pointer if
133// `classname` does not inherit from `basename`.
134// _destroy() will ensure that we are calling `classname` destructor
135// even if `basename` is not the first inherited class. If the
136// dynamic_cast fails, it will not delete the object (that is probably
137// leaked), but it is less dangerous than executing some other random
138// function.
139
140#define YARP_DEFAULT_FACTORY_NAME "yarp_default_factory"
141#define YARP_DEFINE_DEFAULT_SHARED_CLASS(classname) YARP_DEFINE_SHARED_SUBCLASS(yarp_default_factory, classname, classname)
142#define YARP_DEFINE_SHARED_CLASS(factoryname, classname) YARP_DEFINE_SHARED_SUBCLASS(factoryname, classname, classname)
143
144#endif // YARP_OS_SHAREDLIBRARYCLASS_H
#define YARP_SHAREDLIBRARYCLASSAPI_PADDING
A mini-server for performing network communication in the background.
An interface to the operating system, including Port based communication.
std::int32_t NetInt32
Definition of the NetInt32 type.
Definition NetInt32.h:29
Collection of hooks for creating/destroying a plugin.
int(* getVersion)(char *ver, int len)
NetInt32 roomToGrow[(30 - 2 *(8/4))]
int(* getClassName)(char *name, int len)
int(* getAbi)(char *abi, int len)
int(* getBaseClassName)(char *name, int len)
#define YARP_END_PACK
Ends 1 byte packing for structs/classes.
Definition system.h:193
#define YARP_BEGIN_PACK
Starts 1 byte packing for structs/classes.
Definition system.h:192