[sim] Add API for extensions to discover each other (#2681)

This commit is contained in:
Peter Johnson
2020-09-03 10:09:01 -07:00
committed by GitHub
parent 1593eb4d47
commit de0277713b
2 changed files with 46 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
/* Copyright (c) 2017-2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
@@ -44,6 +44,31 @@ int HAL_LoadOneExtension(const char* library);
*/
int HAL_LoadExtensions(void);
/**
* Registers an extension such that other extensions can discover it.
*
* The passed data pointer is retained and the extension must keep this
* pointer valid.
*
* @param name extension name (may embed version number)
* @param data data pointer
*/
void HAL_RegisterExtension(const char* name, void* data);
/**
* Registers an extension registration listener function. The function will
* be called immediately with any currently registered extensions, and will
* be called later when any additional extensions are registered.
*
* @param param parameter data to pass to callback function
* @param func callback function to be called for each registered extension;
* parameters are the parameter data, extension name, and extension
* data pointer passed to HAL_RegisterExtension()
*/
void HAL_RegisterExtensionListener(void* param,
void (*func)(void*, const char* name,
void* data));
/**
* Enables or disables the message saying no HAL extensions were found.
*

View File

@@ -7,6 +7,8 @@
#include "hal/Extensions.h"
#include <vector>
#include <wpi/Path.h>
#include <wpi/SmallString.h>
#include <wpi/StringRef.h>
@@ -37,6 +39,10 @@
#define DLERROR dlerror()
#endif
static std::vector<std::pair<const char*, void*>> gExtensionRegistry;
static std::vector<std::pair<void*, void (*)(void*, const char*, void*)>>
gExtensionListeners;
namespace hal {
namespace init {
void InitializeExtensions() {}
@@ -116,6 +122,20 @@ int HAL_LoadExtensions(void) {
return rc;
}
void HAL_RegisterExtension(const char* name, void* data) {
gExtensionRegistry.emplace_back(name, data);
for (auto&& listener : gExtensionListeners)
listener.second(listener.first, name, data);
}
void HAL_RegisterExtensionListener(void* param,
void (*func)(void*, const char* name,
void* data)) {
gExtensionListeners.emplace_back(param, func);
for (auto&& extension : gExtensionRegistry)
func(param, extension.first, extension.second);
}
void HAL_SetShowExtensionsNotFoundMessages(HAL_Bool showMessage) {
GetShowNotFoundMessage() = showMessage;
}