SenseApis: Define descriptors exported by libs
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
#ifndef SENSE_API_PROVIDER_DESC_H
|
||||
#define SENSE_API_PROVIDER_DESC_H
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <dlfcn.h>
|
||||
#include <functional>
|
||||
#include <user/senseApiDesc.h>
|
||||
|
||||
namespace hk {
|
||||
namespace sense_api {
|
||||
|
||||
/* C++ version of the C struct above, which Harikoff uses to manage the
|
||||
* lib and connect implexors to it.
|
||||
*/
|
||||
class SenseApiDesc
|
||||
{
|
||||
public:
|
||||
class ExportedImplexorApiDesc
|
||||
{
|
||||
public:
|
||||
const std::string name;
|
||||
};
|
||||
|
||||
public:
|
||||
SenseApiDesc(const CSenseApiDesc& cDesc)
|
||||
: cDescriptor(cDesc), name(cDesc.name)
|
||||
{}
|
||||
|
||||
std::reference_wrapper<const CSenseApiDesc> cDescriptor;
|
||||
std::string name;
|
||||
// These options affect the sense api lib's behaviour globally.
|
||||
std::vector<std::string> options;
|
||||
// These are the implexors whose APIs this lib exports.
|
||||
std::vector<ExportedImplexorApiDesc> exportedImplexorApis;
|
||||
};
|
||||
|
||||
class SenseApiLib
|
||||
{
|
||||
public:
|
||||
typedef const CSenseApiDesc *(SenseApiDescGetterFn)(void);
|
||||
|
||||
public:
|
||||
SenseApiLib(const std::string& path)
|
||||
: libraryPath(path), handle(nullptr, &dlclose)
|
||||
{}
|
||||
|
||||
public:
|
||||
std::string libraryPath;
|
||||
std::unique_ptr<void, decltype(&dlclose)> handle;
|
||||
|
||||
/**
|
||||
* @brief Every sense API lib is required to provide a function that returns
|
||||
* a CSenseApiDesc struct. This struct states which API the lib uses to
|
||||
* connect Harikoff to the sense provider it supports.
|
||||
*
|
||||
* This getter function should be visible to dlsym() so that Harikoff can
|
||||
* find it in the lib after loading it, and call it.
|
||||
*/
|
||||
SenseApiDescGetterFn *getSenseApiDescriptor;
|
||||
|
||||
/**
|
||||
* @brief Harikoff will call the `getSenseApiDescriptor` getter function and
|
||||
* use the data it provides in order to fill out this descriptor.
|
||||
*/
|
||||
std::unique_ptr<SenseApiDesc> apiDescriptor;
|
||||
};
|
||||
|
||||
} // namespace sense_api
|
||||
} // namespace hk
|
||||
|
||||
#endif // SENSE_API_PROVIDER_DESC_H
|
||||
@@ -7,63 +7,35 @@
|
||||
#include <string>
|
||||
#include <optional>
|
||||
#include <functional>
|
||||
#include <senseApis/senseApiLib.h>
|
||||
|
||||
struct SenseApiInstance {
|
||||
int a;
|
||||
|
||||
bool operator==(const SenseApiInstance& other) const {
|
||||
return a == other.a;
|
||||
}
|
||||
};
|
||||
|
||||
class SenseApiLib {
|
||||
public:
|
||||
std::string libraryPath;
|
||||
std::unique_ptr<void, decltype(&dlclose)> handle;
|
||||
|
||||
SenseApiLib(const std::string& path)
|
||||
: libraryPath(path), handle(nullptr, &dlclose) {}
|
||||
};
|
||||
namespace hk {
|
||||
namespace sense_api {
|
||||
|
||||
class SenseApiManager {
|
||||
public:
|
||||
static SenseApiManager& getInstance()
|
||||
{
|
||||
static SenseApiManager instance
|
||||
{
|
||||
#ifdef CONFIG_SENSEAPI_FOO
|
||||
"builtinApi1",
|
||||
#endif
|
||||
#ifdef CONFIG_SENSEAPI_BAR
|
||||
"builtinApi2",
|
||||
#endif
|
||||
// X11 XCB is always built-in.
|
||||
"x11-xcb"
|
||||
};
|
||||
|
||||
static SenseApiManager instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
void registerSenseApi(const SenseApiInstance& apiInstance);
|
||||
void unregisterSenseApi(const SenseApiInstance& apiInstance);
|
||||
|
||||
SenseApiLib& loadSenseApiLib(const std::string& libraryPath);
|
||||
std::optional<std::reference_wrapper<SenseApiLib>> getSenseApiLib(
|
||||
const std::string& libraryPath);
|
||||
void unloadSenseApiLib(const std::string& libraryPath);
|
||||
|
||||
private:
|
||||
SenseApiManager() = delete;
|
||||
SenseApiManager(std::initializer_list<std::string> builtinApis)
|
||||
: builtinSenseApis(builtinApis) {}
|
||||
SenseApiManager() = default;
|
||||
~SenseApiManager() = default;
|
||||
|
||||
SenseApiManager(const SenseApiManager&) = delete;
|
||||
SenseApiManager& operator=(const SenseApiManager&) = delete;
|
||||
|
||||
std::vector<std::unique_ptr<SenseApiInstance>> senseApiInstances;
|
||||
std::vector<std::unique_ptr<SenseApiLib>> senseApiLibs;
|
||||
const std::vector<std::string> builtinSenseApis;
|
||||
};
|
||||
|
||||
} // namespace sense_api
|
||||
} // namespace hk
|
||||
|
||||
#endif // SENSE_API_MANAGER_H
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#include <dlfcn.h>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <optional>
|
||||
#include <senseApis/senseApiManager.h>
|
||||
#include <senseApis/senseApiLib.h>
|
||||
|
||||
std::vector<std::unique_ptr<SenseApiLib>> senseApiLibs;
|
||||
std::vector<std::unique_ptr<SenseApiInstance>> senseApiInstances;
|
||||
namespace hk {
|
||||
namespace sense_api {
|
||||
|
||||
SenseApiLib& SenseApiManager::loadSenseApiLib(const std::string& libraryPath)
|
||||
{
|
||||
@@ -29,8 +29,8 @@ SenseApiLib& SenseApiManager::loadSenseApiLib(const std::string& libraryPath)
|
||||
return *senseApiLibs.back();
|
||||
}
|
||||
|
||||
std::optional<std::reference_wrapper<SenseApiLib>> SenseApiManager::getSenseApiLib(
|
||||
const std::string& libraryPath)
|
||||
std::optional<std::reference_wrapper<SenseApiLib>>
|
||||
SenseApiManager::getSenseApiLib(const std::string& libraryPath)
|
||||
{
|
||||
auto it = std::find_if(senseApiLibs.begin(), senseApiLibs.end(),
|
||||
[&libPath = libraryPath](const std::unique_ptr<SenseApiLib>& lib) {
|
||||
@@ -60,39 +60,5 @@ void SenseApiManager::unloadSenseApiLib(const std::string& libraryPath)
|
||||
<< libraryPath << '\n';
|
||||
}
|
||||
|
||||
void SenseApiManager::registerSenseApi(const SenseApiInstance& apiInstance)
|
||||
{
|
||||
auto it = std::find_if(
|
||||
senseApiInstances.begin(), senseApiInstances.end(),
|
||||
[&apiInstance](const std::unique_ptr<SenseApiInstance>& instance) {
|
||||
return const_cast<const SenseApiInstance&>(*instance) == apiInstance;
|
||||
}
|
||||
);
|
||||
|
||||
if (it != senseApiInstances.end())
|
||||
{
|
||||
std::cerr << std::string(__func__)
|
||||
+ ": Sense API Instance already registered.\n";
|
||||
return;
|
||||
}
|
||||
|
||||
senseApiInstances.push_back(std::make_unique<SenseApiInstance>(apiInstance));
|
||||
}
|
||||
|
||||
void SenseApiManager::unregisterSenseApi(const SenseApiInstance& apiInstance)
|
||||
{
|
||||
auto it = std::find_if(
|
||||
senseApiInstances.begin(), senseApiInstances.end(),
|
||||
[&apiInstance](const std::unique_ptr<SenseApiInstance>& instance) {
|
||||
return const_cast<const SenseApiInstance&>(*instance) == apiInstance;
|
||||
}
|
||||
);
|
||||
|
||||
if (it == senseApiInstances.end())
|
||||
{
|
||||
std::cerr << std::string(__func__) + ": Sense API Instance not found.\n";
|
||||
return;
|
||||
}
|
||||
|
||||
senseApiInstances.erase(it);
|
||||
}
|
||||
} // namespace sense_api
|
||||
} // namespace hk
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef __USER_SENSE_API_LIB_H__
|
||||
#define __USER_SENSE_API_LIB_H__
|
||||
|
||||
#define HK_SENSE_API_DESC_GETTER_FN_NAME "getSenseApiDescriptor"
|
||||
|
||||
/* Exported by all sense API Libraries to tell Harikoff what API the lib uses
|
||||
* to connect to providers; and also to state which implexor APIs it exports.
|
||||
*/
|
||||
struct CExportedImplexorApiDesc
|
||||
{
|
||||
const char *name;
|
||||
};
|
||||
|
||||
struct CSenseApiDesc
|
||||
{
|
||||
/* Shortname that identifies the API used by this lib to talk to the sense
|
||||
* provider.
|
||||
*/
|
||||
const char *name;
|
||||
// These are the implexors whose APIs this lib exports.
|
||||
CExportedImplexorApiDesc *exportedImplexorApis;
|
||||
};
|
||||
|
||||
#endif // __USER_SENSE_API_LIB_H__
|
||||
Reference in New Issue
Block a user