Main Page   Modules   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

at_unionptr.h

Go to the documentation of this file.
00001 //
00002 // The Austria library is copyright (c) Gianni Mariani 2004.
00003 // 
00004 // Grant Of License.  Grants to LICENSEE the non-exclusive right to use the Austria
00005 // library subject to the terms of the LGPL.
00006 // 
00007 // A copy of the license is available in this directory or one may be found at this URL:
00008 //      http://www.gnu.org/copyleft/lesser.txt
00009 // 
00015 #ifndef x_at_unionptr_h_x
00016 #define x_at_unionptr_h_x 1
00017 
00018 #include "at_types.h"
00019 
00020 #include "at_assert.h"
00021 
00022 // Austria namespace
00023 namespace at
00024 {
00025 
00026 // ======== AT_UnionPtr_Empty =========================================
00033 class UnionPtr_Empty_A
00034 {
00035 };
00036 
00043 class UnionPtr_Empty_B
00044 {
00045 };
00046 
00047 
00048 
00049 // ======== UnionPtr ===============================================
00073 template <
00074     typename    w_enum_type,
00075     typename    w_type_0,
00076     typename    w_type_1,
00077     typename    w_type_2 = UnionPtr_Empty_A,
00078     typename    w_type_3 = UnionPtr_Empty_B
00079 >
00080 class UnionPtr
00081 {
00082     public:
00083 
00084 
00085     // ======== t_PtrUnion ============================================
00091     union t_PtrUnion
00092     {
00093         w_type_0                                  m_value_0;
00094         w_type_1                                  m_value_1;
00095         w_type_2                                  m_value_2;
00096         w_type_3                                  m_value_3;
00097         PtrDiff                                   m_ptr_diff;
00098     };
00099 
00100     t_PtrUnion                                    m_value;
00101 
00102 
00103     // ======== t_Check ===============================================
00111     private:
00112     struct t_Check
00113     {
00114         char m_array_overflow[
00115             1/ int( !(
00116                 sizeof( t_PtrUnion ) != sizeof( PtrDiff )
00117                 || ( sizeof( ( static_cast< t_PtrUnion * >( 0 ) )->m_value_0 ) < 4 )
00118                 || ( sizeof( ( static_cast< t_PtrUnion * >( 0 ) )->m_value_1 ) < 4 )
00119             ) )
00120         ];
00121     };
00122     
00123 
00124     public:
00125     // ======== Get ===================================================
00134     inline w_enum_type Get(
00135         w_type_0                & o_param_0,
00136         w_type_1                & o_param_1,
00137         w_type_2                & o_param_2 = ( * static_cast< UnionPtr_Empty_A * >( 0 ) ),
00138         w_type_3                & o_param_3 = ( * static_cast< UnionPtr_Empty_B * >( 0 ) )
00139     ) const
00140     {
00141 
00142         t_PtrUnion l_ptr = m_value;
00143 
00144         int l_type = m_value.m_ptr_diff & PtrDiff( 0x3 );
00145 
00146         if ( l_type == 0 )
00147         {
00148             o_param_0 = m_value.m_value_0;
00149             return static_cast<w_enum_type>( 0 );
00150         }
00151         
00152         l_ptr.m_ptr_diff &= ~PtrDiff( 0x3 );
00153 
00154         switch ( l_type )
00155         {
00156             case 1 : o_param_1 = l_ptr.m_value_1; return static_cast<w_enum_type>( 1 );
00157             case 2 : o_param_2 = l_ptr.m_value_2; return static_cast<w_enum_type>( 2 );
00158             default : o_param_3 = l_ptr.m_value_3; return static_cast<w_enum_type>( 3 );
00159         }
00160 
00161     } // end Get
00162 
00163     inline UnionPtr & operator=( const w_type_0 i_value )
00164     {
00165         m_value.m_value_0 = i_value;
00166         
00167         return * this;
00168     }
00169     
00170     inline UnionPtr & operator=( const w_type_1 i_value )
00171     {
00172         t_PtrUnion l_ptr;
00173 
00174         l_ptr.m_value_1 = i_value;
00175 
00176         l_ptr.m_ptr_diff |= PtrDiff( 0x1 );
00177 
00178         m_value = l_ptr;
00179 
00180         return * this;
00181     }
00182     
00183     inline UnionPtr & operator=( const w_type_2 i_value )
00184     {
00185         t_PtrUnion l_ptr;
00186 
00187         l_ptr.m_value_2 = i_value;
00188 
00189         l_ptr.m_ptr_diff |= PtrDiff( 0x2 );
00190 
00191         m_value = l_ptr;
00192 
00193         return * this;
00194     }
00195     
00196     inline UnionPtr & operator=( const w_type_3 i_value )
00197     {
00198         t_PtrUnion l_ptr;
00199 
00200         l_ptr.m_value_3 = i_value;
00201 
00202         l_ptr.m_ptr_diff |= PtrDiff( 0x3 );
00203 
00204         m_value = l_ptr;
00205 
00206         return * this;
00207     }
00208     
00209     inline bool operator==( const UnionPtr & i_value ) const
00210     {
00211         return m_value.m_value_0 == i_value.m_value.m_value_0;
00212     }
00213     
00214     inline UnionPtr()
00215     {
00216     }
00217     
00218     inline UnionPtr( const w_type_0 i_value )
00219     {
00220         m_value.m_value_0 = i_value;
00221         
00222     }
00223     
00224     inline UnionPtr( const w_type_1 i_value )
00225     {
00226         t_PtrUnion l_ptr;
00227 
00228         l_ptr.m_value_1 = i_value;
00229 
00230         l_ptr.m_ptr_diff |= PtrDiff( 0x1 );
00231 
00232         m_value = l_ptr;
00233 
00234     }
00235     
00236     inline UnionPtr( const w_type_2 i_value )
00237     {
00238         t_PtrUnion l_ptr;
00239 
00240         l_ptr.m_value_2 = i_value;
00241 
00242         l_ptr.m_ptr_diff |= PtrDiff( 0x2 );
00243 
00244         m_value = l_ptr;
00245 
00246     }
00247     
00248     inline UnionPtr( const w_type_3 i_value )
00249     {
00250         t_PtrUnion l_ptr;
00251 
00252         l_ptr.m_value_3 = i_value;
00253 
00254         l_ptr.m_ptr_diff |= PtrDiff( 0x3 );
00255 
00256         m_value = l_ptr;
00257         
00258     }
00259     
00260 };
00261 
00262 
00263 
00264 // ======== UnionPtrType ===========================================
00271 template <
00272     typename    w_enum_type,
00273     typename    w_type_0,
00274     typename    w_type_1
00275 >
00276 class UnionPtrType
00277 {
00278     public:
00279 
00280 
00281     // ======== t_PtrUnion ============================================
00287     union t_PtrUnion
00288     {
00289         w_type_0                                  m_value_0;
00290         w_type_1                                  m_value_1;
00291         PtrDiff                                m_ptr_diff;
00292     };
00293 
00294     t_PtrUnion                                    m_value;
00295 
00296 
00297     // ======== t_Check ===============================================
00305     private:
00306     struct t_Check
00307     {
00308         char    m_array_overflow[
00309             1/ int( !(
00310                 sizeof( t_PtrUnion ) != sizeof( PtrDiff )
00311                 || ( sizeof( ( static_cast< t_PtrUnion * >( 0 ) )->m_value_0 ) < 4 )
00312                 || ( sizeof( ( static_cast< t_PtrUnion * >( 0 ) )->m_value_1 ) < 4 )
00313             ) )
00314         ];
00315     };
00316     
00317     public:
00318     // ======== Get ===================================================
00328     inline w_enum_type Get(
00329         w_type_0                                & o_param_0,
00330         w_type_1                                & o_param_1
00331     ) const
00332     {
00333 
00334         t_PtrUnion l_ptr = m_value;
00335 
00336         int l_type = m_value.m_ptr_diff & PtrDiff( 0x3 );
00337 
00338         if ( l_type == 0 )
00339         {
00340             o_param_0 = m_value.m_value_0;
00341             return static_cast<w_enum_type>( 0 );
00342         }
00343         
00344         l_ptr.m_ptr_diff &= ~PtrDiff( 0x3 );
00345 
00346         o_param_1 = l_ptr.m_value_1;
00347 
00348         return static_cast<w_enum_type>( l_type );
00349 
00350 
00351     } // end Get
00352 
00360     inline UnionPtrType & operator=( const w_type_0 i_value )
00361     {
00362         m_value.m_value_0 = i_value;
00363         
00364         return * this;
00365     }
00366     
00373     inline bool operator==( const UnionPtrType & i_value ) const
00374     {
00375         return m_value.m_value_0 == i_value.m_value.m_value_0;
00376     }
00377     
00385     inline UnionPtrType & operator=( const w_type_1 i_value )
00386     {
00387         t_PtrUnion l_ptr;
00388 
00389         l_ptr.m_value_1 = i_value;
00390 
00391         l_ptr.m_ptr_diff |= PtrDiff( 0x1 );
00392 
00393         m_value = l_ptr;
00394 
00395         return * this;
00396     }
00397     
00398     inline UnionPtrType()
00399     {
00400     }
00401     
00402     inline UnionPtrType( const w_type_0 i_value )
00403     {
00404         m_value.m_value_0 = i_value;
00405         
00406     }
00407     
00408     inline UnionPtrType( const w_type_1 i_value )
00409     {
00410         t_PtrUnion l_ptr;
00411 
00412         l_ptr.m_value_1 = i_value;
00413 
00414         l_ptr.m_ptr_diff |= PtrDiff( 0x1 );
00415 
00416         m_value = l_ptr;
00417 
00418     }
00419     
00420     inline UnionPtrType & Set( const w_type_1 i_value, w_enum_type i_type )
00421     {
00422         t_PtrUnion l_ptr;
00423 
00424         AT_Assert( i_type != 0 );
00425 
00426         l_ptr.m_value_1 = i_value;
00427 
00428         l_ptr.m_ptr_diff |= PtrDiff( i_type );
00429 
00430         m_value = l_ptr;
00431 
00432         return * this;
00433     }
00434     
00435 };
00436 
00437 }; // namespace
00438 #endif // x_at_unionptr_h_x
00439 
00440 

Generated for Austria by doxygen and MakeXS at Sun Oct 24 17:35:34 PDT 2004