TuttleOFX  1
TuttleOFX/libraries/tuttle/src/tuttle/common/system/macos.hpp
Go to the documentation of this file.
00001 #ifndef _TUTTLE_COMMON_SYSTEM_MACOS_HPP_
00002 #define _TUTTLE_COMMON_SYSTEM_MACOS_HPP_
00003 
00004 #include "system.hpp"
00005 
00006 #ifdef __MACOS__
00007 
00008 //#include <ApplicationServices/ApplicationServices.h>
00009 #include <CoreFoundation/CoreFoundation.h>
00010 #undef nil // /usr/include/MacTypes.h defines nil as NULL. This borks boost.
00011 
00012 #include <string>
00013 
00014 namespace tuttle {
00015 namespace common {
00016 
00017 /*
00018         Helper class that automates refernce counting for CFtypes.
00019         After constructing the CFTypeContainer object, it can be copied like a
00020         value-based type.
00021 
00022         Note that you must own the object you are wrapping.
00023         This is typically the case if you get the object from a Core
00024         Foundation function with the word "Create" or "Copy" in it. If
00025         you got the object from a "Get" function, either retain it or use
00026         constructFromGet(). One exception to this rule is the
00027         HIThemeGet*Shape functions, which in reality are "Copy" functions.
00028  */
00029 template <typename T>
00030 class CFTypeContainer
00031 {
00032 public:
00033         inline CFTypeContainer( const T &t = 0 )
00034                 : _type( t ) { }
00035 
00036         inline CFTypeContainer( const CFTypeContainer& helper )
00037                 : _type( helper._type )
00038         {
00039                 if( _type )
00040                         CFRetain( _type );
00041         }
00042 
00043         inline ~CFTypeContainer()
00044         {
00045                 if( _type )
00046                         CFRelease( _type );
00047         }
00048 
00049         inline operator T()
00050         {
00051                 return _type;
00052         }
00053 
00054         inline CFTypeContainer operator =(const CFTypeContainer &helper )
00055         {
00056                 if( helper._type )
00057                         CFRetain( helper._type );
00058                 CFTypeRef type2 = _type;
00059                 _type = helper._type;
00060                 if( type2 )
00061                         CFRelease( type2 );
00062                 return *this;
00063         }
00064 
00065         inline T *operator&()
00066         {
00067                 return &_type;
00068         }
00069 
00070         static CFTypeContainer constructFromGet( const T &t )
00071         {
00072                 CFRetain( t );
00073                 return CFTypeContainer<T > ( t );
00074         }
00075 protected:
00076         T _type;
00077 };
00078 
00079 class CFStringContainer : public CFTypeContainer<CFStringRef>
00080 {
00081 public:
00082         inline CFStringContainer( const std::string& str ) : CFTypeContainer<CFStringRef>( 0 ), _string( str ) { }
00083 
00084         inline CFStringContainer( const CFStringRef cfstr = NULL ) : CFTypeContainer<CFStringRef>( cfstr ) { }
00085 
00086         inline CFStringContainer( const CFTypeContainer<CFStringRef> &other ) : CFTypeContainer<CFStringRef>( other ) { }
00087         operator std::string() const;
00088         operator CFStringRef() const;
00089         
00090         const std::string& str() const;
00091         
00092         static std::string toString( CFStringRef cfstr );
00093         static CFStringRef toCFStringRef( const std::string& str );
00094         
00095 private:
00096         std::string _string;
00097 };
00098 
00099 }
00100 }
00101 
00102 #endif
00103 
00104 #endif