简体   繁体   中英

How to create a C++ wrapper class for the following kind of C program?

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);
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM