Menu

CaseSelector.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 _Case_Selector_
00042 #define _Case_Selector_
00043 
00044 #include <stdint.h>
00045 
00046 #include "config/type_traits/ByteCount.h"
00047 
00048 #include "mw/attributes/detail/AttributeElementHeader.h"
00049 #include "mw/attributes/detail/HighDensityIDs.h"
00050 
00051 namespace famouso {
00052     namespace mw {
00053         namespace attributes {
00054             namespace detail {
00055 
00072                 template <typename ResultType, ResultType res1, ResultType res2,
00073                           ResultType res3, ResultType res4, ResultType res5, ResultType res6>
00074                 struct CaseSelector {
00075                         typedef CaseSelector type;
00076 
00077                         // TODO: This could be a static method, the static constants which are
00078                         //  calculated below could be members of an inner struct very similar to
00079                         //  this one and the method only needs to access these; The interface is
00080                         //  a lot simpler to explain if this is a method
00081                         template <typename Attr>
00082                         struct select_ct {
00083                             private:
00084                                 // The number of bits used by the attribute's value
00085                                 static const uint16_t bitCount = BitCount<typename Attr::value_type,
00086                                                                           Attr::value>::value;
00087 
00088                                 // The whole bytes used by the attribute's value
00089                                 static const uint16_t byteCount = ByteCount<typename Attr::value_type,
00090                                                                             Attr::value>::value;
00091 
00092                             public:
00093                                 static const ResultType value =
00094                                     (Attr::highDensity) ?
00095                                         ( // For high density attributes
00096                                             (bitCount < 3) ?
00097                                                 // If the value fits the header byte unextended, this is
00098                                                 //  the first case
00099                                                 res1 :
00100 
00101                                                 // Check if the value can be written directly (without
00102                                                 //  a length)
00103                                                 (bitCount < 11) ?
00104 
00105                                                     // If the value fits 10 bits (2 bits of the header
00106                                                     //  byte and 8 bits of the extension) this is the
00107                                                     //  second case
00108                                                     res2 :
00109 
00110                                                     // If the length fits the header byte unextended,
00111                                                     //  we would write the header byte + "length-many"
00112                                                     //  bytes (Length-values up to 3 fit into two bits,
00113                                                     //  which would be remaining)
00114                                                     (byteCount < 4) ?
00115 
00116                                                         // So this is the third case
00117                                                         res3 :
00118 
00119                                                         // Otherwise we would extend the header byte
00120                                                         //  and so need one more byte which is the fourth
00121                                                         //  case
00122                                                         res4) :
00123 
00124                                         ( // For low density attributes
00125 
00126                                             // The last part of the header byte is always the length, we
00127                                             //  now must find out, if the header must be extended for the
00128                                             //  length
00129                                             (byteCount < 8) ?
00130 
00131                                                 // The length fits unextended, so we need 1 byte for the
00132                                                 //  header itself, one byte for the type and "length-many"
00133                                                 //  bytes for the value so this is the fifth case
00134                                                 res5 :
00135 
00136                                                 // We must extend the header byte, that is one more byte
00137                                                 //  compared to the first case is needed which is the sixth
00138                                                 //  and last case
00139                                                 res6
00140                                         );
00141                         };
00142 
00150                         static const ResultType select_rt(const AttributeElementHeader* const header) {
00151                             // We have to use the element header struct here instead of the
00152                             //  attribute header struct since we would create circular include
00153                             //  dependencies otherwise
00154 
00155                             if (header->isHighDensity()) {
00156                                 // High density attributes
00157                                 if (header->lengthValueSwitch) {
00158                                     // Length is contained
00159                                     if (header->extension) {
00160                                         // Length fits extended
00161                                         return (res4);
00162                                     } else {
00163                                         // Length fits unextended
00164                                         return (res3);
00165                                     }
00166                                 } else {
00167                                     // Value is contained
00168                                     if (header->extension) {
00169                                         // Value fits extended
00170                                         return (res2);
00171                                     } else {
00172                                         // Value fits unextended
00173                                         return (res1);
00174                                     }
00175                                 }
00176                             } else {
00177                                 // Low density attributes
00178                                 if (header->extension) {
00179                                     // Length fits extended
00180                                     return (res6);
00181                                 } else {
00182                                     // Length fits unextended
00183                                     return (res5);
00184                                 }
00185                             }
00186                         }
00187                 };
00188 
00189             } // end namespace detail
00190         } // end namespace attributes
00191     } // end namespace mw
00192 } // end namespace famouso
00193 
00194 #endif // _Case_Selector_