MeVisLab Toolbox Reference
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
32
35{
36public:
38
39
40 template <typename DT> DT readValueFromMapAhead()
41 {
42 static_assert(std::is_integral_v<DT> || std::is_floating_point_v<DT>, "Only integral or floating point types are currently supported.");
43
44 DT value;
45
46 if constexpr (std::is_same_v<DT, bool>)
47 {
48 const auto *tmp = getCurrentAddress(1);
49 value = *tmp != '\0';
50 }
51 else
52 {
53 memcpy(&value, getCurrentAddress(sizeof(DT)), sizeof(DT));
54 if (_swapBytes)
55 {
56 MLSwapBytes(reinterpret_cast<unsigned char *>(&value), sizeof(DT), sizeof(DT));
57 }
58 }
59 return value;
60 }
61
62
63 template <typename DT>
65 {
66 auto value = readValueFromMapAhead<DT>();
67 static_assert(!std::is_same_v<DT, bool> || sizeof(bool) == 1, "Sizeof bool is expected to be 1!");
68 _currentAddress += sizeof(DT);
69 return value;
70 }
71
72 void readStringFromMap(std::string& value, size_t stringLength);
73 void readStringWithSizeFromMap(std::string& value);
74 void readVector3FromMap(Vector3& value, size_t byteSize);
75 void readVector4FromMap(Vector4& value, size_t byteSize);
76
77 const char* getInitialAddress() const { return _initialAddress; }
78 const char* getCurrentAddress() const { return _currentAddress; }
79 const char* getCurrentAddress(size_t byteSize);
80
81 void advance(size_t byteSize);
84 {
85 _swapBytes = swapBytes;
86 }
87 private:
88 bool _readValueWillBeWithinFileMapping(size_t byteSize) const;
89
90 const MLuint64 _filesize;
91 const char* const _initialAddress;
92 char* _currentAddress;
93 const char* const _endAddress;
94 bool _swapBytes;
95};
96
98
100{
101public:
102 OutStreamWrapper(std::ostream& outstream);
103
104 template <typename DT>
105 void writeValueToStream(const DT& value, size_t byteSize)
106 {
107 outstream->write(reinterpret_cast<const char*>(&value), byteSize);
108 }
109
111 void writeStringToStream(const std::string& value);
112 void writeStringWithSizeToStream(const std::string& value);
113
114private:
115 std::ostream* outstream;
116};
117
118
120
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:425
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)