The Battle for Wesnoth  1.15.3+dev
data.hpp
Go to the documentation of this file.
1 //
2 // M A R I A D B + +
3 //
4 // Copyright Sylvain Rochette Langlois 2013,
5 // Frantisek Boranek 2015,
6 // The ViaDuck Project 2016 - 2018.
7 // Distributed under the Boost Software License, Version 1.0.
8 // (See accompanying file LICENSE or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 
11 #ifndef _MARIADB_DATA_HPP_
12 #define _MARIADB_DATA_HPP_
13 
14 #include <string.h>
15 #include <mariadb++/types.hpp>
16 #include <iostream>
17 
18 namespace mariadb {
19 template <typename Type>
20 class data {
21  public:
22  typedef Type char_type;
23 
24  //
25  // Constructor
26  //
27  data() : m_count(0), m_size(0), m_data(0) {}
28 
29  data(u32 count) : m_count(0), m_size(0), m_data(0) { create(count); }
30 
31  data(const Type* data, size_t count) : m_count(0), m_size(0), m_data(0) {
32  create(data, static_cast<u32>(count));
33  }
34 
35  //
36  // Destructor
37  //
38  virtual ~data() { destroy(); }
39 
40  //
41  // Create the data
42  //
43  bool create(u32 count) {
44  if (m_data) destroy();
45 
46  m_data = new Type[count];
47 
48  if (m_data == 0) return false;
49 
50  m_count = count;
51  m_size = sizeof(Type) * count;
52  m_position = 0;
53 
54  return true;
55  }
56 
57  bool create(const Type* data, u32 count) {
58  if (create(count)) {
59  memcpy(m_data, data, m_size);
60  return true;
61  }
62 
63  return false;
64  }
65 
66  bool resize(u32 count) {
67  if (count > m_count) {
68  Type* data = new Type[count];
69 
70  if (data == 0) return false;
71 
72  memcpy(data, m_data, m_size);
73  delete[] m_data;
74  m_data = data;
75  }
76 
77  m_count = count;
78  m_size = sizeof(Type) * count;
79 
80  return true;
81  }
82 
83  //
84  // Destroy
85  //
86  void destroy() {
87  if (m_data) delete[] m_data;
88 
89  m_data = NULL;
90  m_size = 0;
91  m_count = 0;
92  m_position = 0;
93  }
94 
95  //
96  // Convert data to a valid string
97  //
98  std::string string() const {
100  if (m_size) str.append(reinterpret_cast<const char*>(m_data), m_size);
101  return str;
102  }
103 
104  //
105  // Get data / size
106  //
107  inline u32 size() const { return m_size; }
108  inline Type* get() const { return m_data; }
109 
110  //
111  // Operator to access the base directly
112  //
113  inline operator Type*() { return m_data; }
114  inline operator Type*() const { return m_data; }
115 
116  //
117  // Stream methods
118  //
119  std::streamsize read(char_type* buffer, std::streamsize size) {
120  const std::streamsize amount = static_cast<std::streamsize>(m_size - m_position);
121  const std::streamsize result = std::min(size, amount);
122 
123  if (result != 0) {
124  std::copy(m_data + m_position, m_data + m_position + result, buffer);
125  m_position += result;
126  return result;
127  }
128 
129  return size ? -1 : 0;
130  }
131 
132  std::streamsize write(char_type* buffer, std::streamsize size) {
133  const std::streamsize amount = static_cast<std::streamsize>(m_size - m_position);
134  const std::streamsize result = std::min(size, amount);
135 
136  if (result != 0) {
137  std::copy(buffer, buffer + result, m_data + m_position);
138  m_position += result;
139  return result;
140  }
141 
142  return size ? -1 : 0;
143  }
144 
145  std::streampos seek(std::streampos offset, std::ios_base::seekdir seekdir) {
146  std::streampos pos;
147 
148  if (seekdir == std::ios_base::beg)
149  pos = offset;
150  else if (seekdir == std::ios_base::cur)
151  pos = m_position + offset;
152  else if (seekdir == std::ios_base::end)
153  pos = m_size + offset;
154  else
155  throw std::ios_base::failure("Bad seek direction");
156 
157  if (pos < 0 || pos > m_size) throw std::ios_base::failure("Bad seek offset");
158 
159  m_position = static_cast<u32>(pos);
160  return pos;
161  }
162 
163  protected:
167  Type* m_data;
168 };
169 
170 typedef std::shared_ptr< ::mariadb::data<char> > data_ref;
171 typedef std::basic_iostream< ::mariadb::data<char> > data_stream;
172 }
173 
174 #endif
data(const Type *data, size_t count)
Definition: data.hpp:31
std::basic_iostream< ::mariadb::data< char > > data_stream
Definition: data.hpp:171
std::streamsize write(char_type *buffer, std::streamsize size)
Definition: data.hpp:132
bool create(u32 count)
Definition: data.hpp:43
u32 m_position
Definition: data.hpp:166
std::string str
Definition: statement.cpp:110
u32 size() const
Definition: data.hpp:107
bool resize(u32 count)
Definition: data.hpp:66
void destroy()
Definition: data.hpp:86
std::streamsize read(char_type *buffer, std::streamsize size)
Definition: data.hpp:119
virtual ~data()
Definition: data.hpp:38
std::shared_ptr< ::mariadb::data< char > > data_ref
Definition: data.hpp:170
u32 m_count
Definition: data.hpp:164
u32 m_size
Definition: data.hpp:165
Type * m_data
Definition: data.hpp:167
std::streampos seek(std::streampos offset, std::ios_base::seekdir seekdir)
Definition: data.hpp:145
data(u32 count)
Definition: data.hpp:29
bool create(const Type *data, u32 count)
Definition: data.hpp:57
unsigned int u32
Definition: types.hpp:22
std::string string() const
Definition: data.hpp:98
Type char_type
Definition: data.hpp:22