File indexing completed on 2024-04-28 05:32:09

0001 #ifndef oxygencairosurface_h
0002 #define oxygencairosurface_h
0003 /*
0004 * this file is part of the oxygen gtk engine
0005 * SPDX-FileCopyrightText: 2010 Hugo Pereira Da Costa <hugo.pereira@free.fr>
0006 *
0007 * SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #include <cairo.h>
0011 #include <cassert>
0012 
0013 namespace Oxygen
0014 {
0015     namespace Cairo
0016     {
0017 
0018         //! wrapper class around cairo_surface_t structure
0019         class Surface
0020         {
0021             public:
0022 
0023             //! empty constructor is private
0024             Surface( void ):
0025                 _surface( 0L )
0026             {}
0027 
0028             //! constructor
0029             Surface( cairo_surface_t* surface ):
0030                 _surface( surface )
0031             {}
0032 
0033             //! destructor
0034             virtual ~Surface( void )
0035             { free(); }
0036 
0037 
0038             //! copy constructor is private
0039             Surface( const Surface& other ):
0040                 _surface( other._surface )
0041             { if( _surface ) cairo_surface_reference( _surface ); }
0042 
0043             //! equal to operator is private
0044             Surface& operator = (const Surface& other )
0045             {
0046                 cairo_surface_t* old( _surface );
0047                 _surface = other._surface;
0048                 if( _surface ) cairo_surface_reference( _surface );
0049                 if( old ) cairo_surface_destroy( old );
0050                 return *this;
0051             }
0052 
0053             //! returns true if valid
0054             bool isValid( void ) const
0055             { return (bool) _surface; }
0056 
0057             //! set surface
0058             void set( cairo_surface_t* surface )
0059             {
0060                 assert( !_surface );
0061                 _surface = surface;
0062             }
0063 
0064             //! free the surface
0065             /*!
0066             it should not be necessary to call this method
0067             since it is already handled in destructor
0068             */
0069             void free( void )
0070             {
0071                 if( _surface )
0072                 {
0073                     cairo_surface_destroy( _surface );
0074                     _surface = 0L;
0075                 }
0076             }
0077 
0078             //! cast to cairo_surface_t
0079             operator cairo_surface_t* (void) const
0080             { return _surface; }
0081 
0082             private:
0083 
0084             //! equal to operator is private
0085             Surface& operator = (cairo_surface_t* other )
0086             {
0087                 assert( false );
0088                 return *this;
0089             }
0090 
0091             //! surface
0092             cairo_surface_t* _surface;
0093 
0094 
0095         };
0096 
0097     }
0098 }
0099 #endif