Run Time Type Information (RTTI) and typeid()

Using RTTI for a Plugin System

How can I use RTTI to implement a plugin system where different types of plugins are loaded dynamically?

Illustration representing computer hardware

RTTI can be incredibly useful for implementing a flexible plugin system. Here's how you might approach this:

Base Plugin Interface

First, define a base Plugin interface:

#include <string>

class Plugin {
 public:
  virtual ~Plugin() = default;
  virtual std::string GetName() const = 0;
  virtual void Execute() = 0;
};

Plugin Manager

Next, create a PluginManager class that can load and manage plugins:

#include <iostream>
#include <memory>
#include <typeinfo>
#include <vector>

class Plugin {/*...*/}; class PluginManager { public: void LoadPlugin(std::unique_ptr<Plugin> plugin) { plugins_.push_back(std::move(plugin)); } void ExecutePlugin(const std::string& name) { for (const auto& plugin : plugins_) { if (plugin->GetName() == name) { std::cout << "Executing plugin: " << name << "\n"; plugin->Execute(); return; } } std::cout << "Plugin not found: " << name << "\n"; } void ListPlugins() { for (const auto& plugin : plugins_) { std::cout << "Plugin: " << plugin->GetName() << ", Type: " << typeid(*plugin).name() << "\n"; } } using PluginCollection = std::vector<std::unique_ptr<Plugin>>; const PluginCollection& GetPlugins() const { return plugins_; } private: PluginCollection plugins_; };

Using RTTI

Now, let's see how RTTI can be used within this system:

#include <iostream>
#include <memory>
#include <typeinfo>
#include <vector>

class Plugin {/*...*/};
class PluginManager {/*...*/}; class AudioPlugin : public Plugin { public: std::string GetName() const override { return "AudioPlugin"; } void Execute() override { std::cout << "Playing audio\n"; } }; class VideoPlugin : public Plugin { public: std::string GetName() const override { return "VideoPlugin"; } void Execute() override { std::cout << "Playing video\n"; } }; int main() { PluginManager manager; manager.LoadPlugin( std::make_unique<AudioPlugin>()); manager.LoadPlugin( std::make_unique<VideoPlugin>()); manager.ListPlugins(); // Using RTTI to check plugin types for (const auto& plugin : manager.GetPlugins()) { if (typeid(*plugin) == typeid(AudioPlugin)) { std::cout << "Found an AudioPlugin\n"; } else if (typeid(*plugin) == typeid(VideoPlugin)) { std::cout << "Found a VideoPlugin\n"; } } manager.ExecutePlugin("AudioPlugin"); manager.ExecutePlugin("VideoPlugin"); }
Plugin: AudioPlugin, Type: class AudioPlugin
Plugin: VideoPlugin, Type: class VideoPlugin
Found an AudioPlugin
Found a VideoPlugin
Executing plugin: AudioPlugin
Playing audio
Executing plugin: VideoPlugin
Playing video

In this example, RTTI allows us to identify the specific types of plugins at runtime. This can be particularly useful if you need to perform type-specific operations or if you're implementing a more complex plugin architecture.

Remember that while RTTI is powerful, it should be used judiciously. In many cases, virtual functions and polymorphism can provide similar functionality with better performance. However, for a plugin system where flexibility is key, RTTI can be a valuable tool.

This Question is from the Lesson:

Run Time Type Information (RTTI) and typeid()

Learn to identify and react to object types at runtime using RTTI, dynamic casting and the typeid() operator

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Run Time Type Information (RTTI) and typeid()

Learn to identify and react to object types at runtime using RTTI, dynamic casting and the typeid() operator

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved