RMF
BufferConstHandle.h
Go to the documentation of this file.
1 /**
2  * \file RMF/BufferConstHandle.h
3  * \brief Declare RMF::BufferConstHandle.
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef RMF_BUFFER_CONST_HANDLE_H
10 #define RMF_BUFFER_CONST_HANDLE_H
11 
12 #include "RMF/config.h"
13 #include "infrastructure_macros.h"
14 #include "exceptions.h"
15 #include <memory>
16 #include <limits>
17 #include <stdint.h>
18 
19 RMF_ENABLE_WARNINGS
20 
21 namespace RMF {
22 //! Manage a shared buffer for loading a RMF.
23 
24 /** Buffers are pickleable in python and can be created from a wide range
25  of sources.
26 
27  See buffers.py for an example.
28  */
30  protected:
31  std::shared_ptr<std::vector<char> > data_;
32  int compare(BufferConstHandle o) const {
33  if (&*data_ < &*o.data_)
34  return -1;
35  else if (&*data_ > &*o.data_)
36  return 1;
37  else
38  return 0;
39  }
40 
41  public:
42 #ifndef SWIG
43  explicit BufferConstHandle(std::string r)
44  : data_(std::make_shared<std::vector<char> >(r.begin(), r.end())) {}
45 #endif
46  explicit BufferConstHandle(const std::vector<char> &r)
47  : data_(std::make_shared<std::vector<char> >(r.begin(), r.end())) {}
48  explicit BufferConstHandle(const std::vector<uint8_t> &r)
49  : data_(std::make_shared<std::vector<char> >(r.begin(), r.end())) {}
50  explicit BufferConstHandle(std::shared_ptr<std::vector<char> > r)
51  : data_(r) {}
52  const std::vector<char> &get_buffer() const { return *data_; }
53 #ifndef SWIG
54  //! get the buffer encoded in a string
55  std::string get_string() const {
56  return std::string(data_->begin(), data_->end());
57  }
58 #endif
60  RMF_HASHABLE(BufferConstHandle, return reinterpret_cast<size_t>(&*data_););
61  RMF_SHOWABLE(BufferConstHandle, "buffer");
62 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
63  std::pair<const uint8_t *, size_t> get_uint8_t() const {
64  return std::make_pair(reinterpret_cast<const uint8_t *>(&(*data_)[0]),
65  data_->size());
66  }
67  std::shared_ptr<std::vector<char> > get() const { return data_; }
68 #endif
69 };
70 
71 //! Produce hash values for boost hash tables.
72 inline std::size_t hash_value(const BufferConstHandle &t) {
73  return t.__hash__();
74 }
75 
76 RMFEXPORT BufferConstHandle read_buffer(std::string file_name);
77 
78 RMFEXPORT void write_buffer(BufferConstHandle buffer, std::string file_name);
79 
80 } /* namespace RMF */
81 
82 RMF_DISABLE_WARNINGS
83 
84 #endif /* RMF_BUFFER_CONST_HANDLE_H */
Manage a shared buffer for loading a RMF.
std::string get_string() const
get the buffer encoded in a string
std::size_t hash_value(const BufferConstHandle &t)
Produce hash values for boost hash tables.
Declarations of the various exception types.
#define RMF_COMPARISONS(Name)
Implement comparison in a class using field as the variable to compare.
#define RMF_HASHABLE(name, hashret)
Implement a hash function for the class.
Various general useful macros for IMP.