Skip to content

Instantly share code, notes, and snippets.

@sansumbrella
Created May 17, 2013 22:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sansumbrella/5602290 to your computer and use it in GitHub Desktop.
Save sansumbrella/5602290 to your computer and use it in GitHub Desktop.
C++ facade for Objective-C class
// In your .h file:
#ifdef __OBJC__
@class ImplementationClass;
#else
class ImplementationClass;
#endif
class Facade
{
public:
Facade();
~Facade();
void doSomething();
private:
//! Pointer to implementation
ImplementationClass *mImpl;
};
// Optionally, define your custom ImplementationClass here
// Note that this is only necessary if you aren't directly wrapping an existing type
@interface ImplementationClass
-(void) doSomething;
@end
@implementation ImplementationClass
-(void) doSomething
{
NSLog(@"Doing something in Obj-C");
}
@end
Facade::Facade()
{ // initialize implementation however you are meant to
mImpl = [[ImplementationClass alloc] init];
[mImpl retain];
}
Facade::~Facade()
{ // don't forget to release retained objects
[mImpl release];
}
// Call the Objective-C method when your facade's method is called
void Facade::doSomething()
{
// either
mImpl->doSomething();
// or
[mImpl doSomething];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment