Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
StorageView.h
Go to the documentation of this file.
1 //===-- serialbox/core/StorageView.h ------------------------------------------------*- C++ -*-===//
2 //
3 // S E R I A L B O X
4 //
5 // This file is distributed under terms of BSD license.
6 // See LICENSE.txt for more information.
7 //
8 //===------------------------------------------------------------------------------------------===//
9 //
14 //===------------------------------------------------------------------------------------------===//
15 
16 #ifndef SERIALBOX_CORE_STORAGEVIEW_H
17 #define SERIALBOX_CORE_STORAGEVIEW_H
18 
19 #include "serialbox/core/Logging.h"
21 #include "serialbox/core/Slice.h"
23 #include "serialbox/core/Type.h"
24 #include <utility>
25 #include <vector>
26 
27 namespace serialbox {
28 
31 
33 class StorageView {
34 public:
37 
39  StorageView(void* originPtr, TypeID type, const std::vector<int>& dims,
40  const std::vector<int>& strides);
41 
43  StorageView(void* originPtr, TypeID type, std::vector<int>&& dims, std::vector<int>&& strides);
44 
46  StorageView(const StorageView& other) = default;
47 
49  StorageView(StorageView&& other) = default;
50 
54 
57  return StorageViewIterator(originPtr_, bytesPerElement(), dims_, strides_, slice_, true);
58  }
59  ConstStorageViewIterator begin() const noexcept {
60  return ConstStorageViewIterator(originPtr_, bytesPerElement(), dims_, strides_, slice_, true);
61  }
62 
64  StorageViewIterator end() noexcept {
65  return StorageViewIterator(originPtr_, bytesPerElement(), dims_, strides_, slice_, false);
66  }
67  ConstStorageViewIterator end() const noexcept {
68  return ConstStorageViewIterator(originPtr_, bytesPerElement(), dims_, strides_, slice_, false);
69  }
70 
74 
76  template <class T>
77  T* originPtrAs() noexcept {
78  return (T*)(originPtr_);
79  }
80  template <class T>
81  const T* originPtrAs() const noexcept {
82  return (T*)(originPtr_);
83  }
84 
86  Byte* originPtr() noexcept { return originPtr_; }
87  const Byte* originPtr() const noexcept { return originPtr_; }
88 
90  TypeID type() const noexcept { return type_; }
91 
93  int bytesPerElement() const noexcept { return TypeUtil::sizeOf(type_); }
94 
96  const std::vector<int>& dims() const noexcept { return dims_; }
97 
99  const std::vector<int>& strides() const noexcept { return strides_; }
100 
104 
106  StorageView& operator=(const StorageView& other) = default;
107 
109  StorageView& operator=(StorageView&& other) = default;
110 
112  void swap(StorageView& other) noexcept;
113 
115  bool operator==(const StorageView& right) const noexcept;
116 
118  bool operator!=(const StorageView& right) const noexcept { return (!(*this == right)); }
119 
121  friend std::ostream& operator<<(std::ostream& stream, const StorageView& s);
122 
124 
126  void setSlice(Slice slice);
127 
129  Slice& getSlice() noexcept { return slice_; }
130  const Slice& getSlice() const noexcept { return slice_; }
131 
134  bool isMemCopyable() const noexcept;
135 
137  std::size_t size() const noexcept;
138 
140  std::size_t sizeInBytes() const noexcept;
141 
142 private:
143  Byte* originPtr_;
144  TypeID type_;
145  std::vector<int> dims_;
146  std::vector<int> strides_;
147  Slice slice_;
148 };
149 
152 void swap(StorageView& a, StorageView& b) noexcept;
153 
155 
156 } // namespace serialbox
157 
158 #endif
Byte * originPtr() noexcept
Get raw data pointer.
Definition: StorageView.h:86
friend std::ostream & operator<<(std::ostream &stream, const StorageView &s)
Convert to stream.
Definition: StorageView.cpp:54
std::size_t sizeInBytes() const noexcept
Size of the allocated, sliced data (without padding) in Bytes.
T * originPtrAs() noexcept
Get data pointer as type T of the origin of the data.
Definition: StorageView.h:77
static int sizeOf(TypeID id)
Get size of the type.
Definition: Type.cpp:73
Represent a mutable view to a multi-dimensional storage.
Definition: StorageView.h:33
bool operator!=(const StorageView &right) const noexcept
Test for inequality.
Definition: StorageView.h:118
void setSlice(Slice slice)
Set the slice of the StorageView
Definition: StorageView.cpp:71
TypeID type() const noexcept
Get type.
Definition: StorageView.h:90
Namespace of the serialbox library.
Definition: Archive.h:25
TypeID
Type-id of types recognized by serialbox.
Definition: Type.h:55
char Byte
Represent a byte i.e sizeof(Byte) == 1.
Definition: Type.h:35
StorageView & operator=(const StorageView &other)=default
Copy assignment.
const std::vector< int > & dims() const noexcept
Get dimensions.
Definition: StorageView.h:96
Non-mutable forward iterator to access the data of a StorageView.
Slice & getSlice() noexcept
Get the slice of the StorageView
Definition: StorageView.h:129
bool isMemCopyable() const noexcept
Return true if the storage is contiguous in memory (i.e no padding) and is column-major ordered...
Definition: StorageView.cpp:91
StorageViewIterator end() noexcept
Iterator to the end of the data.
Definition: StorageView.h:64
std::size_t size() const noexcept
Size of the allocated, sliced data (without padding)
StorageViewIterator begin() noexcept
Iterator to the beginning of the data.
Definition: StorageView.h:56
const std::vector< int > & strides() const noexcept
Get strides.
Definition: StorageView.h:99
StorageView(void *originPtr, TypeID type, const std::vector< int > &dims, const std::vector< int > &strides)
Construct StorageView.
Definition: StorageView.cpp:25
serialbox::Slice slice
Specification of the slice indices which is used for partial loading of serialized data...
Definition: Slice.h:43
bool operator==(const StorageView &right) const noexcept
Test for equality.
Definition: StorageView.cpp:50
Specification of the slice indices which is used for partial loading of serialized data...
Definition: Slice.h:53
void swap(StorageView &other) noexcept
Swap with other.
Definition: StorageView.cpp:43
Mutable forward iterator to access the data of a StorageView.
int bytesPerElement() const noexcept
Get bytes per element.
Definition: StorageView.h:93