Use templates and macros to reduce boilerplate.
PiperOrigin-RevId: 872805912 Change-Id: I6fc1abe283bc809a95d660abc072906b0810aaeb
This commit is contained in:
committed by
Copybara-Service
parent
a79747334b
commit
28ad603f6b
@@ -12,89 +12,62 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "experimental/platform/plugin.h"
|
||||
|
||||
#include <functional>
|
||||
#include <string_view>
|
||||
|
||||
#include <mujoco/mujoco.h>
|
||||
#include "experimental/platform/plugin.h"
|
||||
#include "engine/engine_global_table.h"
|
||||
|
||||
using GuiPlugin = mujoco::platform::GuiPlugin;
|
||||
using ModelPlugin = mujoco::platform::ModelPlugin;
|
||||
|
||||
namespace mujoco::platform {
|
||||
|
||||
void RegisterGuiPlugin(const GuiPlugin* plugin) {
|
||||
if (plugin->name == nullptr || plugin->name[0] == '\0') {
|
||||
template <typename T>
|
||||
void RegisterPlugin(T plugin) {
|
||||
if (plugin.name == nullptr || plugin.name[0] == '\0') {
|
||||
mju_error("Plugin name must not be empty or null.");
|
||||
}
|
||||
GlobalTable<GuiPlugin>::GetSingleton().AppendIfUnique(*plugin);
|
||||
GlobalTable<T>::GetSingleton().AppendIfUnique(plugin);
|
||||
}
|
||||
|
||||
void ForEachGuiPlugin(const std::function<void(GuiPlugin*)>& fn) {
|
||||
auto& table = GlobalTable<GuiPlugin>::GetSingleton();
|
||||
template <typename T>
|
||||
void ForEachPlugin(const std::function<void(T*)>& fn) {
|
||||
auto& table = mujoco::GlobalTable<T>::GetSingleton();
|
||||
for (int i = 0; i < table.count(); ++i) {
|
||||
const GuiPlugin* plugin = table.GetAtSlot(i);
|
||||
fn(const_cast<GuiPlugin*>(plugin));
|
||||
}
|
||||
}
|
||||
|
||||
void RegisterModelPlugin(const ModelPlugin* plugin) {
|
||||
if (plugin->name == nullptr || plugin->name[0] == '\0') {
|
||||
mju_error("Plugin name must not be empty or null.");
|
||||
}
|
||||
GlobalTable<ModelPlugin>::GetSingleton().AppendIfUnique(*plugin);
|
||||
}
|
||||
|
||||
void ForEachModelPlugin(const std::function<void(ModelPlugin*)>& fn) {
|
||||
auto& table = GlobalTable<ModelPlugin>::GetSingleton();
|
||||
for (int i = 0; i < table.count(); ++i) {
|
||||
const ModelPlugin* plugin = table.GetAtSlot(i);
|
||||
fn(const_cast<ModelPlugin*>(plugin));
|
||||
const T* plugin = table.GetAtSlot(i);
|
||||
fn(const_cast<T*>(plugin));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace mujoco::platform
|
||||
|
||||
using mujoco::GlobalTable;
|
||||
using GuiPlugin = mujoco::platform::GuiPlugin;
|
||||
using ModelPlugin = mujoco::platform::ModelPlugin;
|
||||
#define MUJOCO_SPECIALIZE_PLUGIN(PLUGIN, NAME) \
|
||||
template <> \
|
||||
const char* mujoco::GlobalTable<PLUGIN>::HumanReadableTypeName() { \
|
||||
return NAME; \
|
||||
} \
|
||||
template <> \
|
||||
std::string_view mujoco::GlobalTable<PLUGIN>::ObjectKey(const PLUGIN& p) { \
|
||||
return std::string_view(p.name); \
|
||||
} \
|
||||
template <> \
|
||||
bool mujoco::GlobalTable<PLUGIN>::ObjectEqual(const PLUGIN& p1, \
|
||||
const PLUGIN& p2) { \
|
||||
return CaseInsensitiveEqual(p1.name, p2.name); \
|
||||
} \
|
||||
template <> \
|
||||
bool mujoco::GlobalTable<PLUGIN>::CopyObject(PLUGIN& dst, const PLUGIN& src, \
|
||||
ErrorMessage& err) { \
|
||||
dst = src; \
|
||||
return true; \
|
||||
} \
|
||||
namespace mujoco::platform { \
|
||||
template void RegisterPlugin<PLUGIN>(PLUGIN plugin); \
|
||||
template void ForEachPlugin<PLUGIN>(const std::function<void(PLUGIN*)>& fn); \
|
||||
}
|
||||
|
||||
template <>
|
||||
const char* GlobalTable<GuiPlugin>::HumanReadableTypeName() {
|
||||
return "gui plugin";
|
||||
}
|
||||
|
||||
template <>
|
||||
std::string_view GlobalTable<GuiPlugin>::ObjectKey(const GuiPlugin& plugin) {
|
||||
return std::string_view(plugin.name);
|
||||
}
|
||||
|
||||
template <>
|
||||
bool GlobalTable<GuiPlugin>::ObjectEqual(const GuiPlugin& p1, const GuiPlugin& p2) {
|
||||
return CaseInsensitiveEqual(p1.name, p2.name);
|
||||
}
|
||||
|
||||
template <>
|
||||
bool GlobalTable<GuiPlugin>::CopyObject(GuiPlugin& dst, const GuiPlugin& src, ErrorMessage& err) {
|
||||
dst = src;
|
||||
return true;
|
||||
}
|
||||
|
||||
template <>
|
||||
const char* GlobalTable<ModelPlugin>::HumanReadableTypeName() {
|
||||
return "model plugin";
|
||||
}
|
||||
|
||||
template <>
|
||||
std::string_view GlobalTable<ModelPlugin>::ObjectKey(const ModelPlugin& plugin) {
|
||||
return std::string_view(plugin.name);
|
||||
}
|
||||
|
||||
template <>
|
||||
bool GlobalTable<ModelPlugin>::ObjectEqual(const ModelPlugin& p1, const ModelPlugin& p2) {
|
||||
return CaseInsensitiveEqual(p1.name, p2.name);
|
||||
}
|
||||
|
||||
template <>
|
||||
bool GlobalTable<ModelPlugin>::CopyObject(ModelPlugin& dst, const ModelPlugin& src, ErrorMessage& err) {
|
||||
dst = src;
|
||||
return true;
|
||||
}
|
||||
MUJOCO_SPECIALIZE_PLUGIN(GuiPlugin, "gui plugin");
|
||||
MUJOCO_SPECIALIZE_PLUGIN(ModelPlugin, "model plugin");
|
||||
|
||||
@@ -20,10 +20,16 @@
|
||||
|
||||
namespace mujoco::platform {
|
||||
|
||||
// Important: Do not inherit from these plugin structs. They are copied by value
|
||||
// and therefore any derived classes will be sliced. We assume plugins are
|
||||
// effectively globals and so any pointers will be valid for the lifetime
|
||||
// of the process.
|
||||
// Registers plugins with the global registry. The plugins must have a
|
||||
// case-insensitive unique name for the plugin type. Note that plugins are
|
||||
// copied by value, so do not use inheritance.
|
||||
template <typename T>
|
||||
void RegisterPlugin(T plugin);
|
||||
|
||||
// Executes the given function for each registered plugin of type T.
|
||||
template <typename T>
|
||||
void ForEachPlugin(const std::function<void(T*)>& fn);
|
||||
|
||||
|
||||
// Plugin for processing custom UI windows. The plugin will be listed in the
|
||||
// "Plugins" main menu and, when selected, an ImGui window will be opened with
|
||||
@@ -47,8 +53,9 @@ struct GuiPlugin final {
|
||||
void* data = nullptr;
|
||||
};
|
||||
|
||||
// Plugin for loading and updating models.
|
||||
struct ModelPlugin final {
|
||||
using GetModelToLoadFn = const char* (*)(ModelPlugin * self, int* size,
|
||||
using GetModelToLoadFn = const char* (*)(ModelPlugin* self, int* size,
|
||||
char* content_type,
|
||||
int content_type_size,
|
||||
char* model_name,
|
||||
@@ -75,20 +82,6 @@ struct ModelPlugin final {
|
||||
void* data = nullptr;
|
||||
};
|
||||
|
||||
// Registers a plugin with a global registry. The plugin must have a
|
||||
// case-insensitive unique name.
|
||||
void RegisterGuiPlugin(const GuiPlugin* plugin);
|
||||
|
||||
// Executes the given function for each registered plugin.
|
||||
void ForEachGuiPlugin(const std::function<void(GuiPlugin*)>& fn);
|
||||
|
||||
// Registers a plugin with a global registry. The plugin must have a
|
||||
// case-insensitive unique name.
|
||||
void RegisterModelPlugin(const ModelPlugin* plugin);
|
||||
|
||||
// Executes the given function for each registered plugin.
|
||||
void ForEachModelPlugin(const std::function<void(ModelPlugin*)>& fn);
|
||||
|
||||
} // namespace mujoco::platform
|
||||
|
||||
#endif // MUJOCO_SRC_EXPERIMENTAL_PLATFORM_PLUGIN_H_
|
||||
|
||||
@@ -219,6 +219,6 @@ mjPLUGIN_LIB_INIT {
|
||||
plugin.update = [](mujoco::platform::GuiPlugin* self) {
|
||||
mjr_updateGui(nullptr);
|
||||
};
|
||||
mujoco::platform::RegisterGuiPlugin(&plugin);
|
||||
mujoco::platform::RegisterPlugin(plugin);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -227,7 +227,7 @@ if (spec_op_) {
|
||||
spec_op_ = nullptr;
|
||||
}
|
||||
|
||||
platform::ForEachModelPlugin([&](platform::ModelPlugin* plugin) {
|
||||
platform::ForEachPlugin<platform::ModelPlugin>([&](auto* plugin) {
|
||||
if (plugin->post_model_loaded) {
|
||||
plugin->post_model_loaded(plugin, model_path_.c_str());
|
||||
}
|
||||
@@ -271,7 +271,7 @@ void App::UpdatePhysics() {
|
||||
}
|
||||
|
||||
bool stepped = false;
|
||||
platform::ForEachModelPlugin([&](platform::ModelPlugin* plugin) {
|
||||
platform::ForEachPlugin<platform::ModelPlugin>([&](auto* plugin) {
|
||||
if (plugin->do_update) {
|
||||
if (plugin->do_update(plugin, model(), data())) {
|
||||
stepped = true;
|
||||
@@ -392,7 +392,7 @@ void App::ProcessPendingLoads() {
|
||||
}
|
||||
|
||||
// Check plugins to see if we need to load a new model.
|
||||
platform::ForEachModelPlugin([&](platform::ModelPlugin* plugin) {
|
||||
platform::ForEachPlugin<platform::ModelPlugin>([&](auto* plugin) {
|
||||
if (plugin->get_model_to_load) {
|
||||
char model_name[1000] = "";
|
||||
char content_type[1000] = "";
|
||||
@@ -781,7 +781,7 @@ void App::LoadSettings() {
|
||||
|
||||
platform::KeyValues plugin_names =
|
||||
platform::ReadIniSection(settings, "[Studio][Plugins]");
|
||||
platform::ForEachGuiPlugin([&](platform::GuiPlugin* plugin) {
|
||||
platform::ForEachPlugin<platform::GuiPlugin>([&](auto* plugin) {
|
||||
auto it = plugin_names.find(plugin->name);
|
||||
if (it != plugin_names.end()) {
|
||||
plugin->active = std::stoi(it->second) != 0;
|
||||
@@ -797,7 +797,7 @@ void App::SaveSettings() {
|
||||
platform::AppendIniSection(settings, "[Studio][UX]", ui_.ToDict());
|
||||
|
||||
platform::KeyValues plugin_names;
|
||||
platform::ForEachGuiPlugin([&](platform::GuiPlugin* plugin) {
|
||||
platform::ForEachPlugin<platform::GuiPlugin>([&](auto* plugin) {
|
||||
plugin_names[plugin->name] = std::to_string((int)plugin->active);
|
||||
});
|
||||
platform::AppendIniSection(settings, "[Studio][Plugins]", plugin_names);
|
||||
@@ -977,7 +977,7 @@ void App::BuildGui() {
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
platform::ForEachGuiPlugin([](platform::GuiPlugin* plugin) {
|
||||
platform::ForEachPlugin<platform::GuiPlugin>([](auto* plugin) {
|
||||
if (!plugin->update) {
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user