MethodHolder.h
#pragma once
#include
#include
#include
#include
#include "InvokeFunction.h"
#include "FixedPolygonTool.h"
class MethodHolder
{
public:
typedef void(*VoidFunctionPtr)();
static MethodHolder* GetInstance() {
if (!instance) {
instance = new MethodHolder();
}
return instance;
}
void InvokeMethod(const std::string& methodName);
void RegisterMethod(const std::string& methodName, VoidFunctionPtr methodPtr);
private:
MethodHolder() {}
static MethodHolder* instance;
MethodHolder(const MethodHolder&) = delete;
MethodHolder& operator=(const MethodHolder&) = delete;
std::map<std::string, VoidFunctionPtr>VoidMethodMap;
};
MethodHolder.cpp
#include "MethodHolder.h"
MethodHolder* MethodHolder::instance = nullptr;
void MethodHolder::InvokeMethod(const std::string& methodName)
{
auto it = VoidMethodMap.find(methodName);
if (it != VoidMethodMap.end()) {
it->second();
}
else {
std::cout << "Method not found: " << methodName << std::endl;
}
}
void MethodHolder::RegisterMethod(const std::string& methodName, VoidFunctionPtr methodPtr)
{
VoidMethodMap[methodName] = methodPtr;
}
InvokrFunction.h
#pragma once
#include "Event.h"
#include "MethodHolder.h"
#include "FSMBase.h"
class InvokeFunction
{
public:
void EnterSVelPointEdit();
void EnterEVelPointEdit();
void EncoderInc();
};
InvokeFunction.cpp
#include
#include "InvokeFunction.h"
void InvokeFunction::EnterSVelPointEdit()
{
std::cout << "EnterSVelPointEdit method invoked" << std::endl;
}
void InvokeFunction::EnterEVelPointEdit()
{
std::cout << "EnterEVelPointEdit method invoked" << std::endl;
}
void InvokeFunction::EncoderInc()
{
std::cout << "EncoderInc method called" << std::endl;
}
FSMBase.cpp
#include
#include
#include
#include<rapidjson/document.h>
#include<rapidjson/filereadstream.h>
#include
#include "FSMBase.h"
#include "FSMBuilder.h"
#include "MethodHolder.h"
#include "InvokeFunction.h"
#include "FixedPolygonTool.h"
using namespace std;
using namespace rapidjson;
FSMBase::FSMBase()
{
std::cout << "default constructor called" << std::endl;
}
void FSMBase::Add(FSMBase* b1)
{
subitem.push_back(b1);
//b1->getdata();
}
FSMBase::~FSMBase()
{
std::cout << " destructor called" << std::endl;
}
int main()
{
Document doc;
ifstream file("FSMImplementationJSON.json");
if (!file.is_open()) {
cerr << "Could not open file: FSMImplementationJSON.json" << endl;
return 1;
}
string json((istreambuf_iterator<char>(file)),
istreambuf_iterator<char>());
doc.Parse(json.c_str());
if (doc.HasParseError()) {
cerr << "Error parsing JSON: "
<< doc.GetParseError() << endl;
return 1;
}
PrimitiveTool* prTool1 = new PrimitiveTool();
FSMBuilder::createStatesFromJson(prTool1, doc);
std::cout << " " << std::endl;
prTool1->SetInitialState("StartToolState");
Event* event1 = new Event("CompleteEvent", "ChangeState", "WaitForAccept");
prTool1->InEvent(event1);
Event* event2 = new Event("CompleteEvent", "ChangeState", "AcceptedState");
prTool1->InEvent(event2);
MethodHolder* holder = MethodHolder::GetInstance();
InvokeFunction invokePtr;
holder->RegisterMethod("EnterSVelPointEdit", invokePtr->EnterSVelPointEdit());
Event* event3 = new Event("ClickStartPoint", "InvokeMethod", "EnterSVelPointEdit");
prTool1->InEvent(event3);
In am getting erro r in the RegisterMethod area in the FSM intMain() before triggerring Event3 we need to register method in VoidMethodMap .Provide the corrections based on using typedef void(*VoidFunctionPtr)();