MethodHolder.h #pragma once #include #include #include #include #include…
- User
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", std::bind(&InvokeFunction::EnterSVelPointEdit, &invokePtr)); 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)()
Severity Code Description Project File Line Suppression State Details Error C2664 'void MethodHolder::RegisterMethod(const std::string &,MethodHolder::VoidFunctionPtr)': cannot convert argument 2 from 'std::_Binder<std::_Unforced,void (__cdecl InvokeFunction::* )(void),InvokeFunction *>' to 'MethodHolder::VoidFunctionPtr' FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\FSMBase.cpp 74
- Assistant
The error you are encountering stems from the fact that you're trying to register a method from an instance of the
InvokeFunctionclass usingstd::bind, which produces a callable object that does not match the expected signature of the typeVoidFunctionPtr.Explanation:
Your
VoidFunctionPtrtype is defined as:typedef void(*VoidFunctionPtr)();This means that
VoidFunctionPtrexpects a pointer to a function that takes no parameters and returnsvoid. However, when you usestd::bindto bind a member function (which operates on an instance of an object), you do not get a pointer to a function that matches this signature. Instead,std::bindproduces an object of typestd::functionor some other callable type that doesn't convert toVoidFunctionPtr.Solution:
To resolve this, you can create a static member function that can act as a wrapper around your instance methods. Here's how you can achieve that:
- Change InvokeFunction to include static methods that can be called without an instance.
- Pass the pointer to the instance or use a different approach to register the methods to the
MethodHolder.
Here’s an example of how you can implement this using static functions:
Step 1: Modify the
InvokeFunctionclassAdd static methods and a way to maintain the instance pointer across calls:
class InvokeFunction { public: static void EnterSVelPointEdit(); static void EnterEVelPointEdit(); static void EncoderInc(); static void SetInstance(InvokeFunction* instance) { m_instance = instance; } private: static InvokeFunction* m_instance; }; // Define the static member InvokeFunction* InvokeFunction::m_instance = nullptr; // The static methods void InvokeFunction::EnterSVelPointEdit() { if (m_instance != nullptr) { std::cout << "EnterSVelPointEdit method invoked" << std::endl; } } void InvokeFunction::EnterEVelPointEdit() { if (m_instance != nullptr) { std::cout << "EnterEVelPointEdit method invoked" << std::endl; } } void InvokeFunction::EncoderInc() { if (m_instance != nullptr) { std::cout << "EncoderInc method called" << std::endl; } }Step 2: Update the
mainfunctionNow in your
mainfunction, set the instance before registering the methods:int main() { // ... InvokeFunction invokeInstance; InvokeFunction::SetInstance(&invokeInstance); // Set the instance MethodHolder* holder = MethodHolder::GetInstance(); holder->RegisterMethod("EnterSVelPointEdit", InvokeFunction::EnterSVelPointEdit); // Trigger your events as before Event* event3 = new Event("ClickStartPoint", "InvokeMethod", "EnterSVelPointEdit"); prTool1->InEvent(event3); // ... }Important Note:
If you're using more complex methods or need to pass
thisas part of the call when working with instanced methods, you may need to rethink how you're invoking methods altogether; consider usingstd::functionfor yourVoidFunctionPtrtype instead:#include <functional> // Change typedef to typedef std::function<void()> VoidFunctionPtr;This allows you to store and call bound member functions without issues related to the function pointer types. This switch also accommodates any callable objects, making your implementation flexible.
Following these directions should resolve your compilation issues and allow you to register and invoke methods successfully.