summaryrefslogtreecommitdiff
path: root/i/chuck/src/data/data_circular_buffer.cc
blob: be175260fa2deac211b077298341f56730474d1b (plain)
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
// data_circular_buffer.cc
// marvin - programme du robot 2006. {{{
//
// Copyright (C) 2006 Dufour J�r�my
//
// Robot APB Team/Efrei 2004.
//        Web: http://assos.efrei.fr/robot/
//      Email: robot AT efrei DOT fr
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// }}}
#include "data_circular_buffer.hh"

#include <cstring> 		// std::memcpy
#include <stdexcept>		// std::runtime_error
#include <algorithm>		// std::swap, std::min

#include <iostream> // XXX

/// Default constructor.
/// Size is the allocated size in memory.
DataCircularBuffer::DataCircularBuffer (unsigned size)
    : size_ (size + 1), startPos_ (0), endPos_ (0)
{
    // The + 1 for size is because, in some cases, we will have an empty case.
    buffer_ = new uint8_t [size_];
}

/// Destructor.
DataCircularBuffer::~DataCircularBuffer (void)
{
    delete [] buffer_;
}

/// Read data of maximum size in the data pointer (data must be allocated of
/// size).
unsigned
DataCircularBuffer::read (uint8_t *data, unsigned size)
{
    if (!data || !size)
	throw std::invalid_argument ("Invalid parameter");
    return internalRead (data, size);
}

/// Internal read, do the real job.
unsigned
DataCircularBuffer::internalRead (uint8_t *data, unsigned size)
{
    // Size of data to read
    unsigned sizeToRead = std::min (maxReadableData (), size);
    // Size of data to read for the end of the buffer
    unsigned sizeToReadEnd = std::min (size_ - startPos_, sizeToRead);
    // Copy end part of buffer
    if (data)
	std::memcpy (&data[0], &buffer_[startPos_], sizeToReadEnd);
    // Need to do the begin part of buffer ?
    int sizeToReadStart = sizeToRead - sizeToReadEnd;
    if (sizeToReadStart > 0)
      {
	if (data)
	    std::memcpy (&data[sizeToReadEnd], &buffer_[0], sizeToReadStart);
	// Move start of data
	startPos_ = 0 + sizeToReadStart;
      }
    else
	// Move start of data
	startPos_ = (startPos_ + sizeToRead) % size_;
    return sizeToRead;
}

/// Write data of size length.
void
DataCircularBuffer::write (const uint8_t *data, unsigned size)
{
    if (!data || !size)
	throw std::invalid_argument ("Invalid parameter");

    // Max size we can write
    unsigned sizeToWrite = std::min (size, size_ - 1);
    
    // Read data before overwrite it if needed
    int sizeToRead = sizeToWrite - (size_ - maxReadableData ()) + 1;
    if (sizeToRead > 0)
	if (internalRead (0, sizeToRead) != (unsigned) sizeToRead)
		throw std::runtime_error ("Unexpected error");
    
    // Max size we can write until the end of the buffer
    unsigned sizeToWriteEnd = std::min (size_ - endPos_, sizeToWrite);
    // Write first part.
    std::memcpy (&buffer_[endPos_], &data[size - sizeToWrite + 0], sizeToWriteEnd);
    // More to write ?
    unsigned sizeToWriteStart = sizeToWrite - sizeToWriteEnd;
    if (sizeToWriteStart > 0)
      {
	std::memcpy (&buffer_[0], &data[size - sizeToWrite + sizeToWriteEnd], sizeToWriteStart);
	endPos_ = 0 + sizeToWriteStart;
      }
    else
	endPos_ = (endPos_ + sizeToWrite) % size_;
}

/// Swap data between tow DataCircularBuffer.
void
DataCircularBuffer::swap (DataCircularBuffer &d)
{
    std::swap (buffer_, d.buffer_);
    std::swap (size_, d.size_);
    std::swap (startPos_, d.startPos_);
    std::swap (endPos_, d.endPos_);
}