Menu

NetworkID.h

Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2008-2010 Michael Schulze <mschulze@ivs.cs.uni-magdeburg.de>
00004  *               2009-2010 Michael Kriese <kriese@cs.uni-magdeburg.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 __NetworkID_hpp__
00042 #define __NetworkID_hpp__
00043 
00044 #include <cstring>
00045 #include "debug.h"
00046 
00047 namespace famouso {
00048     namespace mw {
00049         namespace nl {
00050 
00055             template< uint16_t size = 0 >
00056             struct NetworkID {
00057 
00058                     typedef NetworkID<size> type;
00059 
00065                     static NetworkID parse(uint8_t data[size]) {
00066                         NetworkID result;
00067 
00068                         std::memcpy(result._data, data, size);
00069 
00070                         return result;
00071                     }
00072 
00078                     bool operator==(const NetworkID &o) const {
00079                         return std::memcmp(_data, o._data, size) == 0;
00080                     }
00081 
00086                     bool operator<(const NetworkID &o) const {
00087                         return std::memcmp(_data, o._data, size) < 0;
00088                     }
00089 
00094                     void print(::logging::loggingReturnType &out) const {
00095                         char tmp;
00096                         for (uint16_t i = 0; i < size; i++) {
00097                             if (i > 0)
00098                                 out << ':';
00099                             // upper 4 BIT
00100                             tmp = _data[i] >> 4;
00101                             out << (char) (tmp < 10 ? '0' + tmp : 'A' + tmp - 10);
00102 
00103                             // lower 4 BIT
00104                             tmp = _data[i] & 0xF;
00105                             out << (char) (tmp < 10 ? '0' + tmp : 'A' + tmp - 10);
00106                         }
00107                     }
00108 
00109                     operator const uint8_t*() const {
00110                         return _data;
00111                     }
00112 
00113                 private:
00114                     uint8_t _data[size];
00115             }__attribute__((packed));
00116         } /* nl */
00117     } /* mw */
00118 } /* famouso */
00119 
00120 namespace logging {
00127     template< uint16_t size >
00128     inline ::logging::loggingReturnType &operator <<(::logging::loggingReturnType &out, const famouso::mw::nl::NetworkID<size> &id) {
00129         id.print(out);
00130         return out;
00131     }
00132 } // namespace logging
00133 #endif /* __NetworkID_hpp__ */