Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | /* |
2 | * ========== Copyright Header Begin ========================================== | |
3 | * | |
4 | * OpenSPARC T2 Processor File: BL_BoundedArray.h | |
5 | * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
6 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES. | |
7 | * | |
8 | * The above named program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public | |
10 | * License version 2 as published by the Free Software Foundation. | |
11 | * | |
12 | * The above named program is distributed in the hope that it will be | |
13 | * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public | |
18 | * License along with this work; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. | |
20 | * | |
21 | * ========== Copyright Header End ============================================ | |
22 | */ | |
23 | #ifndef __BL_BoundedArray_h__ | |
24 | #define __BL_BoundedArray_h__ | |
25 | ||
26 | #include <assert.h> | |
27 | #include <sys/types.h> | |
28 | #include <stdio.h> | |
29 | #include <string.h> | |
30 | ||
31 | // Bounded array template. Takes the type of the array elements and | |
32 | // the array's length as template arguments. Provides range checked | |
33 | // operator[] methods. | |
34 | ||
35 | template<typename TYPE, uint64_t SIZE, bool DO_INIT=false> class BL_BoundedArray | |
36 | { | |
37 | public: | |
38 | ||
39 | BL_BoundedArray() | |
40 | { | |
41 | !DO_INIT || memset(0); | |
42 | } | |
43 | ~BL_BoundedArray() {} | |
44 | ||
45 | TYPE& operator[](uint64_t ndx) | |
46 | { | |
47 | assert(ndx < SIZE); | |
48 | return array[ndx]; | |
49 | } | |
50 | ||
51 | const TYPE& operator[](uint64_t ndx) const | |
52 | { | |
53 | assert(ndx < SIZE); | |
54 | return array[ndx]; | |
55 | } | |
56 | ||
57 | TYPE* memset(int c, size_t n = 0) | |
58 | { | |
59 | if (n == 0) | |
60 | n = SIZE; | |
61 | ::memset(array, c, n * sizeof(TYPE)); | |
62 | return array; | |
63 | } | |
64 | ||
65 | private: | |
66 | ||
67 | TYPE array[SIZE]; | |
68 | }; | |
69 | ||
70 | #endif /* __BL_BoundedArray_h__ */ |