-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.cpp
More file actions
123 lines (96 loc) · 3.36 KB
/
Copy pathdebug.cpp
File metadata and controls
123 lines (96 loc) · 3.36 KB
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
/* File: /lib/impl/stream/debug.cpp
*
* This file is a part of cpplib project which is distributed under MIT License.
* See file LICENSE for full license details.
*
* Copyright (c) 2020-present Nikita Zuev (V.Slavski!) <[email protected]>
*/
#include <cstdlib>
#include <algorithm>
#include "../../../lib/system/error.hpp"
#include "./debug.hpp"
namespace lib::stream::impl {
// IMPLEMENTATION lib::stream::impl::debug
debug::debug()
{ reset(); }
debug::debug( data::rwstream_t * stream, usize min_rw, usize max_rw )
{ reset( stream, min_rw, max_rw ); }
void debug::reset() {
stream = nullptr;
min_rw = 0;
max_rw = ::std::numeric_limits<decltype(max_rw)>::max();
next_read = ::std::numeric_limits<decltype(next_read)>::max();
next_write = ::std::numeric_limits<decltype(next_write)>::max();
}
void debug::reset( data::rwstream_t * stream_, usize min_rw_, usize max_rw_ ) {
stream = stream_;
min_rw = min_rw_;
max_rw = max_rw_;
shuffle();
}
void debug::shuffle() {
next_read = min_rw + (usize) ::std::rand() % ( max_rw - min_rw );
next_write = min_rw + (usize) ::std::rand() % ( max_rw - min_rw );
}
// IMPLEMENTATION lib::stream::impl::debug: lib::data::rstream_t
data::result_t debug::read( const data::buffer_t & buffer )/* override*/ {
if ( stream == nullptr )
return make_error_not_configured();
const auto count = ::std::min( next_read, buffer.size() );
shuffle();
return stream->read({ buffer.data(), count });
}
data::result_t debug::peek( const data::buffer_t & buffer )/* override*/ {
if ( stream == nullptr )
return make_error_not_configured();
const auto count = ::std::min( next_read, buffer.size() );
shuffle();
return stream->peek({ buffer.data(), count });
}
bool debug::read_flush()/* override*/ {
return stream != nullptr ? stream->read_flush() : false;
}
data::result_t debug::read_size()/* override*/ {
if ( stream == nullptr )
return make_error_not_configured();
auto result = stream->read_size();
if ( result.success() )
result = ::std::min( next_read, result.value() );
return result;
}
::std::error_condition debug::read_error() const/* override*/ {
return stream != nullptr ? stream->read_error() : make_error_no_error();
}
// IMPLEMENTATION lib::stream::impl::debug: lib::data::wstream_t
data::result_t debug::write( const data::cbuffer_t & buffer )/* override*/ {
if ( stream == nullptr )
return make_error_not_configured();
const auto count = ::std::min( next_write, buffer.size() );
shuffle();
return stream->write({ buffer.data(), count });
}
bool debug::write_flush()/* override*/ {
return stream != nullptr ? stream->write_flush() : false;
}
data::result_t debug::write_size()/* override*/ {
if ( stream == nullptr )
return make_error_not_configured();
auto result = stream->write_size();
if ( result.success() )
result = ::std::min( next_write, result.value() );
return result;
}
::std::error_condition debug::write_error() const/* override*/ {
return stream != nullptr ? stream->write_error() : make_error_no_error();
}
// IMPLEMENTATION lib::stream::impl::debug: lib::data::rwstream_t
bool debug::flush()/* override*/ {
return stream != nullptr ? stream->flush() : false;
}
data::result_t debug::size()/* override*/ {
return make_error_not_implemented();
}
::std::error_condition debug::error() const/* override*/ {
return stream != nullptr ? stream->error() : make_error_no_error();
}
} // namespace lib::stream::impl