AttributeSetHeader_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_Set_Header_RT_h_ 00042 #define _Attribute_Set_Header_RT_h_ 00043 00044 #include <stdint.h> 00045 00046 #include "boost/mpl/eval_if.hpp" 00047 #include "boost/mpl/integral_c.hpp" 00048 00049 #include "mw/attributes/detail/AttributeCompileErrors.h" 00050 00051 #include "util/endianness.h" 00052 00053 namespace famouso { 00054 namespace mw { 00055 namespace attributes { 00056 namespace access { 00057 00058 struct AttributeSetHeader_RT { 00059 protected: 00060 AttributeSetHeader_RT() { 00061 // Visibility 00062 } 00063 00064 void writeSize(const uint16_t size, const bool extension) { 00065 uint8_t* const data = reinterpret_cast<uint8_t* const>(this); 00066 00067 // Depending on whether the sequence header is extended either 1 00068 // or 2 two bytes must be written accordingly 00069 if (extension) { 00070 // Convert the length to network byte order and set the 00071 // extension bit 00072 const uint16_t tmpSize = htons(size | 0x8000); 00073 // Assign the converted value to the array 00074 *(reinterpret_cast<uint16_t*>(data)) = tmpSize; 00075 } else { 00076 // Write the lower 7 bits of the sequence size into the first 00077 // and only byte (The extension flag assures that the given 00078 // sequence size fits 7 bits) 00079 data[0] = (size & 0x7F); 00080 } 00081 } 00082 00083 public: 00092 uint16_t contentLength() const { 00093 const uint8_t* const data = reinterpret_cast<const uint8_t* const>(this); 00094 00095 if (isExtended()) { 00096 // Get the first two bytes, convert them to host byte order and mask the 00097 // extension bit 00098 return (ntohs(*(reinterpret_cast<const uint16_t* const>(&data[0]))) & 0x7FFF); 00099 } else { 00100 // Simply return the first (and only) byte, the check above assures that 00101 // only the lower seven bits of this byte are set 00102 return (data[0]); 00103 } 00104 } 00105 00106 uint8_t headerLength() const { 00107 return (isExtended() ? 2 : 1); 00108 } 00109 00110 private: 00111 bool contentLength(const uint16_t newSize) { 00112 // Check if the extension bit is set in the current representation 00113 const bool currentExtension = isExtended(); 00114 00115 // Check if the extension bit must be set for the new size 00116 const bool targetExtension = (newSize > 0x7F); 00117 00118 // If the old size was not extended and the new one has to be, we cannot 00119 // perform the operation since we need more bytes than available 00120 if (!currentExtension && targetExtension) { 00121 return (false); 00122 } 00123 00124 writeSize(newSize, targetExtension); 00125 00126 return (true); 00127 } 00128 00129 bool isExtended() const { 00130 return ((*reinterpret_cast<const uint8_t* const>(this) & 0x80) != 0); 00131 } 00132 }; 00133 00134 } // end namespace access 00135 } // end namespace attributes 00136 } // end namespace mw 00137 } // end namespace famouso 00138 00139 #endif // _Attribute_Set_Header_