sprint 1-alpha
|
00001 /****************************************************************************** 00002 * sprint::buffer 00003 * 00004 * Copyright (C) 2005-2011 Paolo Medici <www.pmx.it> 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * This library 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 GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with this library; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 * 00020 *******************************************************************************/ 00021 00026 #ifndef _SPRINT_BUFFER_H 00027 #define _SPRINT_BUFFER_H 00028 00029 namespace sprint { 00030 00032 class const_buffer { 00033 const char *m_ptr; 00034 int m_size; 00035 public: 00036 const_buffer(const char*ptr, int size) : m_ptr(ptr), m_size(size) { } 00037 00038 inline const char *data() const { return m_ptr; } 00039 inline int lenght() const { return m_size; } 00040 00041 }; 00042 00044 struct mutable_buffer { 00045 char *m_ptr; 00046 int m_size; 00047 00048 public: 00049 mutable_buffer(char *ptr, int size) : m_ptr(ptr), m_size(size) { } 00050 00052 inline operator const_buffer() const { return const_buffer(m_ptr, m_size); } 00053 00054 inline char *data() const { return m_ptr; } 00055 inline int lenght() const { return m_size; } 00056 }; 00057 00059 template<class T> 00060 inline mutable_buffer buffer(T *ptr, int size) { return mutable_buffer(reinterpret_cast<char*>(ptr), size * sizeof(T)); } 00061 00063 template<class T> 00064 inline mutable_buffer buffer(T * ptr) { return mutable_buffer(reinterpret_cast<char*>(ptr), sizeof(T)); } 00065 00067 template<class T> 00068 inline const_buffer buffer(const T *ptr, int size) { return const_buffer(reinterpret_cast<const char*>(ptr), size * sizeof(T)); } 00069 00071 template<class T> 00072 inline const_buffer buffer(const T * ptr) { return const_buffer(reinterpret_cast<const char*>(ptr), sizeof(T)); } 00073 00075 inline const_buffer buffer(const std::string & s) { return const_buffer(reinterpret_cast<const char*>(s.data()), s.length()); } 00076 00077 } // namespace sprint 00078 00079 #endif