Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r65876 - in sandbox/gil/boost/gil/extension/io2: . detail
From: dsaritz_at_[hidden]
Date: 2010-10-10 11:56:16


Author: psiha
Date: 2010-10-10 11:56:10 EDT (Sun, 10 Oct 2010)
New Revision: 65876
URL: http://svn.boost.org/trac/boost/changeset/65876

Log:
Removed dependency on ATL with a small custom COM smart pointer class.
Minor other refactoring.
Text files modified:
   sandbox/gil/boost/gil/extension/io2/detail/wic_extern_lib_guard.hpp | 100 ++++++++++++++++++++++++++++++++++-----
   sandbox/gil/boost/gil/extension/io2/wic_image.hpp | 42 ++++++++--------
   2 files changed, 108 insertions(+), 34 deletions(-)

Modified: sandbox/gil/boost/gil/extension/io2/detail/wic_extern_lib_guard.hpp
==============================================================================
--- sandbox/gil/boost/gil/extension/io2/detail/wic_extern_lib_guard.hpp (original)
+++ sandbox/gil/boost/gil/extension/io2/detail/wic_extern_lib_guard.hpp 2010-10-10 11:56:10 EDT (Sun, 10 Oct 2010)
@@ -21,8 +21,6 @@
 
 #include <boost/preprocessor/comparison/greater.hpp>
 
-#define ATLENSURE ATLVERIFY
-#include "atlcomcli.h"
 #if defined( _STDINT ) && !defined( _INTSAFE_H_INCLUDED_ )
     #define _INTSAFE_H_INCLUDED_
 #endif
@@ -38,7 +36,78 @@
 {
 //------------------------------------------------------------------------------
 
-template <int dummy = 0>
+////////////////////////////////////////////////////////////////////////////////
+//
+// com_scoped_ptr
+// --------------
+//
+////////////////////////////////////////////////////////////////////////////////
+
+template <class COMInterface>
+class com_scoped_ptr : noncopyable
+{
+private:
+ class __declspec( novtable ) block_manual_lifetime_management_proxy : public COMInterface
+ {
+ private:
+ ULONG STDMETHODCALLTYPE AddRef ();
+ ULONG STDMETHODCALLTYPE Release();
+ };
+
+public:
+ com_scoped_ptr() : baseCOMPtr_( NULL ) {}
+ com_scoped_ptr( COMInterface const * const baseCOMPtr ) : baseCOMPtr_( baseCOMPtr ) { if ( baseCOMPtr ) baseCOMPtr->AddRef(); }
+ com_scoped_ptr( com_scoped_ptr const & source ) : baseCOMPtr_( source.baseCOMPtr_ ) { if ( baseCOMPtr_ ) baseCOMPtr_->AddRef(); }
+ com_scoped_ptr( IUnknown & source )
+ {
+ BOOST_VERIFY
+ (
+ source.QueryInterface
+ (
+ __uuidof( COMInterface ),
+ reinterpret_cast<void * *>( &baseCOMPtr_ )
+ ) == S_OK
+ );
+ }
+ ~com_scoped_ptr() { release(); }
+
+ block_manual_lifetime_management_proxy & operator *() const { BOOST_ASSERT( baseCOMPtr_ ); return *static_cast<block_manual_lifetime_management_proxy *>( baseCOMPtr_ ); }
+ block_manual_lifetime_management_proxy * operator->() const { return &operator*(); }
+
+ COMInterface * * operator&()
+ {
+ BOOST_ASSERT( baseCOMPtr_ == NULL );
+ return &baseCOMPtr_;
+ }
+
+ operator COMInterface *() const { return baseCOMPtr_; }
+
+ void release()
+ {
+ if ( baseCOMPtr_ )
+ {
+ baseCOMPtr_->Release();
+ baseCOMPtr_ = 0;
+ }
+ }
+
+ HRESULT create_instance( CLSID const & class_id )
+ {
+ BOOST_ASSERT( baseCOMPtr_ == NULL );
+ return ::CoCreateInstance( class_id, NULL, CLSCTX_INPROC_SERVER, __uuidof( COMInterface ), reinterpret_cast<void * *>( &baseCOMPtr_ ) );
+ }
+
+private:
+ COMInterface * baseCOMPtr_;
+};
+
+
+////////////////////////////////////////////////////////////////////////////////
+///
+/// \class wic_factory
+///
+////////////////////////////////////////////////////////////////////////////////
+
 class wic_factory
 {
 public:
@@ -57,37 +126,42 @@
             if
             (
                 ( ( hr != S_OK ) && ( hr != S_FALSE ) ) ||
- ( ( hr = wic_factory<>::p_imaging_factory_.CoCreateInstance( CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER ) ) != S_OK )
+ ( ( hr = wic_factory::p_imaging_factory_.create_instance( CLSID_WICImagingFactory ) ) != S_OK )
             )
                 io_error( "Boost.GIL failed to load external library." ); //...zzz...duplicated...
- BOOST_ASSERT( wic_factory<>::p_imaging_factory_ );
+ BOOST_ASSERT( wic_factory::p_imaging_factory_ );
         }
         ~creator()
         {
- wic_factory<>::p_imaging_factory_.Release();
+ wic_factory::p_imaging_factory_.release();
             ::CoUninitialize();
         }
     };
 
 private:
- static CComPtr<IWICImagingFactory> p_imaging_factory_;
+ static com_scoped_ptr<IWICImagingFactory> p_imaging_factory_;
 };
 
-template<>
-CComPtr<IWICImagingFactory> wic_factory<>::p_imaging_factory_;
+// Implementation note:
+// GCC supports the __declspec( selectany ) declarator for Windows targets
+// so I'll avoid adding yet another template and assume that all other Windows
+// enabled compilers do too.
+// (10.10.2010.) (Domagoj Saric)
+__declspec( selectany )
+com_scoped_ptr<IWICImagingFactory> wic_factory::p_imaging_factory_;
 
 
 #if BOOST_LIB_INIT( BOOST_GIL_EXTERNAL_LIB ) == BOOST_LIB_INIT_ASSUME
 
- typedef wic_factory<>::creator wic_user_guard;
+ typedef wic_factory::creator wic_user_guard;
 
- struct wic_base_guard {};
+ struct wic_base_guard {};
 
 #else // BOOST_LIB_INIT( BOOST_GIL_EXTERNAL_LIB ) == BOOST_LIB_INIT_AUTO
 
- struct wic_user_guard {};
+ struct wic_user_guard {};
 
- typedef wic_factory<>::creator wic_base_guard;
+ typedef wic_factory::creator wic_base_guard;
 
 #endif // BOOST_LIB_INIT( BOOST_GIL_EXTERNAL_LIB ) == BOOST_LIB_INIT_AUTO
 

Modified: sandbox/gil/boost/gil/extension/io2/wic_image.hpp
==============================================================================
--- sandbox/gil/boost/gil/extension/io2/wic_image.hpp (original)
+++ sandbox/gil/boost/gil/extension/io2/wic_image.hpp 2010-10-10 11:56:10 EDT (Sun, 10 Oct 2010)
@@ -194,9 +194,9 @@
 public:
     struct lib_object_t
     {
- CComPtr<IWICBitmapFrameEncode> p_frame_ ;
- CComPtr<IWICBitmapEncoder > p_encoder_ ;
- IPropertyBag2 * p_frame_parameters_;
+ detail::com_scoped_ptr<IWICBitmapFrameEncode> p_frame_ ;
+ detail::com_scoped_ptr<IWICBitmapEncoder > p_encoder_ ;
+ IPropertyBag2 * p_frame_parameters_;
     };
 
     lib_object_t & lib_object() { return lib_object_; }
@@ -249,8 +249,8 @@
 private:
     void create_encoder( IStream & target, GUID const & format )
     {
- ensure_result( wic_factory<>::singleton().CreateEncoder( format, NULL, &lib_object().p_encoder_ ) );
- ensure_result( lib_object().p_encoder_->Initialize( &target, WICBitmapEncoderNoCache ) );
+ ensure_result( wic_factory::singleton().CreateEncoder( format, NULL, &lib_object().p_encoder_ ) );
+ ensure_result( lib_object().p_encoder_->Initialize( &target, WICBitmapEncoderNoCache ) );
         ensure_result( lib_object().p_encoder_->CreateNewFrame( &lib_object().p_frame_, &lib_object().p_frame_parameters_ ) );
     }
 
@@ -354,8 +354,8 @@
     // (26.07.2010.) (Domagoj Saric)
     typedef std::pair
             <
- CComPtr<IWICBitmapFrameDecode>,
- CComPtr<IWICBitmapDecoder >
+ detail::com_scoped_ptr<IWICBitmapFrameDecode>,
+ detail::com_scoped_ptr<IWICBitmapDecoder >
> lib_object_t;
 
     typedef detail::wic_user_guard guard;
