Welcome to mirror list, hosted at ThFree Co, Russian Federation.

byte_stream.hpp « coding - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9d145b6a4c499fedae832b71ae64551f76962ac9 (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
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
#pragma once
#include "base/base.hpp"

#include "std/vector.hpp"
#include "std/cstring.hpp"


class ArrayByteSource
{
public:
  explicit ArrayByteSource(const void * p) : m_p(static_cast<const unsigned char *>(p)) {}

  unsigned char ReadByte()
  {
    return *m_p++;
  }

  void Read(void * ptr, size_t size)
  {
    memcpy(ptr, m_p, size);
    m_p += size;
  }

  inline const void * Ptr() const { return m_p; }
  inline const unsigned char * PtrUC() const { return m_p; }
  inline const char * PtrC() const { return static_cast<char const *>(Ptr()); }

  void Advance(size_t size)
  {
    m_p += size;
  }

private:
  const unsigned char * m_p;
};

template <class StorageT> class PushBackByteSink
{
public:
  explicit PushBackByteSink(StorageT & storage)
    : m_Storage(storage)
  {
  }

  void Write(void const * p, size_t size)
  {
    // assume input buffer as buffer of bytes
    unsigned char const * pp = static_cast<unsigned char const *>(p);
    m_Storage.insert(m_Storage.end(), pp, pp + size);
  }

  size_t Pos() const
  {
    return m_Storage.size();
  }
private:
  StorageT & m_Storage;
};

class CountingSink
{
public:
  CountingSink() : m_Count(0) {}
  inline void Write(void const *, size_t size) { m_Count += size; }
  inline size_t GetCount() const { return m_Count; }
private:
  size_t m_Count;
};