Reputation: 143
I have created simple C++ wrapper classes before but my current problem is a bit confusing. I have a custom piece of hardware that can connected to a USB port and it gives out certain information depending on the configuration of the hardware based on which different event handlers are executed. It uses Ethernet-over-USB protocol. The C code on the PC side looks like this:
// Relevant headers
int Event1handler(){
// Code to process event 1
}
void Event2handler(){
// Code to process event 2
}
int main(void){
// Code to setup calls to Event1handler() and Event2handler() using Open Sound Control methods
}
Now I am confused how to wrap a C++ class around the above code. In the C program, the event handlers are called automatically depending on what information is coming from the USB. How would I implement the event handlers as methods of a class that are automatically called depending on the information the hardware is sending? Can I put the contents of the main() function into the constructor of the class?
Edit: I do not need separate methods for the class. I just need to wrap the C program into a class. Putting all the event handlers into a single method is also fine (if its possible at all). I just need the data that each event handler receives.
Edit2: Here is what the actual event handlers and calls look like using OpenSoundControl:
// Relevant headers
int Event1handler(const char *path, const char *types, lo_arg **argv, int argc, void *data, void user_data){
// Code to process event 1
}
void Event2handler(const char *path, const char *types, lo_arg **argv, int argc, void *data, void user_data){
// Code to process event 2
}
int main(void){
// Code to setup calls to Event1handler() and Event2handler() using Open Sound Control methods
lo_server_thread st;
// "if" means one integer and one float
lo_server_thread_add_method(st, "/folder/data1", "if", Event1handler, NULL);
lo_server_thread_add_method(st, "/folder/data2", "if", Event2handler, NULL);
}
Upvotes: 1
Views: 1813
Reputation: 1086
Just create a class and put the code inside, for example in your .h
file:
class EventWrapper
{
public:
EventWrapper();
static int Event1Handler();
static void Event2Handler();
}
Then in your .cpp
file:
#include "EventWrapper.h"
// Include relevant headers for your USB device
EventWrapper::EventWrapper()
{
// Code to setup calls to Event1handler() and Event2handler()
// using Open Sound Control methods.
}
int EventWrapper::Event1Handler()
{
// Code to process event 1
}
void EventWrapper::Event2Handler()
{
// Code to process event 2
}
Without knowing all of the details of your program you may have to play around with what is static and how you want to handle it.
Upvotes: 1