neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
binary_packet.hpp
Go to the documentation of this file.
1// binary_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 <vector>
41
42namespace neolib
43{
44 template <typename CharType>
45 class basic_binary_packet : public i_basic_packet<CharType>
46 {
48 // types
49 public:
52 typedef typename base_type::pointer pointer;
55 typedef std::vector<CharType> contents_type;
56 // construction
57 public:
59 iContents(aContents)
60 {
61 }
62 basic_binary_packet(const void* aPointer, size_type aLength) :
63 iContents(static_cast<const_pointer>(aPointer), static_cast<const_pointer>(aPointer) + aLength)
64 {
65 }
67 iContents(aOther.iContents)
68 {
69 }
71 {
72 if (this != &aOther)
73 iContents = aOther.iContents;
74 return *this;
75 }
76 // operations
77 public:
78 // from i_basic_packet
79 virtual const_pointer data() const
80 {
81 if (base_type::empty())
82 throw typename base_type::packet_empty();
83 return &iContents[0];
84 }
85 virtual pointer data()
86 {
87 if (base_type::empty())
88 throw typename base_type::packet_empty();
89 return &iContents[0];
90 }
91 virtual size_type length() const
92 {
93 return iContents.size();
94 }
95 virtual bool has_max_length() const
96 {
97 return false;
98 }
99 virtual size_type max_length() const
100 {
101 return iContents.max_size();
102 }
103 virtual void clear()
104 {
105 iContents.clear();
106 }
107 virtual bool take_some(const_pointer& aFirst, const_pointer aLast)
108 {
109 if (aFirst == aLast)
110 return false;
111 if (has_max_length() && length() + (aLast - aFirst) > max_length())
112 throw typename base_type::packet_too_big();
113 iContents.insert(iContents.end(), aFirst, aLast);
114 aFirst = aLast;
115 return true;
116 }
117 virtual clone_pointer clone() const
118 {
119 return clone_pointer(new basic_binary_packet(*this));
120 }
121 virtual void copy_from(const i_basic_packet<CharType>& aSource)
122 {
123 iContents.clear();
124 if (aSource.length() != 0)
125 iContents.assign(aSource.data(), aSource.data() + aSource.length());
126 }
127 // own
128 const contents_type& contents() const
129 {
130 return iContents;
131 }
133 {
134 return iContents;
135 }
136 // attributes
137 private:
138 contents_type iContents;
139 };
140
142}
base_type::size_type size_type
virtual clone_pointer clone() const
basic_binary_packet(const void *aPointer, size_type aLength)
virtual bool take_some(const_pointer &aFirst, const_pointer aLast)
virtual size_type length() const
virtual size_type max_length() const
basic_binary_packet(const basic_binary_packet &aOther)
basic_binary_packet(const contents_type &aContents=contents_type())
base_type::character_type character_type
virtual const_pointer data() const
virtual void copy_from(const i_basic_packet< CharType > &aSource)
const contents_type & contents() const
std::vector< CharType > contents_type
base_type::clone_pointer clone_pointer
base_type::const_pointer const_pointer
basic_binary_packet & operator=(const basic_binary_packet &aOther)
virtual bool has_max_length() const
character_type * pointer
Definition i_packet.hpp:51
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_binary_packet< char > binary_packet