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

at_relative_pointer.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_relative_pointer_h_x
00016 #define x_at_relative_pointer_h_x 1
00017 
00018 #include "at_types.h"
00019 
00020 #include <cstddef>
00021 
00022 // Austria namespace
00023 namespace at
00024 {
00025         
00026 // ======== MapHeader ================================================
00032 struct MapHeader
00033 {
00034         Uint32                                                                          m_magic;
00035         Uint32                                                                          m_length;
00036 };
00037 
00038 
00039 // ======== RelativePointer ========================================
00046 template <
00047         typename                w_pointer_type,
00048         typename                w_storage_type=Uint32,
00049         typename                w_header_type=MapHeader
00050 >
00051 struct RelativePointer
00052 {
00053         w_storage_type                                                          m_value;
00054 
00055         typedef w_header_type                                           t_HeaderType;
00056         typedef w_storage_type                                          t_StorageType;
00057 
00058         // ======== t_RelativePointerReference ============================
00064         struct t_RelativePointerReference
00065         {
00066 
00073                 w_storage_type                                           * m_value_ptr;
00074                 const w_header_type                                      * m_map_addr;
00075 
00076 
00077                 // ======== t_RelativePointerReference ========================
00083                 t_RelativePointerReference(
00084                         w_storage_type                                   * i_value_ptr,
00085                         const w_header_type                              * i_map_addr
00086                 )
00087                   : m_value_ptr( i_value_ptr ),
00088                         m_map_addr( i_map_addr )
00089                 {
00090                 };
00091 
00092 
00093                 private:
00094                 
00095                 // ======== MapFileToMemory ===================================
00103                 inline w_pointer_type MapFileToMemory() const
00104                 {
00105 
00106                         w_storage_type                                  l_file_addr = * m_value_ptr;
00107 
00108                         if ( l_file_addr == 0 )
00109                         {
00110                                 return 0;
00111                         }
00112                         
00113                         return reinterpret_cast<w_pointer_type>(
00114                                 reinterpret_cast<std::ptrdiff_t>( m_map_addr ) + * m_value_ptr
00115                         );
00116 
00117                 } // end MapFileToMemory
00118 
00119 
00120                 // ======== MapMemoryToFile ===================================
00128                 inline w_storage_type MapMemoryToFile( w_pointer_type i_ptr ) const
00129                 {
00130 
00131                         if ( i_ptr == 0 )
00132                         {
00133                                 return 0;
00134                         }
00135 
00136                         return static_cast<w_storage_type>(
00137                                 reinterpret_cast<std::ptrdiff_t>( i_ptr )
00138                                 - reinterpret_cast<std::ptrdiff_t>( m_map_addr )
00139                         );
00140 
00141                 } // end MapMemoryToFile
00142 
00143                 public:
00144                 
00145                 // ======== operator w_pointer_type ===========================
00152                 inline operator w_pointer_type () const
00153                 {
00154                         return MapFileToMemory();
00155 
00156                 } // end operator w_pointer_type
00157 
00158                 // ======== operator= ( w_pointer_type i_new_value ) ==========
00165                 inline t_RelativePointerReference operator= ( w_pointer_type i_new_value ) const
00166                 {
00167 
00168                         w_storage_type l_rel_ptr = MapMemoryToFile( i_new_value );
00169                         
00170                         //
00171                         // could do a check here to make sure that the pointer
00172                         // does in fact map to the address space
00173                         
00174                         * m_value_ptr = l_rel_ptr;
00175 
00176                         return * this;
00177 
00178                 } // end w_pointer_type operator= ( w_pointer_type i_new_value )
00179 
00180 
00181                 // ======== w_pointer_type operator-> () const ================
00188                 inline w_pointer_type operator-> () const
00189                 {
00190                         return MapFileToMemory();
00191 
00192                 } // end w_pointer_type operator-> () const
00193 
00194         };
00195         
00196 
00197         inline const t_RelativePointerReference operator()( const w_header_type * i_map_addr )
00198         {
00199                 return t_RelativePointerReference( & m_value, i_map_addr );
00200         }
00201 
00208         inline RelativePointer()
00209           : m_value( 0 )
00210         {
00211         }
00212 
00213         private:
00214 
00220         RelativePointer & operator=( const RelativePointer & i_rel_ptr );
00221 
00226         RelativePointer( const RelativePointer & i_rel_ptr );
00227 };
00228 
00229 }; // namespace
00230 #endif // x_at_relative_pointer_h_x
00231 
00232 

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