jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
byte_buffer.hpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2012 Preferred Networks and Nippon Telegraph and Telephone Corporation.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License version 2.1 as published by the Free Software Foundation.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 
17 #ifndef JUBATUS_CORE_COMMON_BYTE_BUFFER_HPP_
18 #define JUBATUS_CORE_COMMON_BYTE_BUFFER_HPP_
19 
20 #include <stdint.h>
21 #include <vector>
22 #include <cstring>
23 #include <msgpack.hpp>
24 #include "jubatus/util/lang/shared_ptr.h"
25 
26 namespace jubatus {
27 namespace core {
28 namespace common {
29 
30 class byte_buffer {
31  public:
33  }
34 
35  explicit byte_buffer(size_t size)
36  : buf_(new std::vector<char>(size)) {
37  }
38 
39  byte_buffer(const void* ptr, size_t size) {
40  const char* const first = static_cast<const char*>(ptr);
41  buf_.reset(new std::vector<char>(first, first+size));
42  }
43 
44  // following member functions are implicily defined:
45  // byte_buffer(const byte_buffer& b) = default;
46  // byte_buffer& operator=(const byte_buffer& b) = default;
47  // ~byte_buffer() = default;
48 
49  void swap(byte_buffer& other) {
50  this->buf_.swap(other.buf_);
51  // jubatus::util::lang::shared_ptr provides no non-member swap;
52  // `swap(this->buf_, other.buf_);' may be inefficient.
53  // when jubatus::util::lang::shared_ptr provide non-member swap,
54  // this function should be rewritten with it
55  // because if new data members are added to
56  // jubatus::util::lang::shared_ptr, member function swap
57  // (currently derived from base) may cause object slicing.
58  }
59 
60  void assign(const void* ptr, size_t size) {
61  if (buf_.unique()) {
62  const char* const first = static_cast<const char*>(ptr);
63  buf_->assign(first, first + size);
64  } else {
65  byte_buffer(ptr, size).swap(*this);
66  }
67  }
68 
69  const char* ptr() const {
70  if (buf_ && !buf_->empty()) {
71  return &(*buf_)[0];
72  // `buf_->data()' is much better (C++11 feature)
73  } else {
74  return NULL;
75  }
76  }
77 
78  char* ptr() {
79  return const_cast<char*>(
80  const_cast<const byte_buffer*>(this)->ptr());
81  }
82 
83  size_t size() const {
84  if (buf_) {
85  return buf_->size();
86  } else {
87  return 0;
88  }
89  }
90 
91  private:
92  jubatus::util::lang::shared_ptr<std::vector<char> > buf_;
93 };
94 
95 inline void swap(byte_buffer& one, byte_buffer& another) { // NOLINT
96  one.swap(another);
97 }
98 
99 } // namespace common
100 } // namespace core
101 } // namespace jubatus
102 
103 namespace msgpack {
104 
106  object o,
108  if (o.type != type::RAW) {
109  throw type_error();
110  }
111 
112  b.assign(o.via.raw.ptr, o.via.raw.size);
113  return b;
114 }
115 
116 template<typename Stream>
118  packer<Stream>& o,
120  o.pack_raw(b.size());
121  o.pack_raw_body(b.ptr(), b.size());
122  return o;
123 }
124 
125 inline void operator<<(
126  object::with_zone& o,
128  o.type = type::RAW;
129  char* ptr = static_cast<char*>(o.zone->malloc(b.size()));
130  o.via.raw.ptr = ptr;
131  o.via.raw.size = static_cast<uint32_t>(b.size());
132  std::memcpy(ptr, b.ptr(), b.size());
133 }
134 
135 inline void operator<<(
136  object& o,
138  o.type = type::RAW;
139  o.via.raw.ptr = b.ptr();
140  o.via.raw.size = static_cast<uint32_t>(b.size());
141 }
142 
143 } // namespace msgpack
144 
145 #endif // JUBATUS_CORE_COMMON_BYTE_BUFFER_HPP_
void swap(byte_buffer &one, byte_buffer &another)
Definition: byte_buffer.hpp:95
packer< Stream > & operator<<(packer< Stream > &o, const jubatus::core::common::byte_buffer &b)
byte_buffer(const void *ptr, size_t size)
Definition: byte_buffer.hpp:39
void swap(byte_buffer &other)
Definition: byte_buffer.hpp:49
void assign(const void *ptr, size_t size)
Definition: byte_buffer.hpp:60
jubatus::core::common::byte_buffer & operator>>(object o, jubatus::core::common::byte_buffer &b)
jubatus::util::lang::shared_ptr< std::vector< char > > buf_
Definition: byte_buffer.hpp:92