summaryrefslogtreecommitdiff
path: root/klm/util/stream/block.hh
blob: 11aa991e431ff8a15ec62e06e90c6297ce8b7548 (plain)
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
#ifndef UTIL_STREAM_BLOCK__
#define UTIL_STREAM_BLOCK__

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

namespace util {
namespace stream {

class Block {
  public:
    Block() : mem_(NULL), valid_size_(0) {}

    Block(void *mem, std::size_t size) : mem_(mem), valid_size_(size) {}

    void SetValidSize(std::size_t to) { valid_size_ = to; }
    // Read might fill in less than Allocated at EOF.   
    std::size_t ValidSize() const { return valid_size_; }

    void *Get() { return mem_; }
    const void *Get() const { return mem_; }

    const void *ValidEnd() const { 
      return reinterpret_cast<const uint8_t*>(mem_) + valid_size_;
    }

    operator bool() const { return mem_ != NULL; }
    bool operator!() const { return mem_ == NULL; }
 
  private:
    friend class Link;
    void SetToPoison() {
      mem_ = NULL;
    }

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

} // namespace stream
} // namespace util

#endif // UTIL_STREAM_BLOCK__