简体   繁体   中英

How to convert this C++ code to Objective C

What would be equivalent Objective C for this:

template<class T>
class Singleton 
{ 
public: 
    static T * instance() 
    { 
        static T t; 
        return &t; 
    }
private: 
    Singleton(); 
    ~Singleton(); 
};

While calling:

friend class Singletone<MyManagerClass>;

Objective-C does not provide templates and templates do not work with Objective-C classes when compiled as Objective-C++. It is not always possible to have a 1-to-1 conversion between two programming languages. For example, Objective-C does not provide compile-time enforced private methods at all, so there is no way to exactly implement what you want.

The same goes the other way, Objective-C has features that are not available in C++, such as the @encode() directive.

Perhaps related to this topic: it is important to understand that design patterns that work well with C++ do not necessarily work well—or at all—with Objective-C.

What is the goal of your design of being able to singletonise any class? Perhaps there is a more established Objective-C way to acheive what you want. There may also be a way to implement something similar using preprocessor macros, but you don't want to go down that path.

Oh thats Pretty easy!

@interface YourClass : NSObject {}
+ (YourClass *)singleton;
@end

static YourClass *singletonVar = nil;
@implementation YourClass
+ (YourClass *)singleton
{
  if( ! singletonVar ) {
    singletonVar = [[self alloc] init];
  }
  return singletonVar;
}
@end

Now you can call [[YourClass singleton] doSomething] anywhere! :D Just like you do [[NSUserDefaults standUserDefaults] valueForKey:@"Burp"] and such, do note that this ads overhead to your app, so when you use the singleton more than once in a scope consider storing it in a pointer for performance.

#define SINGLETON_FOR_CLASS(classname)                          \
                                                                \
static classname *shared##classname = nil;                      \
                                                                \
+ (classname *)instance {                                       \
    @synchronized(self) {                                       \
        if (shared##classname == nil) {                         \
            [[self alloc] init];                                \
        }                                                       \
    }                                                           \
                                                                \
    return shared##classname;                                   \
}                                                               \
                                                                \
+ (id)allocWithZone:(NSZone *)zone {                            \
                                                                \
    @synchronized(self) {                                       \
        if (shared##classname == nil) {                         \
            shared##classname = [super allocWithZone:zone];     \
            return shared##classname;                           \
        }                                                       \
    }                                                           \
                                                                \
    return nil;                                                 \
}                                                               \
                                                                \
- (id)copyWithZone:(NSZone *)zone {                             \
                                                                \
    return self;                                                \
}                                                               \
                                                                \
- (id)retain {                                                  \
                                                                \
    return self;                                                \
}                                                               \
                                                                \
- (NSUInteger)retainCount {                                     \
                                                                \
    return UINT_MAX;                                            \
}                                                               \
                                                                \
- (void)release {                                               \
                                                                \
}                                                               \
                                                                \
- (id)autorelease {                                             \
                                                                \
    return self;                                                \
}                                                               \

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM