Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
buffer_ref.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/net/buffer_ref.hpp
3  *
4  * Look at the Doxygen below....
5  *
6  * Part of Project Thrill - http://project-thrill.org
7  *
8  * Copyright (C) 2013-2015 Timo Bingmann <[email protected]>
9  *
10  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
11  ******************************************************************************/
12 
13 #pragma once
14 #ifndef THRILL_NET_BUFFER_REF_HEADER
15 #define THRILL_NET_BUFFER_REF_HEADER
16 
17 #include <thrill/net/buffer.hpp>
19 
20 #include <string>
21 
22 namespace thrill {
23 namespace net {
24 
25 //! \addtogroup net_layer
26 //! \{
27 
28 /*!
29  * BufferRef represents a reference to a memory area as pointer and valid
30  * length. It is not deallocated or otherwise managed. This class can be used to
31  * pass around references to BufferBuilder and BufferReader objects.
32  */
33 class BufferRef
34 {
35 protected:
36  //! type used to store the bytes
37  using Byte = unsigned char;
38 
39  //! Allocated buffer pointer.
40  const Byte* data_;
41 
42  //! Size of valid data.
43  size_t size_;
44 
45 public:
46  //! Constructor, assign memory area from BufferBuilder.
47  explicit BufferRef(const BufferBuilder& bb)
48  : data_(bb.data()), size_(bb.size())
49  { }
50 
51  //! Constructor, assign memory area from pointer and length.
52  BufferRef(const void* data, size_t size)
53  : data_(reinterpret_cast<const Byte*>(data)), size_(size)
54  { }
55 
56  //! Constructor, assign memory area from string, does NOT copy.
57  explicit BufferRef(const std::string& str)
58  : data_(reinterpret_cast<const Byte*>(str.data())),
59  size_(str.size())
60  { }
61 
62  //! Constructor, assign memory area from net::Buffer, does NOT copy!
63  BufferRef(const Buffer& b) // NOLINT
64  : data_(b.data()), size_(b.size())
65  { }
66 
67  //! Return a pointer to the currently kept memory area.
68  const void * data() const
69  { return data_; }
70 
71  //! Return the currently valid length in bytes.
72  size_t size() const
73  { return size_; }
74 
75  //! Explicit conversion to std::string (copies memory of course).
77  { return std::string(reinterpret_cast<const char*>(data_), size_); }
78 
79  //! Explicit conversion to Buffer MOVING the memory ownership.
81  void* addr = reinterpret_cast<void*>(const_cast<Byte*>(data_));
82  Buffer b = Buffer::Acquire(addr, size_);
83  data_ = nullptr;
84  size_ = 0;
85  return b;
86  }
87 
88  //! Compare contents of two BufferRefs.
89  bool operator == (const BufferRef& br) const noexcept {
90  if (size_ != br.size_) return false;
91  return std::equal(data_, data_ + size_, br.data_);
92  }
93 
94  //! Compare contents of two BufferRefs.
95  bool operator != (const BufferRef& br) const noexcept {
96  return !operator == (br);
97  }
98 };
99 
100 //! \}
101 
102 } // namespace net
103 } // namespace thrill
104 
105 #endif // !THRILL_NET_BUFFER_REF_HEADER
106 
107 /******************************************************************************/
const void * data() const
Return a pointer to the currently kept memory area.
Definition: buffer_ref.hpp:68
bool operator==(const BufferRef &br) const noexcept
Compare contents of two BufferRefs.
Definition: buffer_ref.hpp:89
Buffer ToBuffer()
Explicit conversion to Buffer MOVING the memory ownership.
Definition: buffer_ref.hpp:80
BufferRef(const void *data, size_t size)
Constructor, assign memory area from pointer and length.
Definition: buffer_ref.hpp:52
BufferRef(const BufferBuilder &bb)
Constructor, assign memory area from BufferBuilder.
Definition: buffer_ref.hpp:47
size_t size() const
Return the currently valid length in bytes.
Definition: buffer_ref.hpp:72
const Byte * data_
Allocated buffer pointer.
Definition: buffer_ref.hpp:40
static Buffer Acquire(void *data, size_type size)
Definition: buffer.hpp:110
BufferRef(const std::string &str)
Constructor, assign memory area from string, does NOT copy.
Definition: buffer_ref.hpp:57
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220
unsigned char Byte
type used to store the bytes
Definition: buffer_ref.hpp:37
std::string ToString() const
Explicit conversion to std::string (copies memory of course).
Definition: buffer_ref.hpp:76
BufferRef represents a reference to a memory area as pointer and valid length.
Definition: buffer_ref.hpp:33
BufferBuilder represents a dynamically growable area of memory, which can be modified by appending in...
Simple buffer of characters without initialization or growing functionality.
Definition: buffer.hpp:40
bool operator!=(const BufferRef &br) const noexcept
Compare contents of two BufferRefs.
Definition: buffer_ref.hpp:95
size_t size_
Size of valid data.
Definition: buffer_ref.hpp:43
BufferRef(const Buffer &b)
Constructor, assign memory area from net::Buffer, does NOT copy!
Definition: buffer_ref.hpp:63