sprint 1-alpha
|
00001 /****************************************************************************** 00002 * sprint::static_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_SBUFFER_H 00027 #define _SPRINT_SBUFFER_H 00028 00029 namespace sprint { 00030 00036 template<int t_capacity, class T = char> 00037 class static_buffer { 00038 T m_str[t_capacity]; 00039 unsigned int m_cur_pos; 00040 public: 00041 static_buffer() : m_cur_pos(0) { m_str[0] = 0; } 00042 00043 static_buffer(const T *a) : m_cur_pos(0) 00044 { 00045 insert(a); 00046 } 00047 static_buffer(const T *a, const T *b) : m_cur_pos(0) 00048 { 00049 insert_(a); 00050 insert(b); 00051 } 00052 static_buffer(const T *a, const T *b, const T *c) : m_cur_pos(0) 00053 { 00054 insert_(a); 00055 insert_(b); 00056 insert(c); 00057 } 00058 static_buffer(const T *a, const T *b, const T *c, const T *d) : m_cur_pos(0) 00059 { 00060 insert_(a); 00061 insert_(b); 00062 insert_(c); 00063 insert(d); 00064 } 00065 static_buffer(const T *a, const T *b, const T *c, const T *d, const T *e) : m_cur_pos(0) 00066 { 00067 insert_(a); 00068 insert_(b); 00069 insert_(c); 00070 insert_(d); 00071 insert(e); 00072 } 00073 00075 void insert_(const T *a) 00076 { 00077 // TODO: check size 00078 int len = strlen(a); 00079 memcpy(&m_str[m_cur_pos], a, len * sizeof(T)); 00080 m_cur_pos+=len; 00081 } 00082 00084 void insert(const T *a) 00085 { 00086 // TODO: check size 00087 int len = strlen(a); 00088 memcpy(&m_str[m_cur_pos], a, (len + 1) * sizeof(T) ); 00089 m_cur_pos+=len; 00090 } 00091 00093 static_buffer<t_capacity> & operator << (const T *str) 00094 { 00095 insert_(str); 00096 return *this; 00097 } 00098 00100 inline const char *c_str() const { return &m_str[0]; } 00101 00103 inline char *str() { return &m_str[0]; } 00104 00106 inline const char *data() const { return &m_str[0]; } 00108 inline int lenght() const { return m_cur_pos; } 00109 00111 inline unsigned int size() const { return m_cur_pos; } 00112 00114 inline unsigned int capacity() const { return t_capacity; } 00115 }; 00116 00117 } 00118 00119 #endif