Skip to content

Instantly share code, notes, and snippets.

Created April 15, 2014 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/10719425 to your computer and use it in GitHub Desktop.
Save anonymous/10719425 to your computer and use it in GitHub Desktop.
intrusive_ptr.h
#pragma once
#import "intrusive_ptr.hpp"
inline void intrusive_ptr_add_ref(NSObject * p)
{
if (p) [p retain];
}
inline void intrusive_ptr_release(NSObject * p)
{
if (p) [p release];
}
#define CG_INTRUSIVE_PTR_DEFINE(Object) \
inline void intrusive_ptr_add_ref(CG##ObjectRef p) { CG##ObjectRetain(p); } \
inline void intrusive_ptr_release(CG##ObjectRef p) { CG##ObjectRelease(p); }
//CG_INTRUSIVE_PTR_DEFINE(Color)
typedef boost::intrusive_ptr<CGColor> CGColorPtr;
inline void intrusive_ptr_add_ref(CGColorRef p) { CGColorRetain(p); }
inline void intrusive_ptr_release(CGColorRef p) { CGColorRelease(p); }
typedef boost::intrusive_ptr<CGColorSpace> CGColorSpacePtr;
inline void intrusive_ptr_add_ref(CGColorSpaceRef p) { CGColorSpaceRetain(p); }
inline void intrusive_ptr_release(CGColorSpaceRef p) { CGColorSpaceRelease(p); }
typedef boost::intrusive_ptr<CGContext> CGContextPtr;
inline void intrusive_ptr_add_ref(CGContextRef p) { CGContextRetain(p); }
inline void intrusive_ptr_release(CGContextRef p) { CGContextRelease(p); }
typedef boost::intrusive_ptr<CGDataConsumer> CGDataConsumerPtr;
inline void intrusive_ptr_add_ref(CGDataConsumerRef p) { CGDataConsumerRetain(p); }
inline void intrusive_ptr_release(CGDataConsumerRef p) { CGDataConsumerRelease(p); }
typedef boost::intrusive_ptr<CGDataProvider> CGDataProviderPtr;
inline void intrusive_ptr_add_ref(CGDataProviderRef p) { CGDataProviderRetain(p); }
inline void intrusive_ptr_release(CGDataProviderRef p) { CGDataProviderRelease(p); }
typedef boost::intrusive_ptr<CGFont> CGFontPtr;
inline void intrusive_ptr_add_ref(CGFontRef p) { CGFontRetain(p); }
inline void intrusive_ptr_release(CGFontRef p) { CGFontRelease(p); }
typedef boost::intrusive_ptr<CGFunction> CGFunctionPtr;
inline void intrusive_ptr_add_ref(CGFunctionRef p) { CGFunctionRetain(p); }
inline void intrusive_ptr_release(CGFunctionRef p) { CGFunctionRelease(p); }
typedef boost::intrusive_ptr<CGGradient> CGGradientPtr;
inline void intrusive_ptr_add_ref(CGGradientRef p) { CGGradientRetain(p); }
inline void intrusive_ptr_release(CGGradientRef p) { CGGradientRelease(p); }
class CGImagePtr : public boost::intrusive_ptr<CGImage>
{
float _scale;
public:
CGImagePtr(float scale=1) : boost::intrusive_ptr<CGImage>(), _scale(scale) {}
CGImagePtr(CGImage *p, bool add_ref, float scale=1) : boost::intrusive_ptr<CGImage>(p, add_ref), _scale(scale) {};
CGImagePtr(const CGImagePtr &r) : boost::intrusive_ptr<CGImage>(r), _scale(r._scale) {}
CGImagePtr& operator= (const CGImagePtr &r) { boost::intrusive_ptr<CGImage>::operator=(r); _scale=r._scale; return *this; }
public:
inline float width() const { return CGImageGetWidth(get()) / _scale; }
inline float height() const { return CGImageGetHeight(get()) / _scale; }
inline CGSize size() const { return CGSizeMake(width(), height()); }
inline float scale() const { return _scale; }
operator CGImageRef() const { return get(); }
};
inline void intrusive_ptr_add_ref(CGImageRef p) { CGImageRetain(p); }
inline void intrusive_ptr_release(CGImageRef p) { CGImageRelease(p); }
typedef boost::intrusive_ptr<CGLayer> CGLayerPtr;
inline void intrusive_ptr_add_ref(CGLayerRef p) { CGLayerRetain(p); }
inline void intrusive_ptr_release(CGLayerRef p) { CGLayerRelease(p); }
typedef boost::intrusive_ptr<CGPath> CGPathPtr;
inline void intrusive_ptr_add_ref(CGPathRef p) { CGPathRetain(p); }
inline void intrusive_ptr_release(CGPathRef p) { CGPathRelease(p); }
typedef boost::intrusive_ptr<CGPattern> CGPatternPtr;
inline void intrusive_ptr_add_ref(CGPatternRef p) { CGPatternRetain(p); }
inline void intrusive_ptr_release(CGPatternRef p) { CGPatternRelease(p); }
typedef boost::intrusive_ptr<CGShading> CGShadingPtr;
inline void intrusive_ptr_add_ref(CGShadingRef p) { CGShadingRetain(p); }
inline void intrusive_ptr_release(CGShadingRef p) { CGShadingRelease(p); }
class ref_counted
{
size_t _ref_count;
public:
ref_counted() : _ref_count(0) {}
ref_counted(const ref_counted &r) { *this = r; }
virtual ~ref_counted () {}
ref_counted & operator= (const ref_counted &r)
{
_ref_count = r._ref_count;
return *this;
}
protected:
friend void intrusive_ptr_add_ref(ref_counted *p);
friend void intrusive_ptr_release(ref_counted *p);
void add_ref() { _ref_count++; }
bool release()
{
if (!this || !_ref_count) return false;
if (!--_ref_count) return true;
return false;
}
};
inline void intrusive_ptr_add_ref(ref_counted *p) { p->add_ref(); }
inline void intrusive_ptr_release(ref_counted *p) { if (p->release()) delete p; }
typedef boost::intrusive_ptr<NSArray> NSArrayPtr;
typedef boost::intrusive_ptr<NSCalendar> NSCalendarPtr;
typedef boost::intrusive_ptr<NSCharacterSet> NSCharacterSetPtr;
typedef boost::intrusive_ptr<NSData> NSDataPtr;
typedef boost::intrusive_ptr<NSDate> NSDatePtr;
typedef boost::intrusive_ptr<NSDateComponents> NSDateComponentsPtr;
typedef boost::intrusive_ptr<NSDateFormatter> NSDateFormatterPtr;
typedef boost::intrusive_ptr<NSDecimal> NSDecimalPtr;
typedef boost::intrusive_ptr<NSDictionary> NSDictionaryPtr;
typedef boost::intrusive_ptr<NSError> NSErrorPtr;
typedef boost::intrusive_ptr<NSLocale> NSLocalePtr;
typedef boost::intrusive_ptr<NSMutableArray> NSMutableArrayPtr;
typedef boost::intrusive_ptr<NSMutableDictionary> NSMutableDictionaryPtr;
typedef boost::intrusive_ptr<NSMutableSet> NSMutableSetPtr;
typedef boost::intrusive_ptr<NSMutableString> NSMutableStringPtr;
typedef boost::intrusive_ptr<NSNumber> NSNumberPtr;
typedef boost::intrusive_ptr<NSObject> NSObjectPtr;
typedef boost::intrusive_ptr<NSSet> NSSetPtr;
typedef boost::intrusive_ptr<NSString> NSStringPtr;
typedef boost::intrusive_ptr<NSThread> NSThreadPtr;
typedef boost::intrusive_ptr<NSTimeZone> NSTimeZonePtr;
typedef boost::intrusive_ptr<NSURL> NSURLPtr;
typedef boost::intrusive_ptr<UIActionSheet> UIActionSheetPtr;
typedef boost::intrusive_ptr<UIAlertView> UIAlertViewPtr;
typedef boost::intrusive_ptr<UIColor> UIColorPtr;
typedef boost::intrusive_ptr<UIImage> UIImagePtr;
typedef boost::intrusive_ptr<UIImageView> UIImageViewPtr;
typedef boost::intrusive_ptr<UINavigationController> UINavigationControllerPtr;
typedef boost::intrusive_ptr<UITableView> UITableViewPtr;
typedef boost::intrusive_ptr<UITextField> UITextFieldPtr;
typedef boost::intrusive_ptr<UITextView> UITextViewPtr;
typedef boost::intrusive_ptr<UIView> UIViewPtr;
typedef boost::intrusive_ptr<UIButton> UIButtonPtr;
typedef boost::intrusive_ptr<UILabel> UILabelPtr;
typedef boost::intrusive_ptr<UIViewController> UIViewControllerPtr;
typedef boost::intrusive_ptr<UIWindow> UIWindowPtr;
typedef boost::intrusive_ptr<UIImagePickerController> UIImagePickerControllerPtr;
template <typename T>
inline boost::intrusive_ptr<T> intrusive_ptr(T* p, bool add_ref)
{
return boost::intrusive_ptr<T>(p, add_ref);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment