src/misc/blob.hpp
author KUDr
Sat, 21 Apr 2007 08:23:57 +0000
branchcpp_gui
changeset 6308 646711c5feaa
parent 6304 38b7e46e2def
permissions -rw-r--r--
(svn r9708) [cpp_gui] -Sync with trunk (r9633:9707)
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     1
/* $Id$ */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     2
6308
646711c5feaa (svn r9708) [cpp_gui] -Sync with trunk (r9633:9707)
KUDr
parents: 6304
diff changeset
     3
/** @file blob.hpp */
646711c5feaa (svn r9708) [cpp_gui] -Sync with trunk (r9633:9707)
KUDr
parents: 6304
diff changeset
     4
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     5
#ifndef  BLOB_HPP
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     6
#define  BLOB_HPP
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     7
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
     8
/** Type-safe version of memcpy().
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
     9
 * @param d destination buffer
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    10
 * @param s source buffer
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    11
 * @param num_items number of items to be copied (!not number of bytes!) */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    12
template <class Titem_>
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    13
FORCEINLINE void MemCpyT(Titem_* d, const Titem_* s, int num_items = 1)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    14
{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    15
	memcpy(d, s, num_items * sizeof(Titem_));
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    16
}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    17
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    18
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    19
/** Base class for simple binary blobs.
4549
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
    20
 *  Item is byte.
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
    21
 *  The word 'simple' means:
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
    22
 *    - no configurable allocator type (always made from heap)
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
    23
 *    - no smart deallocation - deallocation must be called from the same
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
    24
 *        module (DLL) where the blob was allocated
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
    25
 *    - no configurable allocation policy (how big blocks should be allocated)
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
    26
 *    - no extra ownership policy (i.e. 'copy on write') when blob is copied
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    27
 *    - no thread synchronization at all
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    28
 *
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    29
 *  Internal member layout:
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    30
 *  1. The only class member is pointer to the first item (see union ptr_u).
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    31
 *  2. Allocated block contains the blob header (see CHdr) followed by the raw byte data.
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    32
 *     Always, when it allocates memory the allocated size is:
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    33
 *                                                      sizeof(CHdr) + <data capacity>
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    34
 *  3. Two 'virtual' members (m_size and m_max_size) are stored in the CHdr at beginning
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    35
 *     of the alloated block.
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    36
 *  4. The pointer (in ptr_u) points behind the header (to the first data byte).
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    37
 *     When memory block is allocated, the sizeof(CHdr) it added to it.
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    38
 *  5. Benefits of this layout:
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    39
 *     - items are accessed in the simplest possible way - just dereferencing the pointer,
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    40
 *       which is good for performance (assuming that data are accessed most often).
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    41
 *     - sizeof(blob) is the same as the size of any other pointer
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    42
 *  6. Drawbacks of this layout:
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    43
 *     - the fact, that pointer to the alocated block is adjusted by sizeof(CHdr) before
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    44
 *       it is stored can lead to several confusions:
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    45
 *         - it is not common pattern so the implementation code is bit harder to read
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    46
 *         - valgrind can generate warning that allocated block is lost (not accessible)
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    47
 * */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    48
class CBlobBaseSimple {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    49
protected:
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    50
	/** header of the allocated memory block */
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 3972
diff changeset
    51
	struct CHdr {
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    52
		int    m_size;      ///< actual blob size in bytes
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    53
		int    m_max_size;  ///< maximum (allocated) size in bytes
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    54
	};
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    55
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    56
	/** type used as class member */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    57
	union {
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    58
		int8   *m_pData;    ///< pointer to the first byte of data
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    59
		CHdr   *m_pHdr_1;   ///< pointer just after the CHdr holding m_size and m_max_size
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    60
	} ptr_u;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    61
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    62
public:
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    63
	static const int Ttail_reserve = 4; ///< four extra bytes will be always allocated and zeroed at the end
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    64
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    65
	/** default constructor - initializes empty blob */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    66
	FORCEINLINE CBlobBaseSimple() { InitEmpty(); }
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    67
	/** copy constructor */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    68
	FORCEINLINE CBlobBaseSimple(const CBlobBaseSimple& src)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    69
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    70
		InitEmpty();
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    71
		AppendRaw(src);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    72
	}
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    73
	/** destructor */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    74
	FORCEINLINE ~CBlobBaseSimple() { Free(); }
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    75
protected:
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    76
	/** initialize the empty blob by setting the ptr_u.m_pHdr_1 pointer to the static CHdr with
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    77
	 *  both m_size and m_max_size containing zero */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    78
	FORCEINLINE void InitEmpty() { static CHdr hdrEmpty[] = {{0, 0}, {0, 0}}; ptr_u.m_pHdr_1 = &hdrEmpty[1]; }
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    79
	/** initialize blob by attaching it to the given header followed by data */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    80
	FORCEINLINE void Init(CHdr* hdr) { ptr_u.m_pHdr_1 = &hdr[1]; }
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    81
	/** blob header accessor - use it rather than using the pointer arithmetics directly - non-const version */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    82
	FORCEINLINE CHdr& Hdr() { return ptr_u.m_pHdr_1[-1]; }
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    83
	/** blob header accessor - use it rather than using the pointer arithmetics directly - const version */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    84
	FORCEINLINE const CHdr& Hdr() const { return ptr_u.m_pHdr_1[-1]; }
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    85
	/** return reference to the actual blob size - used when the size needs to be modified */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    86
	FORCEINLINE int& RawSizeRef() { return Hdr().m_size; };
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    87
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    88
public:
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    89
	/** return true if blob doesn't contain valid data */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    90
	FORCEINLINE bool IsEmpty() const { return RawSize() == 0; }
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    91
	/** return the number of valid data bytes in the blob */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    92
	FORCEINLINE int RawSize() const { return Hdr().m_size; };
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    93
	/** return the current blob capacity in bytes */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    94
	FORCEINLINE int MaxRawSize() const { return Hdr().m_max_size; };
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    95
	/** return pointer to the first byte of data - non-const version */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    96
	FORCEINLINE int8* RawData() { return ptr_u.m_pData; }
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
    97
	/** return pointer to the first byte of data - const version */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    98
	FORCEINLINE const int8* RawData() const { return ptr_u.m_pData; }
5093
40714fcd331b (svn r7162) -CodeChange: disabled compilation of unused code (Darkvater)
KUDr
parents: 5082
diff changeset
    99
#if 0 // reenable when needed
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   100
	/** return the 32 bit CRC of valid data in the blob */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   101
	FORCEINLINE uint32 Crc32() const {return CCrc32::Calc(RawData(), RawSize());}
5093
40714fcd331b (svn r7162) -CodeChange: disabled compilation of unused code (Darkvater)
KUDr
parents: 5082
diff changeset
   102
#endif //0
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   103
	/** invalidate blob's data - doesn't free buffer */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   104
	FORCEINLINE void Clear() { RawSizeRef() = 0; }
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   105
	/** free the blob's memory */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   106
	FORCEINLINE void Free() { if (MaxRawSize() > 0) {RawFree(&Hdr()); InitEmpty();} }
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   107
	/** copy data from another blob - replaces any existing blob's data */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   108
	FORCEINLINE void CopyFrom(const CBlobBaseSimple& src) { Clear(); AppendRaw(src); }
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   109
	/** overtake ownership of data buffer from the source blob - source blob will become empty */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   110
	FORCEINLINE void MoveFrom(CBlobBaseSimple& src) { Free(); ptr_u.m_pData = src.ptr_u.m_pData; src.InitEmpty(); }
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   111
	/** swap buffers (with data) between two blobs (this and source blob) */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   112
	FORCEINLINE void Swap(CBlobBaseSimple& src) { int8 *tmp = ptr_u.m_pData; ptr_u.m_pData = src.ptr_u.m_pData; src.ptr_u.m_pData = tmp; }
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   113
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   114
	/** append new bytes at the end of existing data bytes - reallocates if necessary */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   115
	FORCEINLINE void AppendRaw(int8 *p, int num_bytes)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   116
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   117
		assert(p != NULL);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   118
		if (num_bytes > 0) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   119
			memcpy(GrowRawSize(num_bytes), p, num_bytes);
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 3972
diff changeset
   120
		} else {
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   121
			assert(num_bytes >= 0);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   122
		}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   123
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   124
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   125
	/** append bytes from given source blob to the end of existing data bytes - reallocates if necessary */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   126
	FORCEINLINE void AppendRaw(const CBlobBaseSimple& src)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   127
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   128
		if (!src.IsEmpty())
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   129
			memcpy(GrowRawSize(src.RawSize()), src.RawData(), src.RawSize());
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   130
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   131
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   132
	/** Reallocate if there is no free space for num_bytes bytes.
4549
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   133
	 *  @return pointer to the new data to be added */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   134
	FORCEINLINE int8* MakeRawFreeSpace(int num_bytes)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   135
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   136
		assert(num_bytes >= 0);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   137
		int new_size = RawSize() + num_bytes;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   138
		if (new_size > MaxRawSize()) SmartAlloc(new_size);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   139
		FixTail();
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   140
		return ptr_u.m_pData + RawSize();
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   141
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   142
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   143
	/** Increase RawSize() by num_bytes.
4549
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
   144
	 *  @return pointer to the new data added */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   145
	FORCEINLINE int8* GrowRawSize(int num_bytes)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   146
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   147
		int8* pNewData = MakeRawFreeSpace(num_bytes);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   148
		RawSizeRef() += num_bytes;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   149
		return pNewData;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   150
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   151
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   152
	/** Decrease RawSize() by num_bytes. */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   153
	FORCEINLINE void ReduceRawSize(int num_bytes)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   154
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   155
		if (MaxRawSize() > 0 && num_bytes > 0) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   156
			assert(num_bytes <= RawSize());
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   157
			if (num_bytes < RawSize()) RawSizeRef() -= num_bytes;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   158
			else RawSizeRef() = 0;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   159
		}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   160
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   161
	/** reallocate blob data if needed */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   162
	void SmartAlloc(int new_size)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   163
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   164
		int old_max_size = MaxRawSize();
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   165
		if (old_max_size >= new_size) return;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   166
		// calculate minimum block size we need to allocate
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   167
		int min_alloc_size = sizeof(CHdr) + new_size + Ttail_reserve;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   168
		// ask allocation policy for some reasonable block size
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   169
		int alloc_size = AllocPolicy(min_alloc_size);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   170
		// allocate new block
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 3972
diff changeset
   171
		CHdr* pNewHdr = RawAlloc(alloc_size);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   172
		// setup header
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   173
		pNewHdr->m_size = RawSize();
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   174
		pNewHdr->m_max_size = alloc_size - (sizeof(CHdr) + Ttail_reserve);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   175
		// copy existing data
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   176
		if (RawSize() > 0)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   177
			memcpy(pNewHdr + 1, ptr_u.m_pData, pNewHdr->m_size);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   178
		// replace our block with new one
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   179
		CHdr* pOldHdr = &Hdr();
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   180
		Init(pNewHdr);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   181
		if (old_max_size > 0)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   182
			RawFree(pOldHdr);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   183
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   184
	/** simple allocation policy - can be optimized later */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   185
	FORCEINLINE static int AllocPolicy(int min_alloc)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   186
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   187
		if (min_alloc < (1 << 9)) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   188
			if (min_alloc < (1 << 5)) return (1 << 5);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   189
			return (min_alloc < (1 << 7)) ? (1 << 7) : (1 << 9);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   190
		}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   191
		if (min_alloc < (1 << 15)) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   192
			if (min_alloc < (1 << 11)) return (1 << 11);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   193
			return (min_alloc < (1 << 13)) ? (1 << 13) : (1 << 15);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   194
		}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   195
		if (min_alloc < (1 << 20)) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   196
			if (min_alloc < (1 << 17)) return (1 << 17);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   197
			return (min_alloc < (1 << 19)) ? (1 << 19) : (1 << 20);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   198
		}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   199
		min_alloc = (min_alloc | ((1 << 20) - 1)) + 1;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   200
		return min_alloc;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   201
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   202
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   203
	/** all allocation should happen here */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   204
	static FORCEINLINE CHdr* RawAlloc(int num_bytes) { return (CHdr*)malloc(num_bytes); }
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   205
	/** all deallocations should happen here */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   206
	static FORCEINLINE void RawFree(CHdr* p) { free(p); }
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   207
	/** fixing the four bytes at the end of blob data - useful when blob is used to hold string */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   208
	FORCEINLINE void FixTail()
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   209
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   210
		if (MaxRawSize() > 0) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   211
			int8 *p = &ptr_u.m_pData[RawSize()];
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   212
			for (int i = 0; i < Ttail_reserve; i++) p[i] = 0;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   213
		}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   214
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   215
};
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   216
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   217
/** Blob - simple dynamic Titem_ array. Titem_ (template argument) is a placeholder for any type.
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   218
 *  Titem_ can be any integral type, pointer, or structure. Using Blob instead of just plain C array
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   219
 *  simplifies the resource management in several ways:
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   220
 *  1. When adding new item(s) it automatically grows capacity if needed.
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   221
 *  2. When variable of type Blob comes out of scope it automatically frees the data buffer.
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   222
 *  3. Takes care about the actual data size (number of used items).
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   223
 *  4. Dynamically constructs only used items (as opposite of static array which constructs all items) */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   224
