AgentXcpp  Revision:0.1
Internals Documentation
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends
/home/tanjeff/projekte/agentxcpp/src/helper.hpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2011-2012 Tanjeff-Nicolai Moos <tanjeff@cccmz.de>
00003  *
00004  * This file is part of the agentXcpp library.
00005  *
00006  * AgentXcpp is free software: you can redistribute it and/or modify
00007  * it under the terms of the AgentXcpp library license, version 1, which 
00008  * consists of the GNU General Public License and some additional 
00009  * permissions.
00010  *
00011  * AgentXcpp is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * See the AgentXcpp library license in the LICENSE file of this package 
00017  * for more details.
00018  */
00019 
00020 #ifndef _HELPER_H_
00021 #define _HELPER_H_
00022 
00023 #include "types.hpp"
00024 
00025 inline uint32_t read32(data_t::const_iterator& pos, bool big_endian)
00026 {
00027     uint32_t value;
00028     if( big_endian )
00029     {
00030         value =  *pos++ << 24;
00031         value |= *pos++ << 16;
00032         value |= *pos++ << 8;
00033         value |= *pos++ << 0;
00034     }
00035     else
00036     {
00037         value =  *pos++ << 0;
00038         value |= *pos++ << 8;
00039         value |= *pos++ << 16;
00040         value |= *pos++ << 24;
00041     }
00042 
00043     return value;
00044 }
00045 
00046 /**
00047  * \brief Write a 32-bit value into a string
00048  *
00049  * \param serialized The string to which the value is appended.
00050  * 
00051  * \param value The value which is appended to the string.
00052  */
00053 inline void write32(data_t& serialized, uint32_t value)
00054 {
00055     // always big endian
00056     serialized.push_back(value >> 24 & 0xff);
00057     serialized.push_back(value >> 16 & 0xff);
00058     serialized.push_back(value >> 8 & 0xff);
00059     serialized.push_back(value >> 0 & 0xff);
00060 }
00061 
00062 inline uint16_t read16(data_t::const_iterator& pos, bool big_endian)
00063 {
00064     uint16_t value = 0;
00065     if( big_endian )
00066     {
00067         value |= *pos++ << 8;
00068         value |= *pos++ << 0;
00069     }
00070     else
00071     {
00072         value =  *pos++ << 0;
00073         value |= *pos++ << 8;
00074     }
00075 
00076     return value;
00077 }
00078 
00079 
00080 /**
00081  * \brief Write a 16-bit value into a string
00082  *
00083  * \param serialized The string to which the value is appended.
00084  * 
00085  * \param value The value which is appended to the string.
00086  */
00087 inline void write16(data_t& serialized, uint16_t value)
00088 {
00089     // always big endian
00090     serialized.push_back(value >> 8 & 0xff);
00091     serialized.push_back(value >> 0 & 0xff);
00092 }
00093 
00094 #endif