1/*
2 * Copyright, 2003, Tyler Dauwalder, tyler@dauwalder.net.
3 * Distributed under the terms of the MIT License.
4 */
5
6#ifndef _UDF_ARRAY_H
7#define _UDF_ARRAY_H
8
9
10#include "SupportDefs.h"
11#include "UdfDebug.h"
12
13#include <util/kernel_cpp.h>
14
15/*! \brief Slightly more typesafe static array type than built-in arrays,
16	with array length information stored implicitly (i.e. consuming no
17	physical space in the actual struct) via the \c arrayLength template
18	parameter.
19*/
20template<typename DataType, uint32 arrayLength>
21struct array {
22public:
23	void dump() const {
24		for (uint32 i = 0; i < arrayLength; i++)
25			data[i].print();
26	}
27
28	uint32 length() const { return arrayLength; }
29	uint32 size() const { return arrayLength * sizeof(DataType); }
30
31	// This doesn't appear to work. I don't know why.
32	DataType operator[] (int index) const { return data[index]; }
33
34	DataType data[arrayLength];
35};
36
37
38/*! \brief \c uint8 specialization of the \c array template struct. */
39template<uint32 arrayLength>
40struct array<uint8, arrayLength> {
41	void dump() const
42	{
43		const uint8 bytesPerRow = 8;
44		char classname[40];
45		sprintf(classname, "array<uint8, %ld>", arrayLength);
46
47		DUMP_INIT(classname);
48
49		for (uint32 i = 0; i < arrayLength; i++) {
50			if (i % bytesPerRow == 0)
51				PRINT(("[%ld:%ld]: ", i, i + bytesPerRow - 1));
52			SIMPLE_PRINT(("0x%.2x ", data[i]));
53			if ((i + 1) % bytesPerRow == 0 || i + 1 == arrayLength)
54				SIMPLE_PRINT(("\n"));
55		}
56	}
57
58	uint32 length() const { return arrayLength; }
59	uint32 size() const { return arrayLength; }
60	uint8 data[arrayLength];
61};
62
63
64/*! \brief \c char specialization of the \c array template struct. */
65template<uint32 arrayLength>
66struct array<char, arrayLength> {
67	void dump() const
68	{
69		const uint8 bytesPerRow = 8;
70		char classname[40];
71		sprintf(classname, "array<uint8, %ld>", arrayLength);
72
73		DUMP_INIT(classname);
74
75		for (uint32 i = 0; i < arrayLength; i++) {
76			if (i % bytesPerRow == 0)
77				PRINT(("[%ld:%ld]: ", i, i + bytesPerRow - 1));
78			SIMPLE_PRINT(("0x%.2x ", data[i]));
79			if ((i + 1) % bytesPerRow == 0 || i + 1 == arrayLength)
80				SIMPLE_PRINT(("\n"));
81		}
82	}
83
84	uint32 length() const { return arrayLength; }
85	uint32 size() const { return arrayLength; }
86	uint8 data[arrayLength];
87};
88
89#endif	// _UDF_ARRAY_H
90