@@ -376,14 +376,14 @@
     explicit wic_image( IStream & stream )
     {
         using namespace detail;
- ensure_result( wic_factory<>::singleton().CreateDecoderFromStream( &stream, NULL, WICDecodeMetadataCacheOnDemand, &lib_object().second ) );
+ ensure_result( wic_factory::singleton().CreateDecoderFromStream( &stream, NULL, WICDecodeMetadataCacheOnDemand, &lib_object().second ) );
         create_first_frame_decoder();
     }
 
     explicit wic_image( HANDLE const file_handle )
     {
         using namespace detail;
- ensure_result( wic_factory<>::singleton().CreateDecoderFromFileHandle( reinterpret_cast<ULONG_PTR>( file_handle ), NULL, WICDecodeMetadataCacheOnDemand, &lib_object().second ) );
+ ensure_result( wic_factory::singleton().CreateDecoderFromFileHandle( reinterpret_cast<ULONG_PTR>( file_handle ), NULL, WICDecodeMetadataCacheOnDemand, &lib_object().second ) );
         create_first_frame_decoder();
     }
 
@@ -395,7 +395,7 @@
     //{
     // ensure_result
     // (
- // wic_factory<>::singleton().CreateBitmapFromMemory
+ // wic_factory::singleton().CreateBitmapFromMemory
     // (
     // view.width(),
     // view.height(),
@@ -425,11 +425,11 @@
     static std::size_t format_size( format_t /*const*/ format )
     {
         using namespace detail;
- CComQIPtr<IWICComponentInfo> p_component_info;
- ensure_result( wic_factory<>::singleton().CreateComponentInfo( format, &p_component_info ) );
- //CComQIPtr<IWICPixelFormatInfo> p_pixel_format_info;
- //p_component_info->QueryInterface( );(IID_IWICPixelFormatInfo
- CComQIPtr<IWICPixelFormatInfo> const p_pixel_format_info( p_component_info );
+ com_scoped_ptr<IWICComponentInfo> p_component_info;
+ ensure_result( wic_factory::singleton().CreateComponentInfo( format, &p_component_info ) );
+ //com_scoped_ptr<IWICPixelFormatInfo> p_pixel_format_info;
+ //p_component_info->QueryInterface( );IID_IWICPixelFormatInfo
+ com_scoped_ptr<IWICPixelFormatInfo> const p_pixel_format_info( *p_component_info );
         io_error_if_not( p_pixel_format_info, "WIC failure" );
         unsigned int bits_per_pixel;
         verify_result( p_pixel_format_info->GetBitsPerPixel( &bits_per_pixel ) );
@@ -466,9 +466,9 @@
         //...non template related code yet to be extracted...
         point2<std::ptrdiff_t> const & targetDimensions( original_view( view ).dimensions() );
         wic_roi const roi( get_offset<wic_roi::offset_t>( view ), targetDimensions.x, targetDimensions.y );
- CComQIPtr<IWICBitmap> p_bitmap;
- ensure_result( wic_factory<>::singleton().CreateBitmapFromSourceRect( &frame_decoder(), roi.X, roi.Y, roi.Width, roi.Height, &p_bitmap ) );
- CComQIPtr<IWICBitmapLock> p_bitmap_lock;
+ com_scoped_ptr<IWICBitmap> p_bitmap;
+ ensure_result( wic_factory::singleton().CreateBitmapFromSourceRect( &frame_decoder(), roi.X, roi.Y, roi.Width, roi.Height, &p_bitmap ) );
+ com_scoped_ptr<IWICBitmapLock> p_bitmap_lock;
         ensure_result( p_bitmap->Lock( &roi, WICBitmapLockRead, &p_bitmap_lock ) );
         unsigned int buffer_size;
         BYTE * p_buffer;
@@ -499,8 +499,8 @@
     {
         BOOST_ASSERT( view_data.format_ != GUID_WICPixelFormatUndefined ); //...zzz...
         using namespace detail;
- CComQIPtr<IWICFormatConverter> p_converter;
- ensure_result( wic_factory<>::singleton().CreateFormatConverter( &p_converter ) );
+ com_scoped_ptr<IWICFormatConverter> p_converter;
+ ensure_result( wic_factory::singleton().CreateFormatConverter( &p_converter ) );
         ensure_result( p_converter->Initialize( &frame_decoder(), view_data.format_, WICBitmapDitherTypeNone, NULL, 0, WICBitmapPaletteTypeCustom ) );
         ensure_result
         (
@@ -536,7 +536,7 @@
     void create_decoder_from_filename( wchar_t const * const filename )
     {
         using namespace detail;
- ensure_result( wic_factory<>::singleton().CreateDecoderFromFilename( filename, NULL, GENERIC_READ, WICDecodeMetadataCacheOnDemand, &lib_object().second ) );
+ ensure_result( wic_factory::singleton().CreateDecoderFromFilename( filename, NULL, GENERIC_READ, WICDecodeMetadataCacheOnDemand, &lib_object().second ) );
         create_first_frame_decoder();
     }
 


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk