textwolf  0.2
staticbuffer.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Patrick P. Frey
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
10 
11 #ifndef __TEXTWOLF_STATIC_BUFFER_HPP__
12 #define __TEXTWOLF_STATIC_BUFFER_HPP__
13 #include "textwolf/exception.hpp"
14 #include <cstddef>
15 #include <cstring>
16 #include <cstdlib>
17 #include <stdexcept>
18 #include <new>
19 
20 namespace textwolf {
21 
25 {
26 public:
28  explicit StaticBuffer( std::size_t n)
29  :m_pos(0),m_size(n),m_ar(0),m_allocated(true),m_overflow(false)
30  {
31  m_ar = (char*)std::calloc( n, sizeof(char));
32  if (!m_ar) throw std::bad_alloc();
33  }
34 
36  StaticBuffer( char* p, std::size_t n, std::size_t i=0)
37  :m_pos(i)
38  ,m_size(n)
39  ,m_ar(p)
40  ,m_allocated(false)
41  ,m_overflow(false) {}
42 
45  :m_pos(o.m_pos)
46  ,m_size(o.m_size)
47  ,m_ar(0)
48  ,m_allocated(o.m_allocated)
49  ,m_overflow(o.m_overflow)
50  {
51  m_ar = (char*)std::malloc( m_size * sizeof(char));
52  if (!m_ar) throw std::bad_alloc();
53  std::memcpy( m_ar, o.m_ar, m_size);
54  }
55 
58  {
59  if (m_allocated && m_ar) std::free(m_ar);
60  }
61 
63  void clear()
64  {
65  m_pos = 0;
66  m_overflow = false;
67  }
68 
71  void push_back( char ch)
72  {
73  if (m_pos < m_size)
74  {
75  m_ar[m_pos++] = ch;
76  }
77  else
78  {
79  m_overflow = true;
80  }
81  }
82 
86  void append( const char* cc, std::size_t ccsize)
87  {
88  if (m_pos+ccsize > m_size)
89  {
90  m_overflow = true;
91  ccsize = m_size - m_pos;
92  }
93  std::memcpy( m_ar+m_pos, cc, ccsize);
94  m_pos += ccsize;
95  }
96 
99  std::size_t size() const {return m_pos;}
100 
103  const char* ptr() const {return m_ar;}
104 
108  void resize( std::size_t n, char c=0)
109  {
110  if (m_pos>n)
111  {
112  m_pos=n;
113  }
114  else
115  {
116  if (m_size<n) n=m_size;
117  while (n>m_pos) push_back(c);
118  }
119  }
120 
124  char operator []( std::size_t ii) const
125  {
126  if (ii > m_pos) throw exception( DimOutOfRange);
127  return m_ar[ii];
128  }
129 
133  char& at( std::size_t ii) const
134  {
135  if (ii > m_pos) throw exception( DimOutOfRange);
136  return m_ar[ii];
137  }
138 
141  bool overflow() const {return m_overflow;}
142 private:
143  StaticBuffer(){}
144 private:
145  std::size_t m_pos;
146  std::size_t m_size;
147  char* m_ar;
148  bool m_allocated;
149  bool m_overflow;
150 };
151 
152 }//namespace
153 #endif
StaticBuffer(std::size_t n)
Constructor.
Definition: staticbuffer.hpp:28
std::size_t size() const
Return the number of characters in the buffer.
Definition: staticbuffer.hpp:99
char operator[](std::size_t ii) const
random access of element
Definition: staticbuffer.hpp:124
Base class for structures that can throw exceptions for non recoverable errors.
Definition: exception.hpp:20
~StaticBuffer()
Destructor.
Definition: staticbuffer.hpp:57
StaticBuffer(const StaticBuffer &o)
Copy constructor.
Definition: staticbuffer.hpp:44
const char * ptr() const
Return the buffer content as 0-terminated string.
Definition: staticbuffer.hpp:103
Simple back insertion sequence for storing the outputs of textwolf in a contant size buffer...
Definition: staticbuffer.hpp:24
textwolf exception class
Definition: exception.hpp:48
StaticBuffer(char *p, std::size_t n, std::size_t i=0)
Constructor.
Definition: staticbuffer.hpp:36
void resize(std::size_t n, char c=0)
Shrinks the size of the buffer or expands it with c.
Definition: staticbuffer.hpp:108
void clear()
Clear the buffer content.
Definition: staticbuffer.hpp:63
bool overflow() const
check for array bounds write
Definition: staticbuffer.hpp:141
memory reserved for statically allocated table or memory block is too small. Increase the size of mem...
Definition: exception.hpp:27
Definition of exceptions with containing error codes thrown by textwolf.
char & at(std::size_t ii) const
random access of element reference
Definition: staticbuffer.hpp:133
void append(const char *cc, std::size_t ccsize)
Append an array of characters.
Definition: staticbuffer.hpp:86
void push_back(char ch)
Append one character.
Definition: staticbuffer.hpp:71