Read the Horky classmate's "mixed use objective-c,c++ and objective-c++" blog, I also have some ideas to record, easy to learn in the future
1.objective-c/C + +/C depth blending is not a good choice, prone to error, and poorly maintained, and when using C + + classes in objective-c code, we use equivalent objective-c types and functions to encapsulate C + + code (WRAP).
Do not refer to C + + header files in the 2.objective-c header file because there is no class keyword in objective-c. The compiler will report Unknown type name ' class '; Did you mean ' class '? error.
Here's a column for easy comprehension.
PlayerWindowController.h objective-ct Header File
#import <Cocoa/Cocoa.h>
@interface Playerwindowcontroller:nswindowcontroller
{
@private
void *_player; Define a void *
}
-(BOOL) Playurl: (nsurl *) URL;
@end
PLAYERWINDOWCONTROLLER.MM implementation
#import "PlayerWindowController.h"
#include <libkern/OSAtomic.h>//c++ header file
#include <AudioPlayer.h>//c++ class header files
#include <AudioDecoder.h>//c++ class header files
#define PLAYER (Static_cast<audioplayer *> (_player))
static void decodingstarted (void *context, const audiodecoder *decoder)
{
[(Playerwindowcontroller *) context Decodingstarted:decoder];
}
static void renderingstarted (void *context, const audiodecoder *decoder)
{
[(Playerwindowcontroller *) context Renderingstarted:decoder];
}
static void renderingfinished (void *context, const audiodecoder *decoder)
{
[(Playerwindowcontroller *) context Renderingfinished:decoder];
}
@implementation Playerwindowcontroller
-(ID) init
{
if (nil = = (self = [Super initwithwindownibname:@ "Playerwindow"])) {
[self release];
return nil;
}
_player = new Audioplayer ();
return self;
}
-(BOOL) Playurl: (nsurl *) URL
{
Audiodecoder *decoder = Audiodecoder::createdecoderforurl (reinterpret_cast<cfurlref> (URL));
if (nullptr = = decoder)
return NO;
player->stop ();
Register player Start/end messages to facilitate UI update
Decoder->setdecodingstartedcallback (decodingstarted, self);
Decoder->setrenderingstartedcallback (renderingstarted, self);
Decoder->setrenderingfinishedcallback (renderingfinished, self);
return YES;
}
Objective-c using C + + classes