File size: 2,403 Bytes
1ce325b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#ifndef UTIL_STREAM_BLOCK_H
#define UTIL_STREAM_BLOCK_H

#include <cstddef>
#include <stdint.h>

namespace util {
namespace stream {

/**
 * Encapsulates a block of memory.
 */
class Block {
  public:
  
    /** 
     * Constructs an empty block. 
     */
    Block() : mem_(NULL), valid_size_(0) {}

    /** 
     * Constructs a block that encapsulates a segment of memory.
     *
     * @param[in] mem  The segment of memory to encapsulate
     * @param[in] size The size of the memory segment in bytes
     */
    Block(void *mem, std::size_t size) : mem_(mem), valid_size_(size) {}

    /**
     * Set the number of bytes in this block that should be interpreted as valid.
     *
     * @param[in] to Number of bytes
     */
    void SetValidSize(std::size_t to) { valid_size_ = to; }

    /** 
     * Gets the number of bytes in this block that should be interpreted as valid.
     * This is important because read might fill in less than Allocated at EOF. 
     */
    std::size_t ValidSize() const { return valid_size_; }

    /** Gets a void pointer to the memory underlying this block. */
    void *Get() { return mem_; }

    /** Gets a const void pointer to the memory underlying this block. */
    const void *Get() const { return mem_; }

  
    /**
     * Gets a const void pointer to the end of the valid section of memory
     * encapsulated by this block.
     */
    const void *ValidEnd() const { 
      return reinterpret_cast<const uint8_t*>(mem_) + valid_size_;
    }

    /**
     * Returns true if this block encapsulates a valid (non-NULL) block of memory.
     * 
     * This method is a user-defined implicit conversion function to boolean;
     * among other things, this method enables bare instances of this class 
     * to be used as the condition of an if statement.
     */
    operator bool() const { return mem_ != NULL; }
  
    /**
     * Returns true if this block is empty.
     * 
     * In other words, if Get()==NULL, this method will return true.
     */
    bool operator!() const { return mem_ == NULL; }
 
  private:
    friend class Link;
  
    /**
     * Points this block's memory at NULL.
     *
     * This class defines poison as a block whose memory pointer is NULL.
     */
    void SetToPoison() {
      mem_ = NULL;
    }

    void *mem_;
    std::size_t valid_size_;
};

} // namespace stream
} // namespace util

#endif // UTIL_STREAM_BLOCK_H