Mercurial > hg > openttd
annotate src/misc/blob.hpp @ 10831:31f766ee053b draft
(svn r15166) -Codechange: reduce number of includes in afterload.cpp
author | smatz <smatz@openttd.org> |
---|---|
date | Tue, 20 Jan 2009 13:56:35 +0000 |
parents | 592ae9307430 |
children | 02313cc6114e |
rev | line source |
---|---|
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
1 /* $Id$ */ |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
2 |
9111
d48433370037
(svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium <rubidium@openttd.org>
parents:
8130
diff
changeset
|
3 /** @file blob.hpp Support for storing random binary data. */ |
6481
85a1a79387a2
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas <belugas@openttd.org>
parents:
5633
diff
changeset
|
4 |
8130
0586823afe39
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium <rubidium@openttd.org>
parents:
8037
diff
changeset
|
5 #ifndef BLOB_HPP |
0586823afe39
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium <rubidium@openttd.org>
parents:
8037
diff
changeset
|
6 #define BLOB_HPP |
0586823afe39
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium <rubidium@openttd.org>
parents:
8037
diff
changeset
|
7 |
0586823afe39
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium <rubidium@openttd.org>
parents:
8037
diff
changeset
|
8 #include "../core/alloc_func.hpp" |
9520
7193d782cc34
(svn r13516) -Codechange: Move MemCpyT to a fitting core header
skidd13 <skidd13@openttd.org>
parents:
9135
diff
changeset
|
9 #include "../core/mem_func.hpp" |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
10 |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
11 /** Base class for simple binary blobs. |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
12 * Item is byte. |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
13 * The word 'simple' means: |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
14 * - no configurable allocator type (always made from heap) |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
15 * - no smart deallocation - deallocation must be called from the same |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
16 * module (DLL) where the blob was allocated |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
17 * - no configurable allocation policy (how big blocks should be allocated) |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
18 * - no extra ownership policy (i.e. 'copy on write') when blob is copied |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
19 * - no thread synchronization at all |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
20 * |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
21 * Internal member layout: |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
22 * 1. The only class member is pointer to the first item (see union ptr_u). |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
23 * 2. Allocated block contains the blob header (see CHdr) followed by the raw byte data. |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
24 * Always, when it allocates memory the allocated size is: |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
25 * sizeof(CHdr) + <data capacity> |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
26 * 3. Two 'virtual' members (m_size and m_max_size) are stored in the CHdr at beginning |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
27 * of the alloated block. |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
28 * 4. The pointter (in ptr_u) pobsize_ts behind the header (to the first data byte). |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
29 * When memory block is allocated, the sizeof(CHdr) it added to it. |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
30 * 5. Benefits of this layout: |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
31 * - items are accessed in the simplest possible way - just dereferencing the pointer, |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
32 * which is good for performance (assuming that data are accessed most often). |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
33 * - sizeof(blob) is the same as the size of any other pointer |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
34 * 6. Drawbacks of this layout: |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
35 * - the fact, that pointer to the alocated block is adjusted by sizeof(CHdr) before |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
36 * it is stored can lead to several confusions: |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
37 * - it is not common pattern so the implementation code is bit harder to read |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
38 * - valgrind can generate warning that allocated block is lost (not accessible) |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
39 * */ |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
40 class CBlobBaseSimple { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
41 public: |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
42 typedef ::ptrdiff_t bsize_t; |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
43 typedef ::byte bitem_t; |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
44 |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
45 protected: |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
46 /** header of the allocated memory block */ |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
47 struct CHdr { |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
48 bsize_t m_size; ///< actual blob size in bytes |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
49 bsize_t m_max_size; ///< maximum (allocated) size in bytes |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
50 }; |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
51 |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
52 /** type used as class member */ |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
53 union { |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
54 bitem_t *m_pData; ///< ptr to the first byte of data |
7255
8ab9b7eee23a
(svn r10562) -Fix: most of the MorphOS issues; MorphOS doesn't know about wchars, so disable all code that has to use wchars for MorphOS.
rubidium <rubidium@openttd.org>
parents:
7115
diff
changeset
|
55 #if defined(HAS_WCHAR) |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
56 wchar_t *m_pwData; ///< ptr to the first byte of data |
7255
8ab9b7eee23a
(svn r10562) -Fix: most of the MorphOS issues; MorphOS doesn't know about wchars, so disable all code that has to use wchars for MorphOS.
rubidium <rubidium@openttd.org>
parents:
7115
diff
changeset
|
57 #endif /* HAS_WCHAR */ |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
58 CHdr *m_pHdr_1; ///< ptr just after the CHdr holding m_size and m_max_size |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
59 } ptr_u; |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
60 |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
61 public: |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
62 static const bsize_t Ttail_reserve = 4; ///< four extra bytes will be always allocated and zeroed at the end |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
63 |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
64 /** default constructor - initializes empty blob */ |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
65 FORCEINLINE CBlobBaseSimple() { InitEmpty(); } |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
66 /** constructor - create blob with data */ |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
67 FORCEINLINE CBlobBaseSimple(const bitem_t *p, bsize_t num_bytes) |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
68 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
69 InitEmpty(); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
70 AppendRaw(p, num_bytes); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
71 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
72 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
73 /** copy constructor */ |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
74 FORCEINLINE CBlobBaseSimple(const CBlobBaseSimple& src) |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
75 { |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
76 InitEmpty(); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
77 AppendRaw(src); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
78 } |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
79 |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
80 /** move constructor - take ownership of blob data */ |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
81 FORCEINLINE CBlobBaseSimple(CHdr * const & pHdr_1) |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
82 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
83 assert(pHdr_1 != NULL); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
84 ptr_u.m_pHdr_1 = pHdr_1; |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
85 *(CHdr**)&pHdr_1 = NULL; |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
86 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
87 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
88 /** destructor */ |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
89 FORCEINLINE ~CBlobBaseSimple() |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
90 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
91 Free(); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
92 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
93 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
94 protected: |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
95 /** initialize the empty blob by setting the ptr_u.m_pHdr_1 pointer to the static CHdr with |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
96 * both m_size and m_max_size containing zero */ |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
97 FORCEINLINE void InitEmpty() |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
98 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
99 static CHdr hdrEmpty[] = {{0, 0}, {0, 0}}; |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
100 ptr_u.m_pHdr_1 = &hdrEmpty[1]; |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
101 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
102 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
103 /** initialize blob by attaching it to the given header followed by data */ |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
104 FORCEINLINE void Init(CHdr *hdr) |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
105 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
106 ptr_u.m_pHdr_1 = &hdr[1]; |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
107 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
108 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
109 /** blob header accessor - use it rather than using the pointer arithmetics directly - non-const version */ |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
110 FORCEINLINE CHdr& Hdr() |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
111 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
112 return ptr_u.m_pHdr_1[-1]; |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
113 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
114 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
115 /** blob header accessor - use it rather than using the pointer arithmetics directly - const version */ |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
116 FORCEINLINE const CHdr& Hdr() const |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
117 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
118 return ptr_u.m_pHdr_1[-1]; |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
119 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
120 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
121 /** return reference to the actual blob size - used when the size needs to be modified */ |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
122 FORCEINLINE bsize_t& RawSizeRef() |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
123 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
124 return Hdr().m_size; |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
125 }; |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
126 |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
127 public: |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
128 /** return true if blob doesn't contain valid data */ |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
129 FORCEINLINE bool IsEmpty() const |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
130 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
131 return RawSize() == 0; |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
132 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
133 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
134 /** return the number of valid data bytes in the blob */ |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
135 FORCEINLINE bsize_t RawSize() const |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
136 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
137 return Hdr().m_size; |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
138 }; |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
139 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
140 /** return the current blob capacity in bytes */ |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
141 FORCEINLINE bsize_t MaxRawSize() const |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
142 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
143 return Hdr().m_max_size; |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
144 }; |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
145 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
146 /** return pointer to the first byte of data - non-const version */ |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
147 FORCEINLINE bitem_t *RawData() |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
148 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
149 return ptr_u.m_pData; |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
150 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
151 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
152 /** return pointer to the first byte of data - const version */ |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
153 FORCEINLINE const bitem_t *RawData() const |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
154 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
155 return ptr_u.m_pData; |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
156 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
157 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
158 /** return the 32 bit CRC of valid data in the blob */ |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
159 //FORCEINLINE bsize_t Crc32() const |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
160 //{ |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
161 // return CCrc32::Calc(RawData(), RawSize()); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
162 //} |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
163 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
164 /** invalidate blob's data - doesn't free buffer */ |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
165 FORCEINLINE void Clear() |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
166 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
167 RawSizeRef() = 0; |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
168 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
169 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
170 /** free the blob's memory */ |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
171 FORCEINLINE void Free() |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
172 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
173 if (MaxRawSize() > 0) { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
174 RawFree(&Hdr()); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
175 InitEmpty(); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
176 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
177 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
178 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
179 /** copy data from another blob - replaces any existing blob's data */ |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
180 FORCEINLINE void CopyFrom(const CBlobBaseSimple& src) |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
181 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
182 Clear(); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
183 AppendRaw(src); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
184 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
185 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
186 /** overtake ownership of data buffer from the source blob - source blob will become empty */ |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
187 FORCEINLINE void MoveFrom(CBlobBaseSimple& src) |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
188 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
189 Free(); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
190 ptr_u.m_pData = src.ptr_u.m_pData; |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
191 src.InitEmpty(); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
192 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
193 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
194 /** swap buffers (with data) between two blobs (this and source blob) */ |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
195 FORCEINLINE void Swap(CBlobBaseSimple& src) |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
196 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
197 bitem_t *tmp = ptr_u.m_pData; ptr_u.m_pData = src.ptr_u.m_pData; |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
198 src.ptr_u.m_pData = tmp; |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
199 } |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
200 |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
201 /** append new bytes at the end of existing data bytes - reallocates if necessary */ |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
202 FORCEINLINE void AppendRaw(const void *p, bsize_t num_bytes) |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
203 { |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
204 assert(p != NULL); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
205 if (num_bytes > 0) { |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
206 memcpy(GrowRawSize(num_bytes), p, num_bytes); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
207 } else { |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
208 assert(num_bytes >= 0); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
209 } |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
210 } |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
211 |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
212 /** append bytes from given source blob to the end of existing data bytes - reallocates if necessary */ |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
213 FORCEINLINE void AppendRaw(const CBlobBaseSimple& src) |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
214 { |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
215 if (!src.IsEmpty()) |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
216 memcpy(GrowRawSize(src.RawSize()), src.RawData(), src.RawSize()); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
217 } |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
218 |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
219 /** Reallocate if there is no free space for num_bytes bytes. |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
220 * @return pointer to the new data to be added */ |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
221 FORCEINLINE bitem_t *MakeRawFreeSpace(bsize_t num_bytes) |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
222 { |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
223 assert(num_bytes >= 0); |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
224 bsize_t new_size = RawSize() + num_bytes; |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
225 if (new_size > MaxRawSize()) SmartAlloc(new_size); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
226 return ptr_u.m_pData + RawSize(); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
227 } |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
228 |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
229 /** Increase RawSize() by num_bytes. |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
230 * @return pointer to the new data added */ |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
231 FORCEINLINE bitem_t *GrowRawSize(bsize_t num_bytes) |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
232 { |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
233 bitem_t *pNewData = MakeRawFreeSpace(num_bytes); |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
234 RawSizeRef() += num_bytes; |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
235 return pNewData; |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
236 } |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
237 |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
238 /** Decrease RawSize() by num_bytes. */ |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
239 FORCEINLINE void ReduceRawSize(bsize_t num_bytes) |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
240 { |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
241 if (MaxRawSize() > 0 && num_bytes > 0) { |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
242 assert(num_bytes <= RawSize()); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
243 if (num_bytes < RawSize()) RawSizeRef() -= num_bytes; |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
244 else RawSizeRef() = 0; |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
245 } |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
246 } |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
247 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
248 /** reallocate blob data if needed */ |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
249 void SmartAlloc(bsize_t new_size) |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
250 { |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
251 bsize_t old_max_size = MaxRawSize(); |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
252 if (old_max_size >= new_size) return; |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
253 // calculate minimum block size we need to allocate |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
254 bsize_t min_alloc_size = sizeof(CHdr) + new_size + Ttail_reserve; |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
255 // ask allocation policy for some reasonable block size |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
256 bsize_t alloc_size = AllocPolicy(min_alloc_size); |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
257 // allocate new block |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
258 CHdr *pNewHdr = RawAlloc(alloc_size); |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
259 // setup header |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
260 pNewHdr->m_size = RawSize(); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
261 pNewHdr->m_max_size = alloc_size - (sizeof(CHdr) + Ttail_reserve); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
262 // copy existing data |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
263 if (RawSize() > 0) |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
264 memcpy(pNewHdr + 1, ptr_u.m_pData, pNewHdr->m_size); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
265 // replace our block with new one |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
266 CHdr *pOldHdr = &Hdr(); |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
267 Init(pNewHdr); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
268 if (old_max_size > 0) |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
269 RawFree(pOldHdr); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
270 } |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
271 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
272 /** simple allocation policy - can be optimized later */ |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
273 FORCEINLINE static bsize_t AllocPolicy(bsize_t min_alloc) |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
274 { |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
275 if (min_alloc < (1 << 9)) { |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
276 if (min_alloc < (1 << 5)) return (1 << 5); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
277 return (min_alloc < (1 << 7)) ? (1 << 7) : (1 << 9); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
278 } |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
279 if (min_alloc < (1 << 15)) { |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
280 if (min_alloc < (1 << 11)) return (1 << 11); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
281 return (min_alloc < (1 << 13)) ? (1 << 13) : (1 << 15); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
282 } |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
283 if (min_alloc < (1 << 20)) { |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
284 if (min_alloc < (1 << 17)) return (1 << 17); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
285 return (min_alloc < (1 << 19)) ? (1 << 19) : (1 << 20); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
286 } |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
287 min_alloc = (min_alloc | ((1 << 20) - 1)) + 1; |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
288 return min_alloc; |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
289 } |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
290 |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
291 /** all allocation should happen here */ |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
292 static FORCEINLINE CHdr *RawAlloc(bsize_t num_bytes) |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
293 { |
8037
02252785b8d6
(svn r11597) -Change: replace all remaining instances of (re|m|c)alloc with (Re|M|C)allocT and add a check for out-of-memory situations to the *allocT functions.
rubidium <rubidium@openttd.org>
parents:
7255
diff
changeset
|
294 return (CHdr*)MallocT<byte>(num_bytes); |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
295 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
296 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
297 /** all deallocations should happen here */ |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
298 static FORCEINLINE void RawFree(CHdr *p) |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
299 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
300 free(p); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
301 } |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
302 /** fixing the four bytes at the end of blob data - useful when blob is used to hold string */ |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
303 FORCEINLINE void FixTail() const |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
304 { |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
305 if (MaxRawSize() > 0) { |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
306 bitem_t *p = &ptr_u.m_pData[RawSize()]; |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
307 for (bsize_t i = 0; i < Ttail_reserve; i++) { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
308 p[i] = 0; |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
309 } |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
310 } |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
311 } |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
312 }; |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
313 |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
314 /** Blob - simple dynamic Titem_ array. Titem_ (template argument) is a placeholder for any type. |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
315 * Titem_ can be any integral type, pointer, or structure. Using Blob instead of just plain C array |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
316 * simplifies the resource management in several ways: |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
317 * 1. When adding new item(s) it automatically grows capacity if needed. |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
318 * 2. When variable of type Blob comes out of scope it automatically frees the data buffer. |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
319 * 3. Takes care about the actual data size (number of used items). |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
320 * 4. Dynamically constructs only used items (as opposite of static array which constructs all items) */ |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
321 template <class Titem_, class Tbase_ = CBlobBaseSimple> |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
322 class CBlobT : public Tbase_ { |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
323 // make template arguments public: |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
324 public: |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
325 typedef Titem_ Titem; |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
326 typedef Tbase_ Tbase; |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
327 typedef typename Tbase::bsize_t bsize_t; |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
328 |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
329 static const bsize_t Titem_size = sizeof(Titem); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
330 |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
331 struct OnTransfer { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
332 typename Tbase_::CHdr *m_pHdr_1; |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
333 OnTransfer(const OnTransfer& src) : m_pHdr_1(src.m_pHdr_1) {assert(src.m_pHdr_1 != NULL); *(typename Tbase_::CHdr**)&src.m_pHdr_1 = NULL;} |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
334 OnTransfer(CBlobT& src) : m_pHdr_1(src.ptr_u.m_pHdr_1) {src.InitEmpty();} |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
335 ~OnTransfer() {assert(m_pHdr_1 == NULL);} |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
336 }; |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
337 |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
338 /** Default constructor - makes new Blob ready to accept any data */ |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
339 FORCEINLINE CBlobT() |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
340 : Tbase() |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
341 {} |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
342 |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
343 /** Constructor - makes new Blob with data */ |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
344 FORCEINLINE CBlobT(const Titem_ *p, bsize_t num_items) |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
345 : Tbase((typename Tbase_::bitem_t*)p, num_items * Titem_size) |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
346 {} |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
347 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
348 /** Copy constructor - make new blob to become copy of the original (source) blob */ |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
349 FORCEINLINE CBlobT(const Tbase& src) |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
350 : Tbase(src) |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
351 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
352 assert((Tbase::RawSize() % Titem_size) == 0); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
353 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
354 |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
355 /** Take ownership constructor */ |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
356 FORCEINLINE CBlobT(const OnTransfer& ot) |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
357 : Tbase(ot.m_pHdr_1) |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
358 {} |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
359 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
360 /** Destructor - ensures that allocated memory (if any) is freed */ |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
361 FORCEINLINE ~CBlobT() |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
362 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
363 Free(); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
364 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
365 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
366 /** Check the validity of item index (only in debug mode) */ |
9135
bec379423029
(svn r12995) -Codechange: use std::vector for EngineList instead of C/C++ wrapper for CBlobT
smatz <smatz@openttd.org>
parents:
9111
diff
changeset
|
367 FORCEINLINE void CheckIdx(bsize_t idx) const |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
368 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
369 assert(idx >= 0); assert(idx < Size()); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
370 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
371 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
372 /** Return pointer to the first data item - non-const version */ |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
373 FORCEINLINE Titem *Data() |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
374 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
375 return (Titem*)Tbase::RawData(); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
376 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
377 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
378 /** Return pointer to the first data item - const version */ |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
379 FORCEINLINE const Titem *Data() const |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
380 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
381 return (const Titem*)Tbase::RawData(); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
382 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
383 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
384 /** Return pointer to the idx-th data item - non-const version */ |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
385 FORCEINLINE Titem *Data(bsize_t idx) |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
386 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
387 CheckIdx(idx); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
388 return (Data() + idx); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
389 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
390 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
391 /** Return pointer to the idx-th data item - const version */ |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
392 FORCEINLINE const Titem *Data(bsize_t idx) const |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
393 { |
9135
bec379423029
(svn r12995) -Codechange: use std::vector for EngineList instead of C/C++ wrapper for CBlobT
smatz <smatz@openttd.org>
parents:
9111
diff
changeset
|
394 CheckIdx(idx); |
bec379423029
(svn r12995) -Codechange: use std::vector for EngineList instead of C/C++ wrapper for CBlobT
smatz <smatz@openttd.org>
parents:
9111
diff
changeset
|
395 return (Data() + idx); |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
396 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
397 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
398 /** Return number of items in the Blob */ |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
399 FORCEINLINE bsize_t Size() const |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
400 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
401 return (Tbase::RawSize() / Titem_size); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
402 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
403 |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
404 /** Return total number of items that can fit in the Blob without buffer reallocation */ |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
405 FORCEINLINE bsize_t MaxSize() const |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
406 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
407 return (Tbase::MaxRawSize() / Titem_size); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
408 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
409 /** Return number of additional items that can fit in the Blob without buffer reallocation */ |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
410 FORCEINLINE bsize_t GetReserve() const |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
411 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
412 return ((Tbase::MaxRawSize() - Tbase::RawSize()) / Titem_size); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
413 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
414 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
415 /** Free the memory occupied by Blob destroying all items */ |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
416 FORCEINLINE void Free() |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
417 { |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
418 assert((Tbase::RawSize() % Titem_size) == 0); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
419 bsize_t old_size = Size(); |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
420 if (old_size > 0) { |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
421 // destroy removed items; |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
422 Titem *pI_last_to_destroy = Data(0); |
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
423 for (Titem *pI = Data(old_size - 1); pI >= pI_last_to_destroy; pI--) pI->~Titem_(); |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
424 } |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
425 Tbase::Free(); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
426 } |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
427 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
428 /** Grow number of data items in Blob by given number - doesn't construct items */ |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
429 FORCEINLINE Titem *GrowSizeNC(bsize_t num_items) |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
430 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
431 return (Titem*)Tbase::GrowRawSize(num_items * Titem_size); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
432 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
433 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
434 /** Grow number of data items in Blob by given number - constructs new items (using Titem_'s default constructor) */ |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
435 FORCEINLINE Titem *GrowSizeC(bsize_t num_items) |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
436 { |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
437 Titem *pI = GrowSizeNC(num_items); |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
438 for (bsize_t i = num_items; i > 0; i--, pI++) new (pI) Titem(); |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
439 } |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
440 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
441 /** Destroy given number of items and reduce the Blob's data size */ |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
442 FORCEINLINE void ReduceSize(bsize_t num_items) |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
443 { |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
444 assert((Tbase::RawSize() % Titem_size) == 0); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
445 bsize_t old_size = Size(); |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
446 assert(num_items <= old_size); |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
447 bsize_t new_size = (num_items <= old_size) ? (old_size - num_items) : 0; |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
448 // destroy removed items; |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
449 Titem *pI_last_to_destroy = Data(new_size); |
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
450 for (Titem *pI = Data(old_size - 1); pI >= pI_last_to_destroy; pI--) pI->~Titem(); |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
451 // remove them |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
452 Tbase::ReduceRawSize(num_items * Titem_size); |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
453 } |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
454 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
455 /** Append one data item at the end (calls Titem_'s default constructor) */ |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
456 FORCEINLINE Titem *AppendNew() |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
457 { |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
458 Titem& dst = *GrowSizeNC(1); // Grow size by one item |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
459 Titem *pNewItem = new (&dst) Titem(); // construct the new item by calling in-place new operator |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
460 return pNewItem; |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
461 } |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
462 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
463 /** Append the copy of given item at the end of Blob (using copy constructor) */ |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
464 FORCEINLINE Titem *Append(const Titem& src) |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
465 { |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
466 Titem& dst = *GrowSizeNC(1); // Grow size by one item |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
467 Titem *pNewItem = new (&dst) Titem(src); // construct the new item by calling in-place new operator with copy ctor() |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
468 return pNewItem; |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
469 } |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
470 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
471 /** Add given items (ptr + number of items) at the end of blob */ |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
472 FORCEINLINE Titem *Append(const Titem *pSrc, bsize_t num_items) |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
473 { |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
474 Titem *pDst = GrowSizeNC(num_items); |
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
475 Titem *pDstOrg = pDst; |
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
476 Titem *pDstEnd = pDst + num_items; |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
477 while (pDst < pDstEnd) new (pDst++) Titem(*(pSrc++)); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
478 return pDstOrg; |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
479 } |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
480 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
481 /** Remove item with the given index by replacing it by the last item and reducing the size by one */ |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
482 FORCEINLINE void RemoveBySwap(bsize_t idx) |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
483 { |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
484 CheckIdx(idx); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
485 // destroy removed item |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
486 Titem *pRemoved = Data(idx); |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
487 RemoveBySwap(pRemoved); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
488 } |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
489 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
490 /** Remove item given by pointer replacing it by the last item and reducing the size by one */ |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
491 FORCEINLINE void RemoveBySwap(Titem *pItem) |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
492 { |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
493 Titem *pLast = Data(Size() - 1); |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
494 assert(pItem >= Data() && pItem <= pLast); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
495 // move last item to its new place |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
496 if (pItem != pLast) { |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
497 pItem->~Titem_(); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
498 new (pItem) Titem_(*pLast); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
499 } |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
500 // destroy the last item |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
501 pLast->~Titem_(); |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
502 // and reduce the raw blob size |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
503 Tbase::ReduceRawSize(Titem_size); |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
504 } |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
505 |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
506 /** Ensures that given number of items can be added to the end of Blob. Returns pointer to the |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
507 * first free (unused) item */ |
10647
592ae9307430
(svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents:
9520
diff
changeset
|
508 FORCEINLINE Titem *MakeFreeSpace(bsize_t num_items) |
7115
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
509 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
510 return (Titem*)Tbase::MakeRawFreeSpace(num_items * Titem_size); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
511 } |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
512 |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
513 FORCEINLINE OnTransfer Transfer() |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
514 { |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
515 return OnTransfer(*this); |
0fe66343663e
(svn r10388) -Cleanup: coding style (CBlobT & CBlobBaseSimple), removed CStrA
KUDr <KUDr@openttd.org>
parents:
6481
diff
changeset
|
516 }; |
5633
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
517 }; |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
518 |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
519 |
a9c31d5be476
(svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
520 #endif /* BLOB_HPP */ |