Menu

AttributeHeader.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_h_
00042 #define _Attribute_Header_h_
00043 
00044 #include <stdint.h>
00045 
00046 #include "util/endianness.h"
00047 
00048 #include "mw/attributes/detail/FirstByteOfHeader.h"
00049 #include "mw/attributes/detail/ValueByteCount.h"
00050 #include "mw/attributes/detail/CaseSelector.h"
00051 
00052 namespace famouso {
00053     namespace mw {
00054         namespace attributes {
00055             namespace detail {
00056 
00063                 template <typename Attr>
00064                 struct AttributeHeader {
00065                     private:
00066                         typedef typename CaseSelector<
00067                                           uint8_t, 1, 1, 1, 2, (1 + 1), (1 + 1 + 1)
00068                                          >::type sizeSelector;
00069 
00070                         // The byte count for the attribute header (It could also include a
00071                         //  part of the attribute's value if the LVS switch is set)
00072                         static const uint8_t size = sizeSelector::template select_ct<Attr>::value;
00073 
00074                         static const bool res2 = (ValueBitCount<Attr>::value < 9) ? false : true;
00075 
00076                         static const bool useOr = CaseSelector<bool,
00077                                                                true,
00078                                                                res2,
00079                                                                false,
00080                                                                false,
00081                                                                false,
00082                                                                false
00083                                                               >::template select_ct<Attr>::value;
00084 
00085                         enum {
00086                             writeLowerLengthBits     = 0x10,
00087                             writeType                = 0x11,
00088                             writeLowerLenBitsAndType = 0x12,
00089                             nothingMore              = 0x13
00090                         };
00091 
00092                         // We use this to decide in the code below if further bytes
00093                         //  must be manipulated
00094                         static const uint8_t toWrite = CaseSelector<uint8_t,
00095                                                                     nothingMore,
00096                                                                     nothingMore,
00097                                                                     nothingMore,
00098                                                                     writeLowerLengthBits,
00099                                                                     writeType,
00100                                                                     writeLowerLenBitsAndType
00101                                                                    >::template select_ct<Attr>::value;
00102 
00103                         // TODO: Let this class also work on the this pointer not having real
00104                         //  own memory
00105                         uint8_t data[size];
00106 
00107                     public:
00108                         typedef AttributeHeader type;
00109 
00114                         AttributeHeader() {
00115                             // Either use an OR operation (to not overwrite existing bits already
00116                             //  set by writing the value) or directly assign the header byte since
00117                             //  there is no danger overwriting
00118                             if (useOr) {
00119                                 data[0] |= (FirstByteOfHeader<Attr>::value);
00120                             } else {
00121                                 data[0] = (FirstByteOfHeader<Attr>::value);
00122                             }
00123 
00124                             // Handle eventually further written bytes (depending on extension,
00125                             //  LVS etc.)
00126                             if (toWrite == writeLowerLengthBits) {
00127                                 data[1] = (ValueByteCount<Attr>::value & 0xFF);
00128                             } else if (toWrite == writeType) {
00129                                 data[1] = (Attr::id & 0xFF);
00130                             } else if (toWrite == writeLowerLenBitsAndType) {
00131                                 data[1] = (ValueByteCount<Attr>::value & 0xFF);
00132                                 data[2] = (Attr::id & 0xFF);
00133                             }
00134                         }
00135                 };
00136 
00137             } // end namespace detail
00138         } // end namespace attributes
00139     } // end namespace mw
00140 } // end namespace famouso
00141 
00142 #endif