5

I need to write a wrapper class in objective c for C++ class.

I have referred the following Can't find standard C++ includes when using C++ class in Cocoa project and was able to get rid of the lexical or Preprocessor issue: 'vector' file not found issue.

However, I do not understand coverting a C++ methods which accept several parameters to objective c method.

Can someone please help me to do so ? What i want to do is to write a wrapper class for this http://breakfastquay.com/rubberband/code-doc/classRubberBand_1_1RubberBandStretcher.html#a0af91755d71eecfce5781f2cd759db85

I have tried to do so and followings are the method I am stuck with ...

//  Wrapper.h

#import <Foundation/Foundation.h>

@interface Wrapper : NSObject {
    void *myRubberBandStretcher;
}

#pragma mark - Member Functions
-(void)process:(const float)input samples:(size_t)samples final:(bool)final;
@end

/////////////////////////////////////////////////////////////////////////////////

//Wrapper.mm

#import "Wrapper.h"
#import "RubberBandStretcher.h"

@implementation Wrapper


-(id)init {
self = [super init];
if (self) {
   myRubberBandStretcher = new RubberBand::RubberBandStretcher::RubberBandStretcher(44100, 2, 0, 1.0, 1.0);
}
return self;
}

-(void)process:(const float)input samples:(size_t)samples final:(bool)final {
static_cast<RubberBand::RubberBandStretcher *>(myRubberBandStretcher)->process(<#const float *const *input#>, <#size_t samples#>, <#bool final#>)
}
Community
  • 1
  • 1
Ankahathara
  • 2,866
  • 2
  • 20
  • 26
  • 1
    I guess you won't need to create special wrapper class for C++ in Objective-C. Just change implementation file extension from .m to .mm your C++ code will start compile and running. – Tirth Feb 12 '15 at 06:38
  • I've created the static library for rubber band library and got only the RubberBandStretcher.h file. The header file is written in C++. When I import the header file and it goes to the header file and gives me the "lexical or Preprocessor issue: 'vector' file not found issue" error. I've got rid of the error following this http://stackoverflow.com/questions/6083764/cant-find-standard-c-includes-when-using-c-class-in-cocoa-project So I am thinking I need to write a wrapper – Ankahathara Feb 12 '15 at 06:54
  • You can change the file extension to .mm in which you are using the c++ syntax right? Then there is no need to right the wrapper. – Arjuna Feb 12 '15 at 07:03
  • @Arjuna as I said above I've got only the header file and the static library. The header file extension is .h. There is no .m file I am writing the wrapper class for it and have .h and .mm file. I have include the .h file and the .mm file in the question above but confused about how to convert the c++ methods available in the http://breakfastquay.com/rubberband/code-doc/classRubberBand_1_1RubberBandStretcher.html#a0af91755d71eecfce5781f2cd759db85 file – Ankahathara Feb 12 '15 at 07:36
  • @Arjuna the method I am trying to convert to objective c is this method; void RubberBand::RubberBandStretcher::process ( const float *const * input, size_t samples, bool final ) I have no idea how to convert it to objective c since it asks several parameters for the method. If you need to see the full code I can upload the project for you. Thanks – Ankahathara Feb 12 '15 at 08:14
  • What happens if you give const float *const * as input type? – Arjuna Feb 12 '15 at 08:15
  • when I write it as you suggest it looks like this -(void)process:(const float *const *)input samples:(size_t)samples final:(bool)final { static_cast(myRubberBandStretcher) -> process(, , ); } and xcode prompt me an error saying "Expected expression" – Ankahathara Feb 12 '15 at 08:34
  • Please upload your project. – Arjuna Feb 12 '15 at 09:40

1 Answers1

5

You should read up on "compilation units". In short, there's no way to tell what language a header file is written in for the compiler. Therefore, it always uses the language of the source file that includes the header. So if you include a C++ header from a .m file, it will not work. For every ObjC file that uses a C++ header, you have to change the file name suffix of the source file that uses it from .m to .mm. You do not need the source files for your rubberband library, you simply need to make all the files that use it ObjC++ instead of just ObjC.

Of course, you may not want to change all your files to be ObjC++. Your code for the alternate approach of wrappering the C++ is pretty close. One thing I noticed is that you still have the <#foo#> placeholders in your code (Xcode may display them as blue rounded cartouches, which is probably why you haven't noticed them before). Replace those with the actual names of the parameter variables, just like you would with a regular C function call, and all should compile. I.e.:

-(void)process:(const float)input samples:(size_t)samples final:(bool)final {
    static_cast<RubberBand::RubberBandStretcher *>(myRubberBandStretcher)->process( input, samples, final );
}

Also note that you seem to have omitted the semicolon after the process call.

BTW, you can also get rid of having to use static_cast everywhere, see the tricks listed here: Can I separate C++ main function and classes from Objective-C and/or C routines at compile and link? and there might also be some more useful info here: How to call C++ method from Objective-C Cocoa Interface using Objective-C++

Community
  • 1
  • 1
uliwitness
  • 8,532
  • 36
  • 58