neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
string_packet.hpp
Go to the documentation of this file.
1// string_packet.hpp
2/*
3 * Copyright (c) 2007 Leigh Johnston.
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * * Neither the name of Leigh Johnston nor the names of any
19 * other contributors to this software may be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*/
35
36#pragma once
37
38#include <neolib/neolib.hpp>
39#include <string>
41
42namespace neolib
43{
44 template <typename CharType>
45 class basic_string_packet : public i_basic_packet<CharType>
46 {
48 // constants
49 public:
50 static constexpr CharType CHAR_CR = static_cast<CharType>('\r');
51 static constexpr CharType CHAR_LF = static_cast<CharType>('\n');
52 // types
53 public:
56 typedef typename base_type::pointer pointer;
59 typedef std::basic_string<CharType> contents_type;
60 // construction
61 public:
63 iContents(aContents)
64 {
65 }
66 basic_string_packet(const character_type* aPointer, size_type aLength) :
67 iContents(aPointer, aLength)
68 {
69 }
71 iContents(aOther.iContents)
72 {
73 }
75 {
76 if (this != &aOther)
77 iContents = aOther.iContents;
78 return *this;
79 }
80 // operations
81 public:
82 // from i_basic_packet
83 virtual const_pointer data() const
84 {
85 if (base_type::empty())
86 throw typename base_type::packet_empty();
87 return &iContents[0];
88 }
89 virtual pointer data()
90 {
91 if (base_type::empty())
92 throw typename base_type::packet_empty();
93 return &iContents[0];
94 }
95 virtual size_type length() const
96 {
97 return iContents.size();
98 }
99 virtual bool has_max_length() const
100 {
101 return false;
102 }
103 virtual size_type max_length() const
104 {
105 return iContents.max_size();
106 }
107 virtual void clear()
108 {
109 iContents.clear();
110 }
111 virtual bool take_some(const_pointer& aFirst, const_pointer aLast)
112 {
113 if (aFirst == aLast)
114 return false;
115 while (aFirst != aLast && is_delimiter(*aFirst))
116 ++aFirst;
117 const_pointer start = aFirst;
118 while (aFirst != aLast && !is_delimiter(*aFirst))
119 ++aFirst;
120 const_pointer end = aFirst;
121 if (has_max_length() && length() + (end - start) > max_length())
122 throw typename base_type::packet_too_big();
123 iContents.insert(iContents.end(), start, end);
124 while (aFirst != aLast && !is_terminating_delimiter(*aFirst))
125 ++aFirst;
126 if (aFirst != aLast)
127 ++aFirst;
128 return end != aLast || (start == end && !iContents.empty());
129 }
130 virtual clone_pointer clone() const
131 {
132 return clone_pointer(new basic_string_packet(*this));
133 }
134 virtual void copy_from(const i_basic_packet<CharType>& aSource)
135 {
136 iContents.clear();
137 if (aSource.length() != 0)
138 iContents.assign(aSource.data(), aSource.length());
139 }
140 // own
141 const contents_type& contents() const
142 {
143 return iContents;
144 }
146 {
147 return iContents;
148 }
149 // implementation
150 private:
151 virtual bool has_delimiters() const
152 {
153 return true;
154 }
155 virtual bool is_delimiter(character_type aCharacter) const
156 {
157 return has_delimiters() && (aCharacter == CHAR_CR || aCharacter == CHAR_LF);
158 }
159 virtual bool is_terminating_delimiter(character_type aCharacter) const
160 {
161 return has_delimiters() && aCharacter == CHAR_LF;
162 }
163 // attributes
164 private:
165 contents_type iContents;
166 };
167
169}
virtual size_type length() const
base_type::size_type size_type
virtual clone_pointer clone() const
base_type::clone_pointer clone_pointer
base_type::const_pointer const_pointer
const contents_type & contents() const
virtual bool take_some(const_pointer &aFirst, const_pointer aLast)
virtual size_type max_length() const
basic_string_packet & operator=(const basic_string_packet &aOther)
std::basic_string< CharType > contents_type
static constexpr CharType CHAR_LF
static constexpr CharType CHAR_CR
base_type::character_type character_type
virtual const_pointer data() const
basic_string_packet(const basic_string_packet &aOther)
virtual void copy_from(const i_basic_packet< CharType > &aSource)
basic_string_packet(const character_type *aPointer, size_type aLength)
basic_string_packet(const contents_type &aContents=contents_type())
virtual bool has_max_length() const
character_type * pointer
Definition i_packet.hpp:51
const_iterator end() const
Definition i_packet.hpp:73
const character_type * const_pointer
Definition i_packet.hpp:50
virtual const_pointer data() const =0
std::size_t size_type
Definition i_packet.hpp:52
std::unique_ptr< i_basic_packet > clone_pointer
Definition i_packet.hpp:55
virtual size_type length() const =0
basic_string_packet< char > string_packet