Skip to content

Commit

Permalink
Rename StaticModule to StaticModuleRegistration
Browse files Browse the repository at this point in the history
This class no longer constructs or stores a reference to the actual
module instance, but just registers the type for later construction by
the ModuleRegistry.
  • Loading branch information
Matthew Mott committed Feb 8, 2022
1 parent 90958ed commit 5295019
Show file tree
Hide file tree
Showing 85 changed files with 296 additions and 298 deletions.
56 changes: 27 additions & 29 deletions libs/module/StaticModule.h
Expand Up @@ -3,22 +3,6 @@
#include <functional>
#include "imodule.h"

/**
* greebo: Use a StaticModule to define a static RegisterableModule,
* which enlists itself automatically during application startup.
*
* The template parameter must be a RegisterModule class and
* is automatically registered with the ModuleRegistry by
* the StaticModule constructor.
*
* Since immediate registering is not desired, the constructor
* adds the incoming modules to a static std::list
* for a later routine to add the modules to the registry.
*
* Note: does NOT hold actual shared_ptrs of the RegisterableModule.
*
* Usage: StaticModule<RegisterableModule> myStaticModule;
*/
namespace module
{

Expand All @@ -27,12 +11,12 @@ namespace internal

/**
* Static container holding the modules registered by
* the StaticModule<T> helper. They will be picked up
* and acquired by the ModuleRegistry.
* To support scenarios like the unit test runner (where the
* the StaticModuleRegistration<T> helper. They will be picked up
* and acquired by the ModuleRegistry.
* To support scenarios like the unit test runner (where the
* modules are repeatedly loaded, initialised and shut down
* again) the module list is not cleared after registration,
* such that another startup event will have the full set of
* such that another startup event will have the full set of
* modules properly registered.
*/
typedef std::function<RegisterableModulePtr()> ModuleCreationFunc;
Expand All @@ -52,20 +36,34 @@ class StaticModuleList :

}

template <class ModuleType>
class StaticModule
/**
* @brief Helper object to register a specific RegisterableModule subclass.
*
* This class is intended to be used statically in the .cpp file of a particular module's
* implementing class, to register the module implementation with the ModuleRegistry. Since
* immediate registering is not desired, the constructor adds the incoming modules to a static
* std::list for a later routine to add the modules to the registry.
*
* Note that this object does NOT hold any reference or pointer to the actual module instance. It
* registers the TYPE of module which will later be instantiated and owned by the ModuleRegistry.
*
* @tparam ModuleType
* A RegisterableModule class which is automatically registered with the ModuleRegistry by the
* StaticModuleRegistration constructor.
*/
template <class ModuleType> class StaticModuleRegistration
{
static_assert(std::is_base_of<RegisterableModule, ModuleType>::value, "ModuleType must be of type RegisterableModule");
static_assert(std::is_base_of<RegisterableModule, ModuleType>::value,
"ModuleType must be of type RegisterableModule");

public:
StaticModule()
StaticModuleRegistration()
{
// Add a creator to the list in the backend, it will be called by the registry later
internal::StaticModuleList::Add([]()->RegisterableModulePtr
{
return std::make_shared<ModuleType>();
});
}
internal::StaticModuleList::Add(
[]() -> RegisterableModulePtr { return std::make_shared<ModuleType>(); }
);
}
};

} // namespace module
Expand Down
20 changes: 10 additions & 10 deletions radiant/camera/CameraWndManager.cpp
Expand Up @@ -163,7 +163,7 @@ FloatingCamWndPtr CameraWndManager::createFloatingWindow()

