TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
serialization.h
1 /*
2  * Copyright (c) 2017-present, Facebook, Inc.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #pragma once
9 
10 #include <streambuf>
11 #include <string_view>
12 
13 // Place these here for convenience
14 #include <cereal/archives/binary.hpp>
15 #include <cereal/types/string.hpp>
16 #include <cereal/types/unordered_map.hpp>
17 #include <cereal/types/vector.hpp>
18 
19 namespace common {
20 
21 /**
22  * A stream buffer for reading from a vector of bytes.
23  * This can be used to construct a std::istream from a given binary blob as
24  * follows:
25  *
26 ```
27  std::vector<char> data = getDataFromSomewhere();
28  IMembuf mbuf(data);
29  std::istream is(&mbuf);
30  // Extract data from istream as usual.
31 ```
32  */
33 class IMembuf : public std::streambuf {
34  public:
35  explicit IMembuf(std::vector<char> const& data);
36  explicit IMembuf(std::string_view sv);
37 };
38 
39 /**
40  * A stream buffer for writing to an accessible vector of bytes.
41  * This can be used to construct a std::ostream as follows:
42  *
43 ```
44  OMembuf mbuf;
45  std::ostream os(&mbuf);
46  // Write data to ostream as usual
47  os.flush();
48  auto& data = mbuf.data(); // Obtain data without extra copy
49 ```
50  */
51 class OMembuf : public std::streambuf {
52  public:
53  OMembuf() {}
54 
55  std::vector<char>& data();
56  std::vector<char> takeData();
57 
58  using int_type = typename std::streambuf::int_type;
59  virtual int_type overflow(int_type ch = traits_type::eof());
60 
61  virtual std::streamsize xsputn(char const* s, std::streamsize num);
62 
63  private:
64  std::vector<char> buffer_;
65 };
66 } // namespace common
typename std::streambuf::int_type int_type
Definition: serialization.h:58
A stream buffer for writing to an accessible vector of bytes.
Definition: serialization.h:51
General utilities.
Definition: assert.cpp:7
OMembuf()
Definition: serialization.h:53
A stream buffer for reading from a vector of bytes.
Definition: serialization.h:33
IMembuf(std::vector< char > const &data)
Definition: serialization.cpp:12