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