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