forked from ostis-ai/sc-machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sc_stream.hpp
135 lines (114 loc) · 3.62 KB
/
sc_stream.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* This source file is part of an OSTIS project. For the latest info, see http://ostis.net
* Distributed under the MIT License
* (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
*/
#pragma once
extern "C"
{
#include "sc-core/sc_memory_headers.h"
}
#include "sc_utils.hpp"
#include <type_traits>
class ScStream
{
friend class ScMemoryContext;
ScStream(ScStream const & other) = delete;
ScStream & operator=(ScStream const & other) = delete;
public:
_SC_EXTERN explicit ScStream();
_SC_EXTERN explicit ScStream(sc_stream * stream);
_SC_EXTERN explicit ScStream(std::string const & fileName, sc_uint8 flags);
_SC_EXTERN explicit ScStream(sc_char * buffer, size_t bufferSize, sc_uint8 flags);
_SC_EXTERN explicit ScStream(sc_char const * buffer, size_t bufferSize, sc_uint8 flags);
_SC_EXTERN explicit ScStream(sc_char * buffer, size_t bufferSize, sc_uint8 flags, sc_bool dataOwner);
_SC_EXTERN ~ScStream();
_SC_EXTERN void Reset();
_SC_EXTERN bool IsValid() const;
_SC_EXTERN bool Read(sc_char * buff, size_t buffLen, size_t & readBytes) const;
_SC_EXTERN bool Write(sc_char * data, size_t dataLen, size_t & writtenBytes);
_SC_EXTERN bool Seek(sc_stream_seek_origin origin, size_t offset);
//! Check if current position at the end of file
_SC_EXTERN bool Eof() const;
//! Returns length of stream in bytes
_SC_EXTERN size_t Size() const;
//! Returns current position of stream
_SC_EXTERN size_t Pos() const;
//! Check if stream has a specified flag
_SC_EXTERN bool HasFlag(sc_uint8 flag);
template <typename Type>
bool ReadType(Type & value)
{
size_t readBytes = 0;
return Read((sc_char *)&value, sizeof(Type), readBytes) && (readBytes == sizeof(Type));
}
protected:
sc_stream * m_stream;
};
using ScStreamPtr = std::shared_ptr<ScStream>;
class ScStreamMemory : public ScStream
{
public:
_SC_EXTERN explicit ScStreamMemory(MemoryBufferPtr const & buff);
_SC_EXTERN virtual ~ScStreamMemory();
private:
MemoryBufferPtr m_buffer;
};
class ScStreamConverter
{
public:
static _SC_EXTERN bool StreamToString(ScStreamPtr const & stream, std::string & outString);
static _SC_EXTERN ScStreamPtr StreamFromString(std::string const & str);
};
template <typename T>
inline ScStreamPtr ScStreamMakeReadT(T const & value)
{
static_assert(std::is_arithmetic<T>::value, "Just simple arithmetic types are supported");
return std::make_shared<ScStream>(
(sc_char *)&value, sizeof(T), SC_STREAM_FLAG_READ | SC_STREAM_FLAG_SEEK | SC_STREAM_FLAG_TELL);
}
// TODO: implement with enable_if
inline ScStreamPtr ScStreamMakeRead(std::string const & value)
{
return ScStreamConverter::StreamFromString(value);
}
inline ScStreamPtr ScStreamMakeRead(uint8_t const & value)
{
return ScStreamMakeReadT(value);
}
inline ScStreamPtr ScStreamMakeRead(uint16_t const & value)
{
return ScStreamMakeReadT(value);
}
inline ScStreamPtr ScStreamMakeRead(uint32_t const & value)
{
return ScStreamMakeReadT(value);
}
inline ScStreamPtr ScStreamMakeRead(uint64_t const & value)
{
return ScStreamMakeReadT(value);
}
inline ScStreamPtr ScStreamMakeRead(int8_t const & value)
{
return ScStreamMakeReadT(value);
}
inline ScStreamPtr ScStreamMakeRead(int16_t const & value)
{
return ScStreamMakeReadT(value);
}
inline ScStreamPtr ScStreamMakeRead(int32_t const & value)
{
return ScStreamMakeReadT(value);
}
inline ScStreamPtr ScStreamMakeRead(int64_t const & value)
{
return ScStreamMakeReadT(value);
}
inline ScStreamPtr ScStreamMakeRead(float const & value)
{
return ScStreamMakeReadT(value);
}
inline ScStreamPtr ScStreamMakeRead(double const & value)
{
return ScStreamMakeReadT(value);
}