Menu

find.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 _Find_h_
00042 #define _Find_h_
00043 
00044 #include <stdint.h>
00045 
00046 #include "mw/attributes/access/AttributeSetHeader_RT.h"
00047 #include "mw/attributes/access/AttributeHeader_RT.h"
00048 #include "mw/attributes/access/Attribute_RT.h"
00049 
00050 namespace famouso {
00051     namespace mw {
00052         namespace attributes {
00053             namespace detail {
00054 
00055                 static void* find_impl(uint8_t* data, const uint8_t id, const bool highDensity) {
00056                     // The number of bytes needed by the attributes
00057                     //  contained in the given sequence
00058                     uint16_t seqLen;
00059 
00060                     {
00061                         typedef famouso::mw::attributes::access::AttributeSetHeader_RT setHeaderType;
00062 
00063                         const setHeaderType* const setHeader = reinterpret_cast<const setHeaderType* const>(&data[0]);
00064 
00065                         seqLen = setHeader->contentLength();
00066 
00067                         data += setHeader->headerLength();
00068                     }
00069 
00070                     // The pointer were the given sequence ends
00071                     const uint8_t* const targetPtr = data + seqLen;
00072 
00073                     typedef famouso::mw::attributes::access::AttributeHeader_RT headerType;
00074                     typedef famouso::mw::attributes::access::Attribute_RT attrType;
00075 
00076                     const headerType* header;
00077 
00078                     while (data < targetPtr) {
00079                         header = reinterpret_cast<const headerType*>(data);
00080 
00081                         // Check if the encoded attribute fits the given one
00082                         if ((header->isHighDensity() == highDensity) && (header->getID() == id)) {
00083                             return (data);
00084                         }
00085 
00086                         // We let the attribute class determine its overall size to skip it
00087                         data += reinterpret_cast<const attrType* const>(header)->length();
00088                     }
00089 
00090                     // If we iterated the complete attribute sequence the intended attribute
00091                     //  could not be found and NULL is returned
00092                     return (NULL);
00093                 }
00094 
00095                 template <typename Attr>
00096                 static inline Attr* find(uint8_t* data) {
00097                     return (reinterpret_cast<Attr*>(find_impl(data, Attr::id, Attr::highDensity)));
00098                 }
00099 
00100                 template <typename Attr>
00101                 static inline const Attr* find(const uint8_t* data) {
00102                     return (reinterpret_cast<const Attr*>(find_impl(const_cast<uint8_t*>(data), Attr::id, Attr::highDensity)));
00103                 }
00104 
00105             } // end namespace detail
00106         } // end namespace attributes
00107     } // end namespace attributes
00108 } // end namespace attributes
00109 
00110 #endif