MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
WEMPersistenceHelper.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2015, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#pragma once
14
15#include "MLWEMIncludes.h"
16#include "WEMPrimitive.h"
17
18#include <cstring>
19
21
22namespace WEMPersistenceHelper
23{
24 bool isWEMFormatIdentifier(const std::string& wemIdentifier);
25}
26
33
36{
37public:
39
40
41 template <typename DT> DT readValueFromMapAhead()
42 {
43 static_assert(std::is_integral_v<DT> || std::is_floating_point_v<DT>,
44 "Only integral or floating point types are currently supported.");
45
46 DT value;
47
48 if constexpr (std::is_same_v<DT, bool>) {
49 const auto* tmp = getCurrentAddress(1);
50 value = *tmp != '\0';
51 }
52 else {
53 memcpy(&value, getCurrentAddress(sizeof(DT)), sizeof(DT));
54 if (_swapBytes) {
55 MLSwapBytes(reinterpret_cast<unsigned char*>(&value), sizeof(DT), sizeof(DT));
56 }
57 }
58 return value;
59 }
60
61
62 template <typename DT> DT readValueFromMap()
63 {
64 auto value = readValueFromMapAhead<DT>();
65 static_assert(!std::is_same_v<DT, bool> || sizeof(bool) == 1, "Sizeof bool is expected to be 1!");
66 _currentAddress += sizeof(DT);
67 return value;
68 }
69
70 void readStringFromMap(std::string& value, size_t stringLength);
71 void readStringWithSizeFromMap(std::string& value);
72 void readVector3FromMap(Vector3& value, size_t byteSize);
73 void readVector4FromMap(Vector4& value, size_t byteSize);
74
75 const char* getInitialAddress() const
76 {
77 return _initialAddress;
78 }
79 const char* getCurrentAddress() const
80 {
81 return _currentAddress;
82 }
83 const char* getCurrentAddress(size_t byteSize);
84
85 void advance(size_t byteSize);
88 {
89 _swapBytes = swapBytes;
90 }
91
92private:
93 bool _readValueWillBeWithinFileMapping(size_t byteSize) const;
94
95 const MLuint64 _filesize;
96 const char* const _initialAddress;
97 char* _currentAddress;
98 const char* const _endAddress;
99 bool _swapBytes;
100};
101
103
105{
106public:
107 OutStreamWrapper(std::ostream& outstream);
108
109 template <typename DT> void writeValueToStream(const DT& value, size_t byteSize)
110 {
111 outstream->write(reinterpret_cast<const char*>(&value), byteSize);
112 }
113
115 void writeStringToStream(const std::string& value);
116 void writeStringWithSizeToStream(const std::string& value);
117
118private:
119 std::ostream* outstream;
120};
121
122
124
Wraps a FileMap so we can handle errors and exceptions more easily.
MLuint64 getNumBytesRead() const
void advance(size_t byteSize)
void readVector4FromMap(Vector4 &value, size_t byteSize)
FileMapWrapper(MLuint64 filesize, char *startAddress)
void setSwapBytes(bool swapBytes)
const char * getCurrentAddress() const
void readStringWithSizeFromMap(std::string &value)
void readStringFromMap(std::string &value, size_t stringLength)
const char * getCurrentAddress(size_t byteSize)
const char * getInitialAddress() const
void readVector3FromMap(Vector3 &value, size_t byteSize)
void writePrimitiveIdToStream(WEMPrimitive *primitive)
void writeStringWithSizeToStream(const std::string &value)
OutStreamWrapper(std::ostream &outstream)
void writeValueToStream(const DT &value, size_t byteSize)
void writeStringToStream(const std::string &value)
This is the base class for the WEM elements nodes, edges, and faces.
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
UINT64 MLuint64
Introduce platform-independent 64-bit unsigned integer type.
Definition mlTypeDefs.h:424
ML_UTILS_EXPORT void MLSwapBytes(unsigned char *data, size_t numBytes, size_t dTypeSize)
Takes numBytes starting at position data and swaps byte 0 with byte numBytes - 1, byte 1 with numByte...
bool isWEMFormatIdentifier(const std::string &wemIdentifier)