GpCoreTools/MessageClassId.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 **  This file is part of GpCoreTools.
00004 **
00005 **  This file may be distributed and/or modified under the terms of the
00006 **  GNU General Public License version 2 or 3 as published by the Free
00007 **  Software Foundation and appearing in the file LICENSE.GPL included
00008 **  in the packaging of this file.
00009 **
00010 **  This file is distributed in the hope that it will be useful, but WITHOUT
00011 **  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 **  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
00013 **  more details.
00014 **
00015 **  You should have received a copy of the GNU General Public License
00016 **  along with this program. If not, see <http://www.gnu.org/licenses/>.
00017 **
00018 **  See http://www.geopsy.org for more information.
00019 **
00020 **  Created: 2011-06-29
00021 **  Authors:
00022 **    Marc Wathelet (ISTerre, Grenoble, France)
00023 **
00024 ***************************************************************************/
00025 
00026 #ifndef MESSAGECLASSID_H
00027 #define MESSAGECLASSID_H
00028 
00029 #include "MessageClassIdHeader.h"
00030 
00031 namespace GpCoreTools {
00032 
00033 #pragma pack(1)     /* set alignment to 1 byte boundary */
00034 
00035   template <class Payload> class GPCORETOOLS_EXPORT MessageClassId
00036   {
00037   public:
00038     inline MessageClassId(unsigned char msgClass, unsigned char msgId)
00039       : _head(msgClass, msgId, sizeof(Payload)), _payload(), _tail() {}
00040     inline MessageClassId(unsigned char msgClass, unsigned char msgId, const Payload& p)
00041       : _head(msgClass, msgId, sizeof(Payload)), _payload(p), _tail() {}
00042 
00043     inline bool isLittleEndianValid() const;
00044     inline bool isBigEndianValid() const;
00045 
00046     inline void littleEndianValues();
00047     inline void bigEndianValues();
00048 
00049     const MessageClassIdHeader& header() const {return _head;}
00050     Payload& payload() {return _payload;}
00051     const Payload& payload() const {return _payload;}
00052     const FletcherChecksum& checksum() const {return _tail;}
00053 
00054     const char * buffer() const {return reinterpret_cast<const char *>(this);}
00055     const char * payloadBuffer() const {return reinterpret_cast<const char *>(&_payload);}
00056   private:
00057     MessageClassIdHeader _head;
00058     Payload _payload;
00059     FletcherChecksum _tail;
00060   };
00061 
00062   template <class Payload>
00063   inline void MessageClassId<Payload>::littleEndianValues()
00064   {
00065     _head.littleEndianValues();
00066     _payload.littleEndianValues();
00067     _tail.add(reinterpret_cast<const char *>(_head.header()), MessageClassIdHeader::HeaderSize);
00068     _tail.add(reinterpret_cast<const char *>(&_payload), sizeof(Payload));
00069   }
00070 
00071   template <class Payload>
00072   inline void MessageClassId<Payload>::bigEndianValues()
00073   {
00074     _head.bigEndianValues();
00075     _payload.bigEndianValues();
00076     _tail.add(reinterpret_cast<const char *>(_head.header()), MessageClassIdHeader::HeaderSize);
00077     _tail.add(reinterpret_cast<const char *>(&_payload), sizeof(Payload));
00078   }
00079 
00080   template <class Payload>
00081   inline bool MessageClassId<Payload>::isLittleEndianValid() const
00082   {
00083     return _head.isValid() &&
00084            _head.lengthLittleEndian()==sizeof(Payload) &&
00085            _head.isValid(_tail, payloadBuffer(), sizeof(Payload));
00086   }
00087 
00088   template <class Payload>
00089   inline bool MessageClassId<Payload>::isBigEndianValid() const
00090   {
00091     return _head.isValid() &&
00092            _head.lengthBigEndian()==sizeof(Payload) &&
00093            _head.isValid(_tail, payloadBuffer(), sizeof(Payload));
00094   }
00095 
00096 #pragma pack()   // restore original alignment
00097 
00098 } // namespace GpCoreTools
00099 
00100 #endif // MESSAGECLASSID_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines