C++ Callbacks with objective-C, an application to RtMidi

I’ve read several of the previous discussion about the subject but since I’m relatively new to Objective-C, I don’t really understand them. (Blocks, selectors, and delegate are very new to me).

Here is the situation:
I’m using those abstract class RtMidi that are written in c++

  • Convert first number in an NSString into an Integer?
  • How to add UISegmentControl in UINavigationItem ..?
  • Change the navigation bar's font
  • How to remember rows selected and maintain them after reload by default in UITableView?
  • SceneKit - Lighting and casting shadows
  • Convert MKAnnotation Coordinates to View Coordinates
  • void MidiInApi :: setCallback( RtMidiIn::RtMidiCallback callback, void *userData )
    {
      if ( inputData_.usingCallback ) {
        errorString_ = "MidiInApi::setCallback: a callback function is already set!";
        RtMidi::error( RtError::WARNING, errorString_ );
        return;
      }
    
      if ( !callback ) {
        errorString_ = "RtMidiIn::setCallback: callback function value is invalid!";
        RtMidi::error( RtError::WARNING, errorString_ );
        return;
    }
    
    inputData_.userCallback = (void *) callback;
    inputData_.userData = userData;
    inputData_.usingCallback = true;
    }
    

    I also have a class that Reciever that implements that callback (rtMidiCallback).
    And at some point in my main file I need to write something like this

            midiIn->setCallback(rtMidiCallback);
    

    This Would be in a c++ context but in Objective-C I have no idea how to do it.

    If you have any ideas, pointers I would be glad to discuss about it with you. Thank you very much

    Best,

    greg.

    Solutions Collect From Internet About “C++ Callbacks with objective-C, an application to RtMidi”

    It’s possible to have C++ and Objective-C within the same project and even within the same file (Objective-C++ with file extension .mm). If the code you’re using is designed for a C++ callback, it may be easier to provide that and communicate between different object types outside of that mechanism.


    //  CPPClass.h
    
    #ifndef CPPClass_h
    #define CPPClass_h
    
    class CPPClass {
    private:
        id m_ocObject;
    
    public:
        CPPClass(id ocObject);
        virtual ~CPPClass();
    
        void verifyCPP();
    
    };
    
    #endif
    

    //  OCClass.h
    
    #import <Foundation/Foundation.h>
    
    class CPPClass;
    
    @interface OCClass : NSObject {
        CPPClass *cppObject;
    }
    
    - (void)ocCallback;
    - (void)verifyOC;
    
    @end
    

    //  OCClass.mm
    
    #import "OCClass.h"
    #import "CPPClass.h"
    
    #include <iostream>
    
    @implementation OCClass
    
    - (id)init {
        self = [super init];
        if (self) {
            cppObject = new CPPClass(self);
        }
        return self;
    }
    
    - (void)dealloc {
        delete cppObject;
    }
    
    - (void)ocCallback {
        NSLog(@"Objective-C called from C++");
    }
    
    - (void)verifyOC {
        NSLog(@"Objective-C called from Objective-C");
        cppObject->verifyCPP();
    }
    
    @end
    
    CPPClass::CPPClass(id ocObject) : m_ocObject(ocObject)
    {}
    
    CPPClass::~CPPClass() {}
    
    void CPPClass::verifyCPP() {
        std::cout << "C++ called from Objective-C" << std::endl;
        [m_ocObject ocCallback];
    }
    

    To activate this, some other .mm file would have:

    #import "OCClass.h"
    ...
    OCClass *test = [[OCClass alloc] init];
    [test verifyOC];