fastcgi++
A C++ FastCGI/Web API
block.cpp
Go to the documentation of this file.
1 
10 /*******************************************************************************
11 * Copyright (C) 2017 Eddie Carle [eddie@isatec.ca] *
12 * *
13 * This file is part of fastcgi++. *
14 * *
15 * fastcgi++ is free software: you can redistribute it and/or modify it under *
16 * the terms of the GNU Lesser General Public License as published by the Free *
17 * Software Foundation, either version 3 of the License, or (at your option) *
18 * any later version. *
19 * *
20 * fastcgi++ is distributed in the hope that it will be useful, but WITHOUT ANY *
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for *
23 * more details. *
24 * *
25 * You should have received a copy of the GNU Lesser General Public License *
26 * along with fastcgi++. If not, see <http://www.gnu.org/licenses/>. *
27 *******************************************************************************/
28 
29 #include "fastcgi++/block.hpp"
30 #include <algorithm>
31 
33 {
34  if(x != m_reserve)
35  {
36  std::unique_ptr<char[]> data(new char[x]);
37  size_t newSize = std::min(m_size, x);
38  std::copy(
39  m_data.get(),
40  m_data.get()+newSize,
41  data.get());
42 
43  m_reserve = x;
44  m_size = newSize;
45  m_data = std::move(data);
46 
47  }
48 }
49 
51  m_reserve(0),
52  m_size(0)
53 {}
54 
55 Fastcgipp::Block::Block(const size_t size_):
56  m_reserve(size_),
57  m_size(size_),
58  m_data(new char[size_])
59 {}
60 
61 Fastcgipp::Block::Block(const char* const data, const size_t size_):
62  m_reserve(size_),
63  m_size(size_),
64  m_data(new char[size_])
65 {
66  std::copy(data, data+size_, m_data.get());
67 }
68 
71  m_size(x.m_size),
72  m_data(std::move(x.m_data))
73 {
74  x.m_reserve = 0;
75  x.m_size = 0;
76 }
77 
79 {
80  m_reserve = x.m_reserve;
81  x.m_reserve = 0;
82  m_size = x.m_size;
83  x.m_size = 0;
84  m_data = std::move(x.m_data);
85  return *this;
86 }
87 
88 void Fastcgipp::Block::size(size_t x)
89 {
90  if(x > m_reserve)
91  {
92  m_size = m_reserve;
93  reserve(x);
94  }
95  m_size = x;
96 }
97 
99 {
100  m_reserve = 0;
101  m_size = 0;
102  m_data.reset();
103 }
104 
105 void Fastcgipp::Block::assign(const char* const data, const size_t size_)
106 {
107  if(size_ > m_reserve)
108  {
109  m_data.reset(new char[size_]);
110  m_reserve = size_;
111  }
112  m_size = size_;
113  std::copy(data, data+size_, m_data.get());
114 }
void assign(const char *const data, const size_t size_)
Assign a sequence a data to the block.
Definition: block.cpp:105
STL namespace.
size_t size() const
See the relevant data size.
Definition: block.hpp:94
size_t reserve() const
See the reserve size.
Definition: block.hpp:80
size_t m_reserve
Total bytes allocated.
Definition: block.hpp:48
size_t m_size
Size of relevant part of data allocation.
Definition: block.hpp:51
std::unique_ptr< char[]> m_data
Point to allocated data.
Definition: block.hpp:54
Declares the Block data structure.
void clear()
Deallocate memory and set size and reserve to zero.
Definition: block.cpp:98
Block & operator=(Block &&x)
Steal the data from another block.
Definition: block.cpp:78
Block()
Initialize an empty block.
Definition: block.cpp:50
Data structure to hold a block of raw data.
Definition: block.hpp:44