xref: /haiku/src/add-ons/kernel/file_systems/udf/Array.h (revision d06cbe081b7ea043aea2012359744091de6d604d)
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:
21 	void dump() const {
22 		for (uint32 i = 0; i < arrayLength; i++)
23 			data[i].print();
24 	}
25 
26 	uint32 length() const { return arrayLength; }
27 	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, %ld>", arrayLength);
44 
45 		DUMP_INIT(classname);
46 
47 		for (uint32 i = 0; i < arrayLength; i++) {
48 			if (i % bytesPerRow == 0)
49 				PRINT(("[%ld:%ld]: ", i, i + bytesPerRow - 1));
50 			SIMPLE_PRINT(("0x%.2x ", data[i]));
51 			if ((i + 1) % bytesPerRow == 0 || i + 1 == arrayLength)
52 				SIMPLE_PRINT(("\n"));
53 		}
54 	}
55 
56 	uint32 length() const { return arrayLength; }
57 	uint32 size() const { return arrayLength; }
58 	uint8 data[arrayLength];
59 };
60 
61 
62 /*! \brief \c char specialization of the \c array template struct. */
63 template<uint32 arrayLength>
64 struct array<char, arrayLength> {
65 	void dump() const
66 	{
67 		const uint8 bytesPerRow = 8;
68 		char classname[40];
69 		sprintf(classname, "array<uint8, %ld>", arrayLength);
70 
71 		DUMP_INIT(classname);
72 
73 		for (uint32 i = 0; i < arrayLength; i++) {
74 			if (i % bytesPerRow == 0)
75 				PRINT(("[%ld:%ld]: ", i, i + bytesPerRow - 1));
76 			SIMPLE_PRINT(("0x%.2x ", data[i]));
77 			if ((i + 1) % bytesPerRow == 0 || i + 1 == arrayLength)
78 				SIMPLE_PRINT(("\n"));
79 		}
80 	}
81 
82 	uint32 length() const { return arrayLength; }
83 	uint32 size() const { return arrayLength; }
84 	uint8 data[arrayLength];
85 };
86 
87 #endif	// _UDF_ARRAY_H
88