wiRedPanda
Logic Circuit Simulator
Loading...
Searching...
No Matches
BaseHandler Class Referenceabstract

Abstract base class for MCP command handlers. More...

#include <BaseHandler.h>

Inheritance diagram for BaseHandler:
[legend]

Public Member Functions

QString availableInputPorts (GraphicElement *element) const
QString availableOutputPorts (GraphicElement *element) const
 BaseHandler (MainWindow *mainWindow, const MCPValidator *validator)
QJsonObject createErrorResponse (const QString &error, const QJsonValue &requestId=QJsonValue(), int code=JsonRpcError::InternalError) const
QJsonObject createSuccessResponse (const QJsonObject &result={}, const QJsonValue &requestId=QJsonValue()) const
ScenecurrentScene ()
const ScenecurrentScene () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
virtual QJsonObject handleCommand (const QString &command, const QJsonObject &params, const QJsonValue &requestId)=0
bool inputPortByLabel (GraphicElement *element, const QString &label, int &portIndex, QString &errorMsg) const
bool outputPortByLabel (GraphicElement *element, const QString &label, int &portIndex, QString &errorMsg) const
template<typename Fn>
QJsonObject tryCommand (Fn &&fn, const QString &action, const QJsonValue &requestId=QJsonValue())
 Wraps fn in a try/catch, returning an error response on exception.
GraphicElementvalidatedElement (const QJsonObject &params, const QString &paramName, QString &errorMsg)
 Validates paramName in params, looks up the element, and returns it.
bool validateElementId (int elementId, const QString &paramName, QString &errorMsg) const
bool validateNonEmptyString (const QJsonValue &value, const QString &paramName, QString &errorMsg) const
bool validateNonNegativeInteger (const QJsonValue &value, const QString &paramName, QString &errorMsg) const
bool validateNumeric (const QJsonValue &value, const QString &paramName, QString &errorMsg) const
bool validateParameters (const QJsonObject &params, const QStringList &required) const
bool validatePortRange (GraphicElement *element, int portIndex, bool isOutput, const QString &paramName, QString &errorMsg) const
bool validatePositiveInteger (const QJsonValue &value, const QString &paramName, QString &errorMsg) const
virtual ~BaseHandler ()=default

Protected Member Functions

QString availablePorts (GraphicElement *element, bool isOutput) const

Protected Attributes

MainWindowm_mainWindow
const MCPValidatorm_validator

Detailed Description

Abstract base class for MCP command handlers.

Provides common functionality and interface for all MCP command handlers. Each handler is responsible for a specific domain of MCP commands.

Definition at line 23 of file BaseHandler.h.

Constructor & Destructor Documentation

◆ BaseHandler()

◆ ~BaseHandler()

virtual BaseHandler::~BaseHandler ( )
virtualdefault

Member Function Documentation

◆ availableInputPorts()

QString BaseHandler::availableInputPorts ( GraphicElement * element) const

Definition at line 289 of file BaseHandler.cpp.

References availablePorts().

Referenced by inputPortByLabel().

◆ availableOutputPorts()

QString BaseHandler::availableOutputPorts ( GraphicElement * element) const

Definition at line 294 of file BaseHandler.cpp.

References availablePorts().

Referenced by outputPortByLabel().

◆ availablePorts()

QString BaseHandler::availablePorts ( GraphicElement * element,
bool isOutput ) const
protected

◆ createErrorResponse()

QJsonObject BaseHandler::createErrorResponse ( const QString & error,
const QJsonValue & requestId = QJsonValue(),
int code = JsonRpcError::InternalError ) const

◆ createSuccessResponse()

QJsonObject BaseHandler::createSuccessResponse ( const QJsonObject & result = {},
const QJsonValue & requestId = QJsonValue() ) const

Definition at line 23 of file BaseHandler.cpp.

◆ currentScene() [1/2]

Scene * BaseHandler::currentScene ( )

Definition at line 60 of file BaseHandler.cpp.

References m_mainWindow, and WorkSpace::scene().