void CameraWndManager::resetCameraAngles(const cmd::ArgumentList& args)
{
doWithActiveCamWnd([](CamWnd& camWnd)
doWithActiveCamWnd([](CamWnd& camWnd)
{
Vector3 angles;
angles[camera::CAMERA_ROLL] = angles[camera::CAMERA_PITCH] = 0;
Expand Down Expand Up @@ -196,7 +196,7 @@ void CameraWndManager::decreaseCameraSpeed(const cmd::ArgumentList& args) {
registry::setValue(RKEY_MOVEMENT_SPEED, movementSpeed);
}

void CameraWndManager::benchmark()
void CameraWndManager::benchmark()
{
doWithActiveCamWnd([](CamWnd& camWnd) { camWnd.benchmark(); });
}
Expand Down Expand Up @@ -274,19 +274,19 @@ void CameraWndManager::moveCameraCmd(const cmd::ArgumentList& args)
return;
}

if (arg == "up")
if (arg == "up")
{
camWnd->moveUpDiscrete(amount);
}
else if (arg == "down")
else if (arg == "down")
{
camWnd->moveDownDiscrete(amount);
}
else if (arg == "left")
else if (arg == "left")
{
camWnd->moveLeftDiscrete(amount);
}
if (arg == "right")
if (arg == "right")
{
camWnd->moveRightDiscrete(amount);
}
Expand Down Expand Up @@ -316,7 +316,7 @@ void CameraWndManager::foreachCamWnd(const std::function<void(CamWnd&)>& action)
_cameras.erase(i++);
continue;
}

++i;
action(*cam);
}
Expand All @@ -325,7 +325,7 @@ void CameraWndManager::foreachCamWnd(const std::function<void(CamWnd&)>& action)
void CameraWndManager::doWithActiveCamWnd(const std::function<void(CamWnd&)>& action)
{
auto camWnd = getActiveCamWnd();

if (camWnd)
{
action(*camWnd);
Expand Down Expand Up @@ -437,7 +437,7 @@ void CameraWndManager::loadCameraStrafeDefinitions()
}

// RegisterableModule implementation
const std::string& CameraWndManager::getName() const
const std::string& CameraWndManager::getName() const
{
static std::string _name("CameraWndManager");
return _name;
Expand Down Expand Up @@ -497,7 +497,7 @@ void CameraWndManager::shutdownModule()
}

// Define the static Camera module
module::StaticModule<CameraWndManager> cameraWndManagerModule;
module::StaticModuleRegistration<CameraWndManager> cameraWndManagerModule;

} // namespace

Expand Down
2 changes: 1 addition & 1 deletion radiant/clipboard/ClipboardModule.cpp
Expand Up @@ -104,6 +104,6 @@ void ClipboardModule::onAppActivated(wxActivateEvent& ev)
ev.Skip();
}

module::StaticModule<ClipboardModule> clipboardModule;
module::StaticModuleRegistration<ClipboardModule> clipboardModule;

}
40 changes: 20 additions & 20 deletions radiant/eventmanager/EventManager.cpp
Expand Up @@ -119,7 +119,7 @@ void EventManager::resetAcceleratorBindings()
loadAcceleratorFromList(shortcutList);
}

IEventPtr EventManager::findEvent(const std::string& name)
IEventPtr EventManager::findEvent(const std::string& name)
{
// Try to lookup the command
auto found = _events.find(name);
Expand Down Expand Up @@ -162,7 +162,7 @@ bool EventManager::alreadyRegistered(const std::string& eventName)
{
return false;
}

rWarning() << "EventManager: Event " << eventName << " already registered!" << std::endl;
return true;
}
Expand Down Expand Up @@ -190,7 +190,7 @@ IEventPtr EventManager::addKeyEvent(const std::string& name, const KeyStateChang
{
return _emptyEvent;
}

IEventPtr event = std::make_shared<KeyEvent>(keyStateChangeCallback);

// Add the new keyevent to the list
Expand All @@ -206,7 +206,7 @@ IEventPtr EventManager::addWidgetToggle(const std::string& name) {
{
return _emptyEvent;
}

IEventPtr event = std::make_shared<WidgetToggle>();

// Add the command to the list
Expand All @@ -224,10 +224,10 @@ IEventPtr EventManager::addRegistryToggle(const std::string& name, const std::st
}

IEventPtr event = std::make_shared<RegistryToggle>(registryKey);
// Add the command to the list

// Add the command to the list
_events[name] = event;

// Return the pointer to the newly created event
return event;
}
Expand Down Expand Up @@ -264,7 +264,7 @@ void EventManager::registerMenuItem(const std::string& eventName, wxMenuItem* it

// Set the accelerator of this menu item
auto& accelerator = findAccelerator(eventName);

Event::setMenuItemAccelerator(item, accelerator);

// Check if we have an event object corresponding to this event name
Expand All @@ -282,7 +282,7 @@ void EventManager::registerMenuItem(const std::string& eventName, wxMenuItem* it

void EventManager::unregisterMenuItem(const std::string& eventName, wxMenuItem* item)
{
for (auto it = _menuItems.lower_bound(eventName);
for (auto it = _menuItems.lower_bound(eventName);
it != _menuItems.end() && it != _menuItems.upper_bound(eventName); ++it)
{
if (it->second != item) continue;
Expand Down Expand Up @@ -418,7 +418,7 @@ void EventManager::connectAccelerator(wxKeyEvent& keyEvent, const std::string& c
connectAccelerator(keyCode, modifierFlags, command);
}

void EventManager::disconnectAccelerator(const std::string& command)
void EventManager::disconnectAccelerator(const std::string& command)
{
auto existing = _accelerators.find(command);

Expand All @@ -439,7 +439,7 @@ void EventManager::disconnectAccelerator(const std::string& command)

void EventManager::setToolItemAccelerator(const std::string& command, const std::string& acceleratorStr)
{
for (auto it = _toolItems.lower_bound(command);
for (auto it = _toolItems.lower_bound(command);
it != _toolItems.end() && it != _toolItems.upper_bound(command); ++it)
{
Event::setToolItemAccelerator(it->second, acceleratorStr);
Expand All @@ -448,19 +448,19 @@ void EventManager::setToolItemAccelerator(const std::string& command, const std:

void EventManager::setMenuItemAccelerator(const std::string& command, const std::string& acceleratorStr)
{
for (auto it = _menuItems.lower_bound(command);
for (auto it = _menuItems.lower_bound(command);
it != _menuItems.end() && it != _menuItems.upper_bound(command); ++it)
{
Event::setMenuItemAccelerator(it->second, acceleratorStr);
}
}

void EventManager::disableEvent(const std::string& eventName)
void EventManager::disableEvent(const std::string& eventName)
{
findEvent(eventName)->setEnabled(false);
}

void EventManager::enableEvent(const std::string& eventName)
void EventManager::enableEvent(const std::string& eventName)
{
findEvent(eventName)->setEnabled(true);
}
Expand All @@ -480,7 +480,7 @@ void EventManager::renameEvent(const std::string& oldEventName, const std::strin
_events.insert(std::make_pair(newEventName, existingEvent));
}

void EventManager::removeEvent(const std::string& eventName)
void EventManager::removeEvent(const std::string& eventName)
{
// Try to lookup the command
auto found = _events.find(eventName);
Expand Down Expand Up @@ -543,7 +543,7 @@ void EventManager::loadAcceleratorFromList(const xml::NodeList& shortcutList)

if (replacement != commandRemap.end())
{
rMessage() << "Mapping shortcut of legacy command " << replacement->first <<
rMessage() << "Mapping shortcut of legacy command " << replacement->first <<
" to " << replacement->second << std::endl;
cmd = replacement->second;
}
Expand Down Expand Up @@ -579,7 +579,7 @@ void EventManager::foreachEvent(IEventVisitor& eventVisitor)
for (const auto& pair : _events)
{
auto accel = _accelerators.find(pair.first);
eventVisitor.visit(pair.first,
eventVisitor.visit(pair.first,
accel != _accelerators.end() ? *accel->second : _emptyAccelerator);
}

Expand All @@ -596,7 +596,7 @@ void EventManager::foreachEvent(IEventVisitor& eventVisitor)
if (signatureIsEmptyOrOptional(signature))
{
auto accel = _accelerators.find(command);
eventVisitor.visit(command,
eventVisitor.visit(command,
accel != _accelerators.end() ? *accel->second : _emptyAccelerator);
}
});
Expand Down Expand Up @@ -655,7 +655,7 @@ EventManager::AcceleratorMap::iterator EventManager::findAccelerator(unsigned in
Accelerator& EventManager::findAccelerator(wxKeyEvent& ev)
{
int keyval = ev.GetKeyCode(); // is always uppercase

auto it = findAccelerator(keyval, wxutil::Modifier::GetStateForKeyEvent(ev));

return it != _accelerators.end() ? *it->second : _emptyAccelerator;
Expand Down Expand Up @@ -756,6 +756,6 @@ std::string EventManager::getEventStr(wxKeyEvent& ev)
}

// Static module registration
module::StaticModule<EventManager> eventManagerModule;
module::StaticModuleRegistration<EventManager> eventManagerModule;

}
2 changes: 1 addition & 1 deletion radiant/eventmanager/MouseToolManager.cpp
Expand Up @@ -279,6 +279,6 @@ void MouseToolManager::onCloseTimerIntervalReached(wxTimerEvent& ev)
}
}

module::StaticModule<MouseToolManager> mouseToolManagerModule;
module::StaticModuleRegistration<MouseToolManager> mouseToolManagerModule;

} // namespace
6 changes: 3 additions & 3 deletions radiant/map/StartupMapLoader.cpp
Expand Up @@ -16,7 +16,7 @@
#include "os/file.h"
#include "registry/registry.h"

namespace map
namespace map
{

void StartupMapLoader::onMainFrameReady()
Expand All @@ -29,7 +29,7 @@ void StartupMapLoader::onMainFrameReady()

for (const std::string& candidate : args)
{
if (os::getExtension(candidate) != "map" &&
if (os::getExtension(candidate) != "map" &&
os::getExtension(candidate) != "mapx") continue;

// We have a map file, check if it exists (and where)
Expand Down Expand Up @@ -115,6 +115,6 @@ void StartupMapLoader::initialiseModule(const IApplicationContext& ctx)
);
}

module::StaticModule<StartupMapLoader> startupMapLoader;
module::StaticModuleRegistration<StartupMapLoader> startupMapLoader;

} // namespace map
2 changes: 1 addition & 1 deletion radiant/settings/LocalisationModule.cpp
Expand Up @@ -59,6 +59,6 @@ void LocalisationModule::shutdownModule()
LocalisationProvider::Instance()->saveLanguageSetting();
}

module::StaticModule<LocalisationModule> localisationModule;
module::StaticModuleRegistration<LocalisationModule> localisationModule;

}
2 changes: 1 addition & 1 deletion radiant/ui/UserInterfaceModule.cpp
Expand Up @@ -450,7 +450,7 @@ void UserInterfaceModule::registerUICommands()
}

// Static module registration
module::StaticModule<UserInterfaceModule> userInterfaceModule;
module::StaticModuleRegistration<UserInterfaceModule> userInterfaceModule;

UserInterfaceModule& GetUserInterfaceModule()
{
Expand Down
2 changes: 1 addition & 1 deletion radiant/ui/common/DialogManager.cpp
Expand Up @@ -119,6 +119,6 @@ IAnimationChooser* DialogManager::createAnimationChooser(wxWindow* parent)
return new MD5AnimationChooser(parent);
}

module::StaticModule<DialogManager> dialogManagerModule;
module::StaticModuleRegistration<DialogManager> dialogManagerModule;

} // namespace ui
2 changes: 1 addition & 1 deletion radiant/ui/einspector/EntityInspector.cpp
Expand Up @@ -1709,6 +1709,6 @@ void EntityInspector::toggle(const cmd::ArgumentList& args)
}

// Define the static EntityInspector module
module::StaticModule<EntityInspector> entityInspectorModule;
module::StaticModuleRegistration<EntityInspector> entityInspectorModule;

} // namespace ui

0 comments on commit 5295019

Please sign in to comment.