message-buffer.cpp

Go to the documentation of this file.
00001 /*
00002  * message-buffer.cpp
00003  *
00004  * Copyright (C) 2007  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  *
00031  * Implementation of a simple message buffer object.  See message-buffer.h
00032  */
00033 
00034 // includes --------------------------------------------------------------------
00035 #include "message-buffer.h"
00036 
00037 
00038 namespace netlib {
00039 
00040 typedef std::vector<char> buffer_t;
00041 
00042 
00043 // interface destructor implementation
00044 MessageBuffer::~MessageBuffer(void) throw() { }
00045 
00046 
00047 ////////////////////////////////////////////////////////////////////////////////
00048 //
00049 //      private helper methods
00050 //
00051 ////////////////////////////////////////////////////////////////////////////////
00052 
00053 class Buffer : public MessageBuffer {
00054 public:
00055         Buffer(void) throw() : m_closed(false) { }
00056         ~Buffer(void) throw() { }
00057 
00058         // public class methods ------------------------------------------------
00059         void initialize(void) throw() { }
00060 
00061         // netlib::MessageBuffer class interface methods -----------------------
00062         long getBytes(void) const throw();
00063         const char * getData(void) const throw();
00064         bool isClosed(void) const throw() { return m_closed; }
00065         void reserve(IN long bytes);
00066         void appendData(IN const char * data, IN long bytes);
00067         void appendToken(IN const char * token);
00068         void close(void);
00069 
00070 private:
00071         // private member data -------------------------------------------------
00072         buffer_t                m_buffer;
00073         bool                    m_closed;
00074 };
00075 
00076 
00077 
00078 long
00079 Buffer::getBytes
00080 (
00081 void
00082 )
00083 const
00084 throw()
00085 {
00086         ASSERT(m_closed, "read operation not allowed: message buffer not closed!");
00087         long bytes = m_buffer.size();
00088         ASSERT(bytes > 0, "should be at least 1, even for empty message!");
00089         return bytes - 1;
00090 }
00091 
00092 
00093 
00094 const char *
00095 Buffer::getData
00096 (
00097 void
00098 )
00099 const
00100 throw()
00101 {
00102         ASSERT(m_closed, "read operation not allowed: message buffer not closed!");
00103         return m_buffer.data();
00104 }
00105 
00106 
00107 
00108 void
00109 Buffer::reserve
00110 (
00111 IN long bytes
00112 )
00113 {
00114         ASSERT(!m_closed, "write operation not allowed: message buffer is closed!");
00115         m_buffer.reserve(bytes + 1);    // add one for final null-termination
00116 }
00117 
00118 
00119 
00120 void
00121 Buffer::appendData
00122 (
00123 IN const char * data,
00124 IN long bytes
00125 )
00126 {
00127         // timer itself impacts timing!
00128         // perf::Timer timer("Buffer::appendData");
00129         ASSERT(!m_closed, "write operation not allowed: message buffer is closed!");
00130         ASSERT(data, "null");
00131         ASSERT(bytes > 0, "bad byte count: %ld", bytes);
00132 
00133         long curr = m_buffer.size();
00134         long required = curr + bytes;
00135         m_buffer.resize(required);
00136         char * dst = m_buffer.data() + curr;
00137         memcpy(dst, data, bytes);
00138 }
00139 
00140 
00141 
00142 void
00143 Buffer::close
00144 (
00145 void
00146 )
00147 {
00148         ASSERT(!m_closed, "message buffer is already closed!");
00149 
00150         m_closed = true;
00151         m_buffer.push_back(0);  // force a null termination
00152 }
00153 
00154 
00155 
00156 void
00157 Buffer::appendToken
00158 (
00159 IN const char * token
00160 )
00161 {
00162         ASSERT(token, "null");
00163         int bytes = strlen(token);
00164         this->appendData(token, bytes);
00165 }
00166 
00167 
00168 
00169 ////////////////////////////////////////////////////////////////////////////////
00170 //
00171 //      public API
00172 //
00173 ////////////////////////////////////////////////////////////////////////////////
00174 
00175 smart_ptr<MessageBuffer>
00176 MessageBuffer::create
00177 (
00178 void
00179 )
00180 {
00181         smart_ptr<Buffer> local = new Buffer;
00182         ASSERT(local, "out of memory");
00183 
00184         local->initialize();
00185 
00186         return local;
00187 }
00188 
00189 
00190 
00191 };      // netlib namespace
00192