Menu

AttributeSet.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_h_
00042 #define _Attribute_Set_h_
00043 
00044 #include <stdint.h>
00045 #include <string.h>
00046 
00047 #include "boost/mpl/is_sequence.hpp"
00048 #include "boost/mpl/vector.hpp"
00049 #include "boost/mpl/find_if.hpp"
00050 #include "boost/mpl/void.hpp"
00051 #include "boost/mpl/placeholders.hpp"
00052 #include "boost/mpl/deref.hpp"
00053 #include "boost/mpl/eval_if.hpp"
00054 
00055 #include "assert/staticerror.h"
00056 
00057 #include "object/PlacementNew.h"
00058 
00059 #include "mw/attributes/detail/AttributeSetImpl.h"
00060 #include "mw/attributes/detail/find.h"
00061 #include "mw/attributes/detail/AttributeSize.h"
00062 #include "mw/attributes/detail/Duplicates.h"
00063 #include "mw/attributes/detail/AttributeCompileErrors.h"
00064 
00065 #include "mw/attributes/access/Attribute_RT.h"
00066 #include "mw/attributes/access/AttributeSet_RT.h"
00067 #include "mw/attributes/access/AttributeSetHeader_RT.h"
00068 
00069 #include "mw/attributes/Null.h"
00070 #include "mw/attributes/type_traits/is_same_base_type.h"
00071 
00072 namespace famouso {
00073     namespace mw {
00074         namespace attributes {
00075 
00083             template <typename AttrSeq = boost::mpl::vector<> >
00084             struct AttributeSet : public famouso::mw::attributes::access::AttributeSet_RT {
00085                 private:
00086                     // Assert that a forward sequence is given
00087                     FAMOUSO_STATIC_ASSERT_ERROR(
00088                         (boost::mpl::is_sequence<AttrSeq>::value),
00089                         no_forward_sequence_given,
00090                         (AttrSeq));
00091 
00092                     // Duplicate-Tester for the given attribute sequence
00093                     typedef detail::Duplicates<AttrSeq> duplicateTester;
00094 
00095                     // Assert that the sequence does not contain duplicates, if it does print out
00096                     //  the first attribute in the sequence for which a duplicate could be found
00097                     //  and the whole sequence, too
00098                     FAMOUSO_STATIC_ASSERT_ERROR(
00099                         (!duplicateTester::result),
00100                         duplicate_attribute_detected_in_sequence,
00101                         (typename duplicateTester::duplicateAttribute, AttrSeq));
00102 
00103                     // The implementation struct of the actual attribute set
00104                     typedef detail::AttributeSetImpl<AttrSeq> impl;
00105 
00106                     // The content size (the bytes needed for the attribute data) is determined
00107                     //  by the implementation struct
00108                     static const uint16_t contentSize = impl::size;
00109 
00110                 public:
00114                     typedef AttributeSet type;
00115 
00119                     typedef AttrSeq sequence;
00120 
00121                 private:
00122                     // True, if the set header will be written extended, false if the
00123                     //  header fits one byte
00124                     static const bool extension = (contentSize > 0x7F);
00125 
00126                     // Assert that the sequence size fits the format bounds
00127                     typedef typename boost::mpl::eval_if_c<
00128                                                   extension,
00129                                                   detail::ExtendedSequenceBoundError<contentSize>,
00130                                                   detail::UnextendedSequenceBoundError<contentSize>
00131                                                  >::type assertDummy;
00132 
00133                     // The size of the header is 1 byte if it is not extended and 2 bytes
00134                     //  if it is extended
00135                     static const uint8_t headerSize = (extension ? 2 : 1);
00136 
00137                 public:
00144                     static const uint16_t overallSize = headerSize + contentSize;
00145 
00146                 private:
00151                     uint8_t data[overallSize];
00152 
00153                 public:
00158                     AttributeSet() {
00159                         // The header always starts at index 0 (In this case we do not have
00160                         //  to consider the offset since it is always 0 at this point)
00161                         writeSize(contentSize, extension);
00162 
00163                         // The construction of the attribute binary data is done by the
00164                         //  wrapped implementation
00165                         impl::construct(&data[headerSize]);
00166                     }
00167 
00177                     template <typename Attr>
00178                     struct find_ct {
00179                         private:
00180                             // The predicate for finding the specified attribute
00181                             typedef famouso::mw::attributes::type_traits::is_same_base_type<
00182                                                                            boost::mpl::_1,
00183                                                                            Attr
00184                                                                           > pred;
00185 
00186                             // Search the attribute in the underlying sequence
00187                             typedef typename boost::mpl::find_if<
00188                                                           sequence,
00189                                                           pred
00190                                                          >::type found;
00191 
00192                             // Helper struct to map void_ to Null
00193                             template <typename Found, bool dummy>
00194                             struct helper {
00195                                     typedef Found type;
00196                             };
00197                             template <bool dummy>
00198                             struct helper<boost::mpl::void_, dummy> {
00199                                     typedef Null type;
00200                             };
00201 
00202                         public:
00203                             typedef typename helper<
00204                                               typename boost::mpl::deref<found>::type,
00205                                               true
00206                                              >::type type;
00207                     };
00208 
00209             };
00210 
00211         } // end namespace attributes
00212     } // end namespace mw
00213 } // end namespace famouso
00214 
00215 
00216 #endif // _Attribute_Set_