-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRing.cpp
More file actions
67 lines (41 loc) · 937 Bytes
/
Copy pathRing.cpp
File metadata and controls
67 lines (41 loc) · 937 Bytes
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
#include <algorithm>
#include <iterator>
#include <iostream>
#include <utility>
#include <stdexcept>
#include "Ring.h"
using namespace std;
RingBuffer::RingBuffer(size_t numEntries, ostream* ostr)
:mEntries(numEntries), mOstr(ostr), mWrapped(false)
{
if (numEntries == 0)
throw invalid_argument("Number of entries must be > 0");
mNext = begin(mEntries);
}
void RingBuffer::addStringEntry(string&& entry)
{
if (mOstr)
{
*mOstr << entry << endl;
}
*mNext = std::move(entry);
++mNext;
if (mNext == end(mEntries))
{
mNext = begin(mEntries);
mWrapped = true;
}
}
ostream* RingBuffer::setOutput(ostream* newOstr)
{
return std::exchange(mOstr, newOstr);
}
ostream& operator<<(ostream& ostr, RingBuffer& rb)
{
if (rb.mWrapped)
{
copy(rb.mNext, end(rb.mEntries), ostream_iterator<string>(ostr, "\n"));
}
copy(begin(rb.mEntries), rb.mNext, ostream_iterator<string>(ostr, "\n"));
return ostr;
}