xref: /haiku/src/add-ons/kernel/file_systems/udf/Array.h (revision 3218588cc72c8f16cab10024ececc6b5d4b76288)
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 /*! \brief Slightly more typesafe static array type than built-in arrays,
14 	with array length information stored implicitly (i.e. consuming no
15 	physical space in the actual struct) via the \c arrayLength template
16 	parameter.
17 */
18 template<typename DataType, uint32 arrayLength>
19 struct array {
20 public:
dumparray21 	void dump() const {
22 		for (uint32 i = 0; i < arrayLength; i++)
23 			data[i].print();
24 	}
25 
lengtharray26 	uint32 length() const { return arrayLength; }
sizearray27 	uint32 size() const { return arrayLength * sizeof(DataType); }
28 
29 	// This doesn't appear to work. I don't know why.
30 	DataType operator[] (int index) const { return data[index]; }
31 
32 	DataType data[arrayLength];
33 };
34 
35 
36 /*! \brief \c uint8 specialization of the \c array template struct. */
37 template<uint32 arrayLength>
38 struct array<uint8, arrayLength> {
39 	void dump() const
40 	{
41 		const uint8 bytesPerRow = 8;
42 		char classname[40];
43 		sprintf(classname, "array<uint8, %" B_PRIu32 ">", arrayLength);
44 
45 		DUMP_INIT(classname);
46 
47 		for (uint32 i = 0; i < arrayLength; i++) {
48 			if (i % bytesPerRow == 0)
49 				PRINT(("[%" B_PRIu32 ":%" B_PRIu32 "]: ",
50 					i, i + bytesPerRow - 1));
51 			SIMPLE_PRINT(("0x%.2x ", data[i]));
52 			if ((i + 1) % bytesPerRow == 0 || i + 1 == arrayLength)
53 				SIMPLE_PRINT(("\n"));
54 		}
55 	}
56 
57 	uint32 length() const { return arrayLength; }
58 	uint32 size() const { return arrayLength; }
59 	uint8 data[arrayLength];
60 };
61 
62 
63 /*! \brief \c char specialization of the \c array template struct. */
64 template<uint32 arrayLength>
65 struct array<char, arrayLength> {
66 	void dump() const
67 	{
68 		const uint8 bytesPerRow = 8;
69 		char classname[40];
70 		sprintf(classname, "array<uint8, %" B_PRIu32 ">", arrayLength);
71 
72 		DUMP_INIT(classname);
73 
74 		for (uint32 i = 0; i < arrayLength; i++) {
75 			if (i % bytesPerRow == 0)
76 				PRINT(("[%" B_PRIu32 ":%" B_PRIu32 "]: ",
77 					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