utilities  0.0
bytestreamC.h
Go to the documentation of this file.
1 #ifndef BYTESTREAM_H
2 #define BYTESTREAM_H
3 #include <molpro/memory.h>
4 #include <cstdint>
5 
6 namespace molpro {
12 #ifdef MEMORY_I8
13 typedef int64_t fint;
14 #else
15 typedef int32_t fint;
16 #endif
17 
21 class bytestream {
22  public:
26  bytestream();
31  bytestream(const char* buffer);
32  ~bytestream();
51  // note that there cannot be an implementation like this for booleans because of lack of std::vector<bool>
52 
62  void reset();
63 
69  void append(const double* array, size_t length = 1);
77  void append(const fint* array, size_t length = 1);
83  void append(const char* array, size_t length = 1);
84 
85  void append(const bytestream& bs);
86 
87 #ifdef MEMORY_I8
88  void append(const int *array, size_t length=1) {
89  std::vector<fint> buffer(length);
90  for (size_t i=0; i<length; i++)
91  buffer[i] = array[i];
92  append(buffer.data(),length);
93  }
94 #endif
95 
96  void append(size_t i) {
97  fint v = i;
98  append(&v);
99  }
100 
101  // for other integer types
102  template<class T>
103  void append(const T* array, size_t length = 1) {
104  std::vector<fint> o(length);
105  for (size_t k = 0; k < length; k++) o[k] = array[k];
106  append(&o[0], o.size());
107  }
108 
109  template<class T>
110  void append(const std::vector<T> array) {
111  std::vector<fint> o;
112  for (auto& i : array) o.push_back(i);
113  append(&o[0], o.size());
114  }
115 
120  size_t size() const;
126  size_t position() const;
131  const std::vector<char>& data() const { return buffer; }
132 
133  void dump();
134 
135  size_t hash() const;
136 
137  private:
138  enum datatypes { datatype_char = 0, datatype_int = 1, datatype_double = 2, datatype_bool = 3 };
139 
140  struct metadata { size_t length; enum datatypes datatype; };
144  bytestream::metadata get_metadata();
150  void put_metadata(size_t length, enum datatypes datatype);
151  std::vector<char> buffer;
152  size_t pointer;
153 public:
154  size_t position() { return pointer; }
155 };
156 }
157 
158 extern "C" size_t memory_bytestream_hash(const char* s, size_t n);
159 
160 #endif // BYTESTREAM_H
size_t memory_bytestream_hash(const char *s, size_t n)
Definition: bytestreamC.cpp:272
A template for a container class like std::array<T> but with the following features.
Definition: memory.h:666
The bytestream class provides a container for data to be serialised, and subsequently unpacked.
Definition: bytestreamC.h:21
void append(const std::vector< T > array)
Definition: bytestreamC.h:110
molpro::array< fint > ints()
Interpret the next array in the object as an array of integers, advance the pointer to the following ...
Definition: bytestreamC.cpp:57
bytestream byteStream()
Interpret the next item in the object as a bytestream, advance the pointer to the following object,...
Definition: bytestreamC.cpp:84
size_t position() const
The current position in the bytestream data during unpacking through successive calls to doubles(),...
Definition: bytestreamC.cpp:262
molpro::array< char > chars()
Interpret the next array in the object as an array of characters, advance the pointer to the followin...
Definition: bytestreamC.cpp:71
size_t hash() const
Definition: bytestreamC.cpp:277
void append(size_t i)
Definition: bytestreamC.h:96
void reset()
Reset the pointer to the beginning of the buffer.
Definition: bytestreamC.cpp:257
size_t size() const
The size of the bytestream data.
Definition: bytestreamC.cpp:261
bytestream()
Construct an empty bytestream object.
Definition: bytestreamC.cpp:11
void dump()
Definition: bytestreamC.cpp:264
void append(const T *array, size_t length=1)
Definition: bytestreamC.h:103
void append(const double *array, size_t length=1)
Add a double array to the bytestream.
Definition: bytestreamC.cpp:182
const std::vector< char > & data() const
The raw data of the object.
Definition: bytestreamC.h:131
molpro::array< double > doubles()
Interpret the next array in the object as an array of doubles, advance the pointer to the following o...
Definition: bytestreamC.cpp:29
~bytestream()
Definition: bytestreamC.cpp:26
size_t position()
Definition: bytestreamC.h:154
Class that manages input options.
Definition: iostream.h:14
int32_t fint
Type for integers stored in a bytestream. The intention is that these should match the Fortran standa...
Definition: bytestreamC.h:15