TuttleOFX  1
TuttleOFX/libraries/tuttle/src/tuttle/common/system/macos.cpp
Go to the documentation of this file.
00001 #include "macos.hpp"
00002 
00003 #ifdef __MACOS__
00004 
00005 #include <string>
00006 
00007 
00008 namespace tuttle {
00009 namespace common {
00010 
00011 std::string CFStringContainer::toString( CFStringRef cfString )
00012 {
00013         CFIndex length = CFStringGetLength( cfString );
00014         CFIndex size = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
00015         char *buffer = new char[size];
00016         Boolean worked = CFStringGetCString( cfString, buffer, size, kCFStringEncodingUTF8 );
00017         if( worked ) {
00018                 std::string result( buffer );
00019                 delete [] buffer;
00020                 return result;
00021         }
00022         delete [] buffer;
00023         return std::string();
00024 }
00025 
00026 CFStringContainer::operator std::string() const
00027 {
00028         return str();
00029 }
00030 
00031 const std::string& CFStringContainer::str() const
00032 {
00033         if( _string.empty() && _type )
00034                 const_cast<CFStringContainer*>(this)->_string = toString(_type);
00035         return _string;
00036 }
00037 
00038 CFStringRef CFStringContainer::toCFStringRef( const std::string& s )
00039 {
00040         return CFStringCreateWithCString(kCFAllocatorDefault, s.c_str(), kCFStringEncodingUTF8); 
00041 }
00042 
00043 CFStringContainer::operator CFStringRef() const
00044 {
00045         if( !_type )
00046         {
00047                 const_cast<CFStringContainer*> ( this )->_type =
00048                         CFStringCreateWithCharactersNoCopy( 0,
00049                                 reinterpret_cast<const UniChar *> ( _string.c_str() ),
00050                                 _string.size(),
00051                                 kCFAllocatorNull );
00052         }
00053         return _type;
00054 }
00055 
00056 
00057 }
00058 }
00059 
00060 #endif