Aegis  4.25.D505
common/nstring/accumulator.h
Go to the documentation of this file.
00001 //
00002 //      aegis - project change supervisor
00003 //      Copyright (C) 2004-2006, 2008 Peter Miller
00004 //
00005 //      This program is free software; you can redistribute it and/or modify
00006 //      it under the terms of the GNU General Public License as published by
00007 //      the Free Software Foundation; either version 3 of the License, or
00008 //      (at your option) any later version.
00009 //
00010 //      This program is distributed in the hope that it will be useful,
00011 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 //      GNU General Public License for more details.
00014 //
00015 //      You should have received a copy of the GNU General Public License
00016 //      along with this program. If not, see
00017 //      <http://www.gnu.org/licenses/>.
00018 //
00019 
00020 #ifndef COMMON_NSTRING_ACCUMULATOR_H
00021 #define COMMON_NSTRING_ACCUMULATOR_H
00022 
00023 #include <common/nstring.h>
00024 
00034 class nstring_accumulator
00035 {
00036 public:
00041     ~nstring_accumulator();
00042 
00046     nstring_accumulator();
00047 
00051     nstring_accumulator(const nstring_accumulator &);
00052 
00056     nstring_accumulator &operator=(const nstring_accumulator &);
00057 
00062     nstring mkstr() const;
00063 
00071     void
00072     push_back(char c)
00073     {
00074         //
00075         // The nstring_accumulator::push_back(char) method shows up
00076         // in the profiles as occupying 10% of the time it takes to
00077         // parse the database files.  By making it an inline, things go
00078         // measurably faster.
00079         //
00080         if (length < maximum)
00081             buffer[length++] = c;
00082         else
00083             overflow(c);
00084     }
00085 
00095     void push_back(const char *data, size_t len);
00096 
00104     void push_back(const char *data);
00105 
00113     void push_back(const nstring_accumulator &data);
00114 
00122     void push_back(const nstring &data);
00123 
00128     void clear() { length = 0; }
00129 
00134     size_t size() const { return length; }
00135 
00140     bool empty() const { return (length == 0); }
00141 
00146     void pop_back() { if (length) --length; }
00147 
00152     char back() { return (length ? buffer[length - 1] : 0); }
00153 
00164     const char *get_data() const { return (buffer ? buffer : ""); }
00165 
00174     char operator[](size_t n) { return buffer[n]; }
00175 
00176 private:
00185     void overflow(char c);
00186 
00191     size_t length;
00192 
00197     size_t maximum;
00198 
00203     char *buffer;
00204 };
00205 
00206 
00208 #endif // COMMON_NSTRING_ACCUMULATOR_H