net: minor improvements in tcp client sockets
[quarnos.git] / libs / buffer.h
blob2893bd9c5fd1e434042c0a07757a8b4fe5ab5083
1 /* Quarn OS
3 * Buffer
5 * Copyright (C) 2008-2009 Pawel Dziepak
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #ifndef _BUFFER_H_
24 #define _BUFFER_H_
26 #include "pointer.h"
27 #include "math.h"
29 void *memcpy(void *,const void*,int);
31 class buffer {
32 private:
33 char *address;
34 unsigned int size;
36 public:
37 static const buffer empty;
39 buffer() : address((char*)0), size(0) { }
41 buffer(unsigned int bytes) : address(new char[bytes]), size(bytes) { }
43 buffer(void *addr, unsigned int siz) : address((char*)addr), size(siz) { }
45 buffer(const buffer &x) : address(x.address), size(x.size) { }
47 buffer &operator=(const buffer &x) {
48 address = x.address;
49 size = x.size;
51 return *this;
54 operator void*() {
55 return address;
58 char &operator[](int i) {
59 if (i >= 0 && i < size)
60 return address[i];
63 const char &operator[](int i) const {
64 if (i >= 0 && i < size)
65 return address[i];
68 buffer &operator+=(const buffer &x) {
69 if (!x.size)
70 return *this;
72 if (!size) {
73 copy_data(x);
74 return *this;
77 char *buf = new char[x.size + size];
78 memcpy(buf, address, size);
79 memcpy((void*)((unsigned int)buf + size), x.address, x.size);
81 delete address;
82 address = buf;
83 size += x.size;
85 return *this;
88 template<typename T>
89 p<T> cast() const {
90 if (size >= sizeof(T))
91 return p<T>(reinterpret_cast<T*>(address));
94 int get_size() const {
95 return size;
98 void *get_address() const {
99 return address;
102 buffer cut_first(unsigned int cut_size) const {
103 if (cut_size >= size)
104 return empty;
106 return buffer(address + cut_size, size - cut_size);
109 buffer get_next(unsigned int bytes) const {
110 bytes = math::min(bytes, size);
112 if (!bytes)
113 return empty;
115 return buffer(address, bytes);
118 buffer copy() const {
119 buffer buf(new char[size], size);
120 memcpy(buf.address, address, size);
121 return buf;
124 buffer copy_data(const buffer &buf) {
125 memcpy(address, buf.address, math::min(size, buf.size));
128 template<typename T>
129 static buffer to_mem(p<T> addr) {
130 return buffer((void*)addr, sizeof(T));
133 template<typename T>
134 static buffer to_mem(T &addr) {
135 return buffer((void*)&addr, sizeof(T));
139 #endif