2 * Copyright (c) 2004 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #ifndef __BASE_TIMEBUF_HH__
30 #define __BASE_TIMEBUF_HH__
50 assert (idx >= -past && idx <= future);
57 friend class TimeBuffer;
59 TimeBuffer<T> *buffer;
68 wire(TimeBuffer<T> *buf, int i)
69 : buffer(buf), index(i)
77 : buffer(i.buffer), index(i.index)
80 const wire &operator=(const wire &i)
87 const wire &operator=(int idx)
93 const wire &operator+=(int offset)
99 const wire &operator-=(int offset)
111 wire &operator++(int)
115 return wire(this, i);
124 wire &operator--(int)
128 return wire(this, i);
130 T &operator*() const { return *buffer->access(index); }
131 T *operator->() const { return buffer->access(index); }
136 TimeBuffer(int p, int f)
137 : past(p), future(f), size(past + future + 1),
138 data(new char[size * sizeof(T)]), index(size), base(0)
140 assert(past >= 0 && future >= 0);
142 for (int i = 0; i < size; i++) {
144 memset(ptr, 0, sizeof(T));
157 for (int i = 0; i < size; ++i)
158 (reinterpret_cast<T *>(index[i]))->~T();
168 int ptr = base + future;
171 (reinterpret_cast<T *>(index[ptr]))->~T();
172 memset(index[ptr], 0, sizeof(T));
178 //Need more complex math here to calculate index.
181 int vector_index = idx + base;
182 if (vector_index >= size) {
183 vector_index -= size;
184 } else if (vector_index < 0) {
185 vector_index += size;
188 return reinterpret_cast<T *>(index[vector_index]);
191 T &operator[](int idx)
193 //Need more complex math here to calculate index.
196 int vector_index = idx + base;
197 if (vector_index >= size) {
198 vector_index -= size;
199 } else if (vector_index < 0) {
200 vector_index += size;
203 return reinterpret_cast<T &>(*index[vector_index]);
206 wire getWire(int idx)
210 return wire(this, idx);
215 return wire(this, 0);
219 #endif // __BASE_TIMEBUF_HH__