Created
May 17, 2013 22:03
-
-
Save sansumbrella/5602290 to your computer and use it in GitHub Desktop.
C++ facade for Objective-C class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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