template <class Titem_, class Tbase_ = CBlobBaseSimple>
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   225
class CBlobT : public CBlobBaseSimple {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   226
	// make template arguments public:
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   227
public:
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   228
	typedef Titem_ Titem;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   229
	typedef Tbase_ Tbase;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   230
5082
f5c258d60562 (svn r7146) -CodeChange: ST_CONST macro removed as it is no longer needed (Tron)
KUDr
parents: 5081
diff changeset
   231
	static const int Titem_size = sizeof(Titem);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   232
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   233
	/** Default constructor - makes new Blob ready to accept any data */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   234
	FORCEINLINE CBlobT() : Tbase() {}
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   235
	/** Copy constructor - make new blob to become copy of the original (source) blob */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   236
	FORCEINLINE CBlobT(const Tbase& src) : Tbase(src) {assert((RawSize() % Titem_size) == 0);}
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   237
	/** Destructor - ensures that allocated memory (if any) is freed */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   238
	FORCEINLINE ~CBlobT() { Free(); }
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   239
	/** Check the validity of item index (only in debug mode) */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   240
	FORCEINLINE void CheckIdx(int idx) { assert(idx >= 0); assert(idx < Size()); }
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   241
	/** Return pointer to the first data item - non-const version */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   242
	FORCEINLINE Titem* Data() { return (Titem*)RawData(); }
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   243
	/** Return pointer to the first data item - const version */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   244
	FORCEINLINE const Titem* Data() const { return (const Titem*)RawData(); }
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   245
	/** Return pointer to the idx-th data item - non-const version */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   246
	FORCEINLINE Titem* Data(int idx) { CheckIdx(idx); return (Data() + idx); }
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   247
	/** Return pointer to the idx-th data item - const version */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   248
	FORCEINLINE const Titem* Data(int idx) const { CheckIdx(idx); return (Data() + idx); }
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   249
	/** Return number of items in the Blob */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   250
	FORCEINLINE int Size() const { return (RawSize() / Titem_size); }
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   251
	/** Free the memory occupied by Blob destroying all items */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   252
	FORCEINLINE void Free()
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   253
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   254
		assert((RawSize() % Titem_size) == 0);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   255
		int old_size = Size();
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   256
		if (old_size > 0) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   257
			// destroy removed items;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   258
			Titem* pI_last_to_destroy = Data(0);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   259
			for (Titem* pI = Data(old_size - 1); pI >= pI_last_to_destroy; pI--) pI->~Titem_();
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   260
		}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   261
		Tbase::Free();
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   262
	}
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   263
	/** Grow number of data items in Blob by given number - doesn't construct items */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   264
	FORCEINLINE Titem* GrowSizeNC(int num_items) { return (Titem*)GrowRawSize(num_items * Titem_size); }
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   265
	/** Grow number of data items in Blob by given number - constructs new items (using Titem_'s default constructor) */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   266
	FORCEINLINE Titem* GrowSizeC(int num_items)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   267
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   268
		Titem* pI = GrowSizeNC(num_items);
6304
38b7e46e2def (svn r9635) [cpp_gui] -Codechange: use proper array item pointer arithmetics (pointer + index) instead of &pointer[index] since they can differ in C++
KUDr
parents: 6299
diff changeset
   269
		for (int i = 0; i < num_items; ++i) new (pI + i) Titem();
6299
7892983044e6 (svn r9482) [cpp_gui] -Fix: missing return value
KUDr
parents: 5884
diff changeset
   270
		return pI;
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   271
	}
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   272
	/** Destroy given number of items and reduce the Blob's data size */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   273
	FORCEINLINE void ReduceSize(int num_items)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   274
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   275
		assert((RawSize() % Titem_size) == 0);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   276
		int old_size = Size();
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   277
		assert(num_items <= old_size);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   278
		int new_size = (num_items <= old_size) ? (old_size - num_items) : 0;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   279
		// destroy removed items;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   280
		Titem* pI_last_to_destroy = Data(new_size);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   281
		for (Titem* pI = Data(old_size - 1); pI >= pI_last_to_destroy; pI--) pI->~Titem();
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   282
		// remove them
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   283
		ReduceRawSize(num_items * Titem_size);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   284
	}
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   285
	/** Append one data item at the end (calls Titem_'s default constructor) */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   286
	FORCEINLINE Titem* AppendNew()
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   287
	{
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   288
		Titem& dst = *GrowSizeNC(1); // Grow size by one item
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   289
		Titem* pNewItem = new (&dst) Titem(); // construct the new item by calling in-place new operator
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   290
		return pNewItem;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   291
	}
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   292
	/** Append the copy of given item at the end of Blob (using copy constructor) */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   293
	FORCEINLINE Titem* Append(const Titem& src)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   294
	{
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   295
		Titem& dst = *GrowSizeNC(1); // Grow size by one item
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   296
		Titem* pNewItem = new (&dst) Titem(src); // construct the new item by calling in-place new operator with copy ctor()
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   297
		return pNewItem;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   298
	}
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   299
	/** Add given items (ptr + number of items) at the end of blob */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   300
	FORCEINLINE Titem* Append(const Titem* pSrc, int num_items)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   301
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   302
		Titem* pDst = GrowSizeNC(num_items);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   303
		Titem* pDstOrg = pDst;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   304
		Titem* pDstEnd = pDst + num_items;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   305
		while (pDst < pDstEnd) new (pDst++) Titem(*(pSrc++));
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   306
		return pDstOrg;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   307
	}
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   308
	/** Remove item with the given index by replacing it by the last item and reducing the size by one */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   309
	FORCEINLINE void RemoveBySwap(int idx)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   310
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   311
		CheckIdx(idx);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   312
		// destroy removed item
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   313
		Titem* pRemoved = Data(idx);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   314
		RemoveBySwap(pRemoved);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   315
	}
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   316
	/** Remove item given by pointer replacing it by the last item and reducing the size by one */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   317
	FORCEINLINE void RemoveBySwap(Titem* pItem)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   318
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   319
		Titem* pLast = Data(Size() - 1);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   320
		assert(pItem >= Data() && pItem <= pLast);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   321
		// move last item to its new place
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   322
		if (pItem != pLast) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   323
			pItem->~Titem_();
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   324
			new (pItem) Titem_(*pLast);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   325
		}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   326
		// destroy the last item
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   327
		pLast->~Titem_();
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   328
		// and reduce the raw blob size
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   329
		ReduceRawSize(Titem_size);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   330
	}
5157
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   331
	/** Ensures that given number of items can be added to the end of Blob. Returns pointer to the
4ffb95a16b96 (svn r7255) -Comments added for CBlobBaseSimple, CBlobT classes
KUDr
parents: 5093
diff changeset
   332
	 *  first free (unused) item */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   333
	FORCEINLINE Titem* MakeFreeSpace(int num_items) { return (Titem*)MakeRawFreeSpace(num_items * Titem_size); }
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   334
};
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   335
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   336
// simple string implementation
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   337
struct CStrA : public CBlobT<char>
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   338
{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   339
	typedef CBlobT<char> base;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   340
	CStrA(const char* str = NULL) {Append(str);}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   341
	FORCEINLINE CStrA(const CBlobBaseSimple& src) : base(src) {}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   342
	void Append(const char* str) {if (str != NULL && str[0] != '\0') base::Append(str, (int)strlen(str));}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   343
};
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   344
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   345
#endif /* BLOB_HPP */