this is kind of a beginners question so please bear with me.
I’ve got a class that makes use of a third-party library (oniguruma, if that matters). I want library methods to be completely decorated by my own, so that I can switch the underlying implementation of my class anytime. Something like:
// MyClass.h
@interface MyClass : NSObject ...
- (int) doSomething;
// MyClass.m
#import "some_library.h"
@implementation MyClass
- (int) doSomething
{
//call library's specific stuff
}
So far, so good, but now I’m needing to use an instance variable in MyClass that has some library-defined type (a structure declared in “some_library.h”). Of course I can import the library right in the interface section:
//MyClass.h
#import "some_library.h"
@interface MyClass : NSObject {
some_library_t blah;
}
- (int) doSomething;
@end
but this is exactly what i’m trying to avoid – make users of MyClass aware of its implementation details.
Can I somehow “hide” library-specific types from my class’ interface? What is the standard practice?
The standard practice is to use opaque pointers to either the library types or a custom implementation structure (thus its also called Pimpl – pointer to implementation).
To do that you have to know that you can define pointers to incomplete types, i.e. types that you only declare to exist. E.g.:
You can then define the type in the implementation file:
and allocate/initialize it e.g. in your
-(id)initmethod.FooImplcould also beSomeLibraryTypeif the library type was a struct – you’d then forward declare it in the same way and include the library header in the source file, which gives you the structs definition.