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

streams_sink.hpp « coding - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bf5c8699c820c50f43603d91fcbebd410145109c (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#pragma once

#include "coding/streams_common.hpp"
#include "coding/reader.hpp"
#include "coding/write_to_sink.hpp"

#include <cstdint>
#include <string>
#include <type_traits>

namespace stream
{
  template <class TReader> class SinkReaderStream
  {
    TReader & m_reader;

  public:
    SinkReaderStream(TReader & reader) : m_reader(reader) {}

    template <typename T>
    std::enable_if_t<std::is_integral<T>::value, SinkReaderStream &> operator>>(T & t)
    {
      t = ReadPrimitiveFromSource<T>(m_reader);
      return (*this);
    }

    SinkReaderStream & operator >> (bool & t)
    {
      detail::ReadBool(*this, t);
      return *this;
    }

    SinkReaderStream & operator >> (string & t)
    {
      detail::ReadString(*this, t);
      return *this;
    }

    SinkReaderStream & operator >> (double & t)
    {
      static_assert(sizeof(double) == sizeof(int64_t), "");
      int64_t * tInt = reinterpret_cast<int64_t *>(&t);
      operator >> (*tInt);
      return *this;
    }
  };

  template <class TWriter> class SinkWriterStream
  {
    TWriter & m_writer;

  public:
    SinkWriterStream(TWriter & writer) : m_writer(writer) {}

    template <typename T>
    std::enable_if_t<std::is_integral<T>::value, SinkWriterStream &> operator<<(T const & t)
    {
      WriteToSink(m_writer, t);
      return (*this);
    }

    SinkWriterStream & operator << (bool t)
    {
      detail::WriteBool(*this, t);
      return (*this);
    }

    SinkWriterStream & operator<<(std::string const & t)
    {
      detail::WriteString(*this, m_writer, t);
      return *this;
    }

    SinkWriterStream & operator << (double t)
    {
      static_assert(sizeof(double) == sizeof(int64_t), "");
      int64_t const tInt = *reinterpret_cast<int64_t const *>(&t);
      operator << (tInt);
      return (*this);
    }
  };
}