문제

Hello I know that this problem was discussed several times, but I have feeling that no other explenation works for me. Maybe it isn't possible but... at the moment I have mm file it looks like (init methods/ header file) obj-c except few C++ methods (library live555 which I use is written in C++), and I can call C++ methods form obj-c just fine. But when I want call obj-c in c++... then I got error. I know self isn't know. But how I can move aroud it? I try with 2-3 tuts but all of them assume that obj-c method isn't called inside that obj-c++ class.

My Obj-C++ class (.mm file)

@interface testSender : NSObject{
    @private
    NSMutableArray *_buffors;
}

+(id)voiceSender;
//Function to invoke
-(bool)continueSendBuffer;

@end

@implementation testSender
@synthesize address=_address,port=_port;

UsageEnvironment* env;

+(id)voiceSender{
    return [[self alloc]init];
}

- (id)init
{
    self = [super init];
    if (self) {
        [self initRTPProt];
        _buffors = [NSMutableArray array];

    }
    return self;
}

-(void)initRTPProt{
  //init protocol
}

void afterPlaying(void* clientData); // forward


void afterPlaying(void* /*clientData*/) {
    // We and stream and now I want check if it's some other streams to send so normaly I should call
    [self continueSendBuffer];

}


-(bool)continueSendBuffer{
    if ([_buffors count] == 0) return false;
    NSData *nextBuffer = [_buffors objectAtIndex:0];
    [_buffors removeObjectAtIndex:0];

    [self sendNextBuffer:nextBuffer];

    return true;
}

-(void)sendNextBuffer:(NSData*)buffer{
    // Send next buffer
    /setting sessionState..



    // Start the streaming:
    *env << "Beginning streaming...\n";

    // Method afterPlaying will be called after ther will be nothing to send
    sessionState.sink->startPlaying(*sessionState.source, afterPlaying, NULL);
     env->taskScheduler().doEventLoop();
}

@end

At c++ method void afterPlaying(void*) when I want use [self continueSendBuffer] then I got error Use of undeclared identifier 'self'.


Solved

Chuck did great job and explain me how Objective-C++ works. Read it! Basically you just can't call obj-c method in "c++ method" (I know there are free floating methods) with out passing to them self pointer.

I have to modify a bit startPlaying call and now in third argument I pass self pointer Also method afterPlaying had to been change so now she can use clientData pointer. Throught this I can call continueSendBuffer.

startPlaying

sessionState.sink->startPlaying(*sessionState.source, afterPlaying, (__bridge void*)self);

afterPlaying

void afterPlaying(void* clientData) {
    [(__bridge NXVoiceSender*)clientData continueSendBuffer];
}
도움이 되었습니까?

해결책

Objective-C++ does not unify C++ classes and Objective-C classes. It allows you to mix Objective-C code and C++ code, but the two object models are still completely separate — it basically just uses C++ where C would be used in normal Objective-C. What you're calling "C++ methods" there are actually just free-floating functions, because an Objective-C class definition isn't a special context in C++. They are not methods of the class.

You'll somehow need to keep a pointer to the object in question and get that into the afterPlaying() function (and also keep the object alive long enough to be referenced when afterPlaying() is called). I'm not familiar with the library, but that clientData parameter looks promising.

다른 팁

The type of the second argument of startPlaying is std::function? If so, you can pass a Block instead of C++11 lambda for that as the following with "C++ Language Dialect" to "C++11".

__weak testSender *self_ = self;
sessionState.sink->startPlaying(*sessionState.source, ^(void *){
    // We and stream and now I want check if it's some other streams to send so normaly I should call
    [self_ continueSendBuffer];
}, NULL);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top