diff --git a/src/experimental/platform/plugin.cc b/src/experimental/platform/plugin.cc index 13c262c1..fb790f63 100644 --- a/src/experimental/platform/plugin.cc +++ b/src/experimental/platform/plugin.cc @@ -12,89 +12,62 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "experimental/platform/plugin.h" + #include #include #include -#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 +void RegisterPlugin(T plugin) { + if (plugin.name == nullptr || plugin.name[0] == '\0') { mju_error("Plugin name must not be empty or null."); } - GlobalTable::GetSingleton().AppendIfUnique(*plugin); + GlobalTable::GetSingleton().AppendIfUnique(plugin); } -void ForEachGuiPlugin(const std::function& fn) { - auto& table = GlobalTable::GetSingleton(); +template +void ForEachPlugin(const std::function& fn) { + auto& table = mujoco::GlobalTable::GetSingleton(); for (int i = 0; i < table.count(); ++i) { - const GuiPlugin* plugin = table.GetAtSlot(i); - fn(const_cast(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::GetSingleton().AppendIfUnique(*plugin); -} - -void ForEachModelPlugin(const std::function& fn) { - auto& table = GlobalTable::GetSingleton(); - for (int i = 0; i < table.count(); ++i) { - const ModelPlugin* plugin = table.GetAtSlot(i); - fn(const_cast(plugin)); + const T* plugin = table.GetAtSlot(i); + fn(const_cast(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::HumanReadableTypeName() { \ + return NAME; \ + } \ + template <> \ + std::string_view mujoco::GlobalTable::ObjectKey(const PLUGIN& p) { \ + return std::string_view(p.name); \ + } \ + template <> \ + bool mujoco::GlobalTable::ObjectEqual(const PLUGIN& p1, \ + const PLUGIN& p2) { \ + return CaseInsensitiveEqual(p1.name, p2.name); \ + } \ + template <> \ + bool mujoco::GlobalTable::CopyObject(PLUGIN& dst, const PLUGIN& src, \ + ErrorMessage& err) { \ + dst = src; \ + return true; \ + } \ + namespace mujoco::platform { \ + template void RegisterPlugin(PLUGIN plugin); \ + template void ForEachPlugin(const std::function& fn); \ + } -template <> -const char* GlobalTable::HumanReadableTypeName() { - return "gui plugin"; -} - -template <> -std::string_view GlobalTable::ObjectKey(const GuiPlugin& plugin) { - return std::string_view(plugin.name); -} - -template <> -bool GlobalTable::ObjectEqual(const GuiPlugin& p1, const GuiPlugin& p2) { - return CaseInsensitiveEqual(p1.name, p2.name); -} - -template <> -bool GlobalTable::CopyObject(GuiPlugin& dst, const GuiPlugin& src, ErrorMessage& err) { - dst = src; - return true; -} - -template <> -const char* GlobalTable::HumanReadableTypeName() { - return "model plugin"; -} - -template <> -std::string_view GlobalTable::ObjectKey(const ModelPlugin& plugin) { - return std::string_view(plugin.name); -} - -template <> -bool GlobalTable::ObjectEqual(const ModelPlugin& p1, const ModelPlugin& p2) { - return CaseInsensitiveEqual(p1.name, p2.name); -} - -template <> -bool GlobalTable::CopyObject(ModelPlugin& dst, const ModelPlugin& src, ErrorMessage& err) { - dst = src; - return true; -} +MUJOCO_SPECIALIZE_PLUGIN(GuiPlugin, "gui plugin"); +MUJOCO_SPECIALIZE_PLUGIN(ModelPlugin, "model plugin"); diff --git a/src/experimental/platform/plugin.h b/src/experimental/platform/plugin.h index 824b9793..f10fd641 100644 --- a/src/experimental/platform/plugin.h +++ b/src/experimental/platform/plugin.h @@ -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 +void RegisterPlugin(T plugin); + +// Executes the given function for each registered plugin of type T. +template +void ForEachPlugin(const std::function& 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& 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& fn); - } // namespace mujoco::platform #endif // MUJOCO_SRC_EXPERIMENTAL_PLATFORM_PLUGIN_H_ diff --git a/src/experimental/platform/renderer.cc b/src/experimental/platform/renderer.cc index b1b39e52..6fe16730 100644 --- a/src/experimental/platform/renderer.cc +++ b/src/experimental/platform/renderer.cc @@ -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 diff --git a/src/experimental/studio/app.cc b/src/experimental/studio/app.cc index 2454821f..6da86d2f 100644 --- a/src/experimental/studio/app.cc +++ b/src/experimental/studio/app.cc @@ -227,7 +227,7 @@ if (spec_op_) { spec_op_ = nullptr; } - platform::ForEachModelPlugin([&](platform::ModelPlugin* plugin) { + platform::ForEachPlugin([&](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([&](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([&](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([&](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([&](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([](auto* plugin) { if (!plugin->update) { return; }