VR-Engage  2.2
Loading...
Searching...
No Matches
vreChatCommandsManager.h
Go to the documentation of this file.
1/******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4******************************************************************************/
5
6//! \file vreChatCommandsManager.h
7//! \brief Defines the chat command system for VR-Engage
8//!
9//! This file contains the DtChatCommand and DtChatCommandsManager classes
10//! which provide the infrastructure for processing text commands entered
11//! in the chat window. These commands allow users to execute various
12//! actions through a simple command-line interface within the application.
13
14#pragma once
15
17
18namespace makVre
19{
20//! \brief Forward declaration of the player station application class
22
23//! \brief Abstract base class for chat commands in VR-Engage
24//!
25//! DtChatCommand defines the interface for all executable chat commands
26//! in the application. Derived classes implement specific command functionality
27//! through the execute method. Commands can be entered in the chat window
28//! with a leading slash followed by the command name and any parameters.
29class PLAYERSTATION_DLL DtChatCommand : public std::enable_shared_from_this<DtChatCommand>
30{
31public:
32 //! \brief Virtual destructor
33 //!
34 //! Default destructor for proper cleanup of derived command classes.
35 ~DtChatCommand() = default;
36
37 //! \brief Executes the command with given parameters
38 //! \param parameters String containing parameters passed to the command
39 //! \return True if the command executed successfully, false otherwise
40 //!
41 //! Pure virtual method that must be implemented by derived classes to
42 //! provide the actual command functionality. The parameters string
43 //! contains everything after the command name.
44 virtual bool execute(const std::string& parameters) = 0;
45
46 //! \brief Gets a weak pointer to this command
47 //! \return Weak pointer to this command instance
48 //!
49 //! Returns a weak_ptr to this command instance, which helps
50 //! prevent circular references while allowing access to the command.
51 virtual std::weak_ptr<DtChatCommand> getWeak() { return shared_from_this(); }
52
53 //! \brief Gets help text for the command
54 //! \param parameters Optional parameters to customize help output
55 //! \return String containing help text for the command
56 //!
57 //! Returns documentation about how to use the command. The default
58 //! implementation simply returns the parameters string, but derived
59 //! classes should override this to provide meaningful help text.
60 virtual std::string help(const std::string& parameters) { return parameters; };
61
62 //! \brief Checks if this command should be visible in help listings
63 //! \return True if the command should appear in help, false otherwise
64 //!
65 //! Determines whether this command should be included in the list of
66 //! available commands when users request help. The default implementation
67 //! returns false, meaning the command is hidden unless overridden.
68 virtual bool showInHelp() const { return false; };
69
70 //! \brief Utility function to split a string into tokens
71 //! \param stringToSplit String to be split
72 //! \param tokenized Vector to store the resulting tokens
73 //! \param delimiter Character to use as the splitting delimiter
74 //! \return True if the string was successfully split, false otherwise
75 //!
76 //! Splits a string into tokens based on the specified delimiter.
77 //! This is useful for parsing command parameters into individual arguments.
78 static bool splitString(
79 const std::string& stringToSplit, std::vector<std::string>& tokenized, const char delimiter = ' ');
80};
81
82//! \brief Manager class for chat commands in VR-Engage
83//!
84//! DtChatCommandsManager provides a centralized registry of available
85//! chat commands. It handles command registration, execution, and help
86//! text generation. When a user enters a command in the chat window,
87//! this manager locates and executes the appropriate command handler.
89{
90public:
91 //! \brief Constructor
92 //! \param app Reference to the player station application
93 //!
94 //! Creates a new chat commands manager associated with the given application.
95 //! The manager starts with no registered commands.
97
98 //! \brief Destructor
99 //!
100 //! Cleans up resources, including any owned command objects.
102
103 //! \brief Deleted copy constructor and assignment operator
104 //! @{
105 //! These operations are not supported for the command manager.
108 //! @}
109
110 //! \brief Registers a new chat command
111 //! \param commandString String that triggers this command (without the leading slash)
112 //! \param command Shared pointer to the command implementation
113 //! \param takeOwnership If true, the manager takes ownership of the command object
114 //! \return True if the command was successfully added, false otherwise
115 //!
116 //! Adds a new command to the registry. If takeOwnership is true, the manager
117 //! will maintain a strong reference to the command and handle its lifetime.
118 //! If false, the manager only keeps a weak reference, and the caller must
119 //! ensure the command object remains valid for as long as it's registered.
120 virtual bool addCommand(
121 std::string commandString, std::shared_ptr<DtChatCommand> command, bool takeOwnership = false);
122
123 //! \brief Unregisters a chat command
124 //! \param commandString String that triggers the command to remove
125 //!
126 //! Removes a command from the registry. The command string is case-insensitive,
127 //! so commands with the same spelling but different capitalization will be
128 //! treated as identical. If the manager owned the command, it will be released.
129 virtual void removeCommand(std::string commandString);
130
131 //! \brief Executes a command from a console input string
132 //! \param consoleString Full string entered in the chat/console
133 //! \return True if a valid command was found and executed, false otherwise
134 //!
135 //! Parses the input string to extract the command and its parameters,
136 //! then executes the command if found. Commands are case-insensitive.
137 //! The expected format is "/command parameters", where the leading
138 //! slash identifies it as a command rather than normal chat text.
139 virtual bool runCommand(std::string consoleString);
140
141 //! \brief Generates help text for available commands
142 //! \param parameters Optional parameters to filter or customize help output
143 //! \return String containing help text for all visible commands
144 //!
145 //! Creates a formatted string containing help information for all
146 //! registered commands that have showInHelp() returning true.
147 //! This is typically shown when the user enters "/help" in the chat.
148 virtual std::string helpText(const std::string& parameters) const;
149
150protected:
151 //! \brief Map of command strings to weak pointers to commands
152 //!
153 //! Contains all registered commands that are not owned by the manager.
154 //! The weak pointers prevent circular references but require external
155 //! lifetime management for the command objects.
156 std::unordered_map<std::string, std::weak_ptr<DtChatCommand>> myCommandMap;
157
158 //! \brief Map of command strings to shared pointers to commands
159 //!
160 //! Contains all registered commands that are owned by the manager.
161 //! The shared pointers ensure the command objects remain valid as
162 //! long as they are registered.
163 std::unordered_map<std::string, std::shared_ptr<DtChatCommand>> myOwnedCommandMap;
164
165 //! \brief Reference to the player station application
166 //!
167 //! Provides access to application services and state needed by commands.
169};
170
171} // namespace makVre
Abstract base class for chat commands in VR-Engage.
Definition vreChatCommandsManager.h:30
virtual std::string help(const std::string &parameters)
Gets help text for the command.
Definition vreChatCommandsManager.h:60
static bool splitString(const std::string &stringToSplit, std::vector< std::string > &tokenized, const char delimiter=' ')
Utility function to split a string into tokens.
virtual std::weak_ptr< DtChatCommand > getWeak()
Gets a weak pointer to this command.
Definition vreChatCommandsManager.h:51
~DtChatCommand()=default
Virtual destructor.
virtual bool execute(const std::string &parameters)=0
Executes the command with given parameters.
virtual bool showInHelp() const
Checks if this command should be visible in help listings.
Definition vreChatCommandsManager.h:68
virtual void removeCommand(std::string commandString)
Unregisters a chat command.
DtChatCommandsManager & operator=(const DtChatCommandsManager &orig)=delete
Deleted copy constructor and assignment operatorThese operations are not supported for the command ma...
DtPlayerStationApp & myApp
Reference to the player station application.
Definition vreChatCommandsManager.h:168
DtChatCommandsManager(DtPlayerStationApp &app)
Constructor.
virtual bool runCommand(std::string consoleString)
Executes a command from a console input string.
std::unordered_map< std::string, std::shared_ptr< DtChatCommand > > myOwnedCommandMap
Map of command strings to shared pointers to commands.
Definition vreChatCommandsManager.h:163
DtChatCommandsManager(const DtChatCommandsManager &orig)=delete
Deleted copy constructor and assignment operatorThese operations are not supported for the command ma...
std::unordered_map< std::string, std::weak_ptr< DtChatCommand > > myCommandMap
Map of command strings to weak pointers to commands.
Definition vreChatCommandsManager.h:156
virtual bool addCommand(std::string commandString, std::shared_ptr< DtChatCommand > command, bool takeOwnership=false)
Registers a new chat command.
virtual std::string helpText(const std::string &parameters) const
Generates help text for available commands.
Top-level class representing the VR-Engage application.
Definition playerStationApp.h:163
Defines export macros for the VR-Engage Player Station library.
#define PLAYERSTATION_DLL
Definition export.h:24
Include export definitions for this library.
Definition glsVreMessageUtil.h:49