Referenced by validatedElement(), and validateElementId().

◆ currentScene() [2/2]

const Scene * BaseHandler::currentScene ( ) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 74 of file BaseHandler.cpp.

References m_mainWindow, and WorkSpace::scene().

◆ handleCommand()

virtual QJsonObject BaseHandler::handleCommand ( const QString & command,
const QJsonObject & params,
const QJsonValue & requestId )
pure virtual

◆ inputPortByLabel()

bool BaseHandler::inputPortByLabel ( GraphicElement * element,
const QString & label,
int & portIndex,
QString & errorMsg ) const

◆ outputPortByLabel()

bool BaseHandler::outputPortByLabel ( GraphicElement * element,
const QString & label,
int & portIndex,
QString & errorMsg ) const

◆ tryCommand()

template<typename Fn>
QJsonObject BaseHandler::tryCommand ( Fn && fn,
const QString & action,
const QJsonValue & requestId = QJsonValue() )
inline

Wraps fn in a try/catch, returning an error response on exception.

Calls fn() inside a try block. On std::exception or unknown exception, returns createErrorResponse("Failed to \<action\>: ..."). Use this to eliminate repetitive try/catch boilerplate in handler methods.

Definition at line 55 of file BaseHandler.h.

References createErrorResponse(), and JsonRpcError::OperationFailed.

◆ validatedElement()

GraphicElement * BaseHandler::validatedElement ( const QJsonObject & params,
const QString & paramName,
QString & errorMsg )

Validates paramName in params, looks up the element, and returns it.

Combines validatePositiveInteger() + validateElementId() + itemById() + dynamic_cast in one call. On failure sets errorMsg and returns nullptr. Typical use: auto *e = validatedElement(params, "element_id", errorMsg); if (!e) return createErrorResponse(errorMsg, requestId);

Definition at line 202 of file BaseHandler.cpp.

References currentScene(), Scene::itemById(), validateElementId(), and validatePositiveInteger().

◆ validateElementId()

bool BaseHandler::validateElementId ( int elementId,
const QString & paramName,
QString & errorMsg ) const

Definition at line 136 of file BaseHandler.cpp.

References currentScene(), and Scene::itemById().

Referenced by validatedElement().

◆ validateNonEmptyString()

bool BaseHandler::validateNonEmptyString ( const QJsonValue & value,
const QString & paramName,
QString & errorMsg ) const

Definition at line 120 of file BaseHandler.cpp.

◆ validateNonNegativeInteger()

bool BaseHandler::validateNonNegativeInteger ( const QJsonValue & value,
const QString & paramName,
QString & errorMsg ) const

Definition at line 104 of file BaseHandler.cpp.

◆ validateNumeric()

bool BaseHandler::validateNumeric ( const QJsonValue & value,
const QString & paramName,
QString & errorMsg ) const

Definition at line 158 of file BaseHandler.cpp.

◆ validateParameters()

bool BaseHandler::validateParameters ( const QJsonObject & params,
const QStringList & required ) const

Definition at line 50 of file BaseHandler.cpp.

◆ validatePortRange()

bool BaseHandler::validatePortRange ( GraphicElement * element,
int portIndex,
bool isOutput,
const QString & paramName,
QString & errorMsg ) const

Definition at line 174 of file BaseHandler.cpp.

References GraphicElement::inputs(), and GraphicElement::outputs().

◆ validatePositiveInteger()

bool BaseHandler::validatePositiveInteger ( const QJsonValue & value,
const QString & paramName,
QString & errorMsg ) const

Definition at line 88 of file BaseHandler.cpp.

Referenced by validatedElement().

Member Data Documentation

◆ m_mainWindow

MainWindow* BaseHandler::m_mainWindow
protected

Definition at line 85 of file BaseHandler.h.

Referenced by BaseHandler(), currentScene(), and currentScene().

◆ m_validator

const MCPValidator* BaseHandler::m_validator
protected

Definition at line 86 of file BaseHandler.h.

Referenced by BaseHandler().


The documentation for this class was generated from the following files: