AttributeHeader_RT.h
Go to the documentation of this file.00001 /******************************************************************************* 00002 * 00003 * Copyright (c) 2008-2010 Michael Schulze <mschulze@ivs.cs.uni-magdeburg.de> 00004 * 2010 Marcus Foerster <MarcusFoerster1@gmx.de> 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * 00014 * * Redistributions in binary form must reproduce the above copyright 00015 * notice, this list of conditions and the following disclaimer in 00016 * the documentation and/or other materials provided with the 00017 * distribution. 00018 * 00019 * * Neither the name of the copyright holders nor the names of 00020 * contributors may be used to endorse or promote products derived 00021 * from this software without specific prior written permission. 00022 * 00023 * 00024 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 00025 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 00026 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00027 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00028 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00029 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00030 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00031 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00032 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00033 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00034 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00035 * 00036 * 00037 * $Id$ 00038 * 00039 ******************************************************************************/ 00040 00041 #ifndef _Attribute_Header_RT_h_ 00042 #define _Attribute_Header_RT_h_ 00043 00044 #include <stdint.h> 00045 00046 #include "util/endianness.h" 00047 00048 #include "mw/attributes/detail/CaseSelector.h" 00049 00050 #include "mw/attributes/detail/AttributeElementHeader.h" 00051 #include "mw/attributes/detail/HighDensityIDs.h" 00052 00053 namespace famouso { 00054 namespace mw { 00055 namespace attributes { 00056 namespace access { 00057 00072 struct AttributeHeader_RT { 00073 private: 00074 typedef famouso::mw::attributes::detail::CaseSelector< 00075 uint8_t, 1, 1, 1, 2, (1 + 1), (1 + 1 + 1) 00076 >::type sizeSelector; 00077 00078 protected: 00079 AttributeHeader_RT() { 00080 // Visibility 00081 } 00082 00083 // Shortcut typedef for the attribute header union type 00084 typedef famouso::mw::attributes::detail::AttributeElementHeader elemHeader_t; 00085 00086 const elemHeader_t* const asElementHeader() const { 00087 return (reinterpret_cast<const elemHeader_t* const>(this)); 00088 } 00089 00090 elemHeader_t* const asElementHeader() { 00091 return (reinterpret_cast<elemHeader_t* const>(this)); 00092 } 00093 00094 private: 00098 bool isExtended() const { 00099 return (asElementHeader()->extension); 00100 } 00101 00102 public: 00106 bool isHighDensity() const { 00107 return (asElementHeader()->isHighDensity()); 00108 } 00109 00114 uint8_t getID() const { 00115 if (isHighDensity()) { 00116 return (asElementHeader()->category); 00117 } else { 00118 const uint8_t* const data = reinterpret_cast<const uint8_t* const>(this); 00119 00120 return (data[isExtended() ? 2 : 1]); 00121 } 00122 } 00123 00137 uint16_t valueLength() const { 00138 if (isHighDensity()) { 00139 if (asElementHeader()->lengthValueSwitch) { 00140 // The length is encoded 00141 if (isExtended()) { 00142 // The length is encoded extended, so we reinterpret 00143 // the masked header as the value length 00144 return (ntohs(*(reinterpret_cast< 00145 const uint16_t* 00146 >(this))) & 0x03FF); 00147 } else { 00148 // The length is encoded unextended so we access 00149 // the encoded length using the union member 00150 return (asElementHeader()->valueOrLength); 00151 } 00152 } else { 00153 // Special case that a high density attribute's 00154 // value is encoded as a part of the header 00155 return (isExtended() ? 1 : 0); 00156 } 00157 } else { 00158 // LD-attributes always have their lengths encoded as 00159 // a part of the header, we only have to check the 00160 // extension bit 00161 if (isExtended()) { 00162 return (ntohs(*(reinterpret_cast< 00163 const uint16_t* 00164 >(this))) & 0x07FF); 00165 } else { 00166 return (asElementHeader()->length); 00167 } 00168 } 00169 } 00170 00171 00172 // Additional helper methods 00173 00183 uint8_t headerLength() const { 00184 return (sizeSelector::select_rt(asElementHeader())); 00185 } 00186 }; 00187 00188 } // end namespace access 00189 } // end namespace attributes 00190 } // end namespace mw 00191 } // end namespace famouso 00192 00193 #endif