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