decode.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018 The urg_stamped Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SCIP2_DECODE_H
18 #define SCIP2_DECODE_H
19 
20 #include <cstdint>
21 #include <string>
22 
23 namespace scip2
24 {
26 {
27 public:
28  uint64_t buf_;
29  uint32_t chars_;
30 
32  : buf_(0)
33  , chars_(0)
34  {
35  }
36  DecoderRemain(const uint64_t& buf, const uint32_t& chars)
37  : buf_(buf)
38  , chars_(chars)
39  {
40  }
41 };
42 
43 template <int L>
44 class Decoder
45 {
46 protected:
47  const std::string::const_iterator begin_;
48  const std::string::const_iterator end_;
50 
51 public:
52  class Iterator
53  {
54  protected:
55  std::string::const_iterator pos_;
56  std::string::const_iterator end_;
58 
59  public:
61  const std::string::const_iterator& pos,
62  const std::string::const_iterator& end,
63  const DecoderRemain& remain = DecoderRemain())
64  : pos_(pos)
65  , end_(end)
66  , remain_(remain)
67  {
68  }
69  bool operator==(const Iterator& it) const
70  {
71  return pos_ == it.pos_;
72  }
73  bool operator!=(const Iterator& it) const
74  {
75  return !operator==(it);
76  }
77  void operator++()
78  {
79  pos_ += L - remain_.chars_;
80  if (pos_ + L > end_)
81  {
82  remain_ = DecoderRemain(operator*(), end_ - pos_);
83  pos_ = end_;
84  }
85  else
86  {
88  }
89  }
91  {
92  return remain_;
93  }
94  const uint64_t operator*()
95  {
96  std::string::const_iterator pos(pos_);
97  uint64_t buf(remain_.buf_);
98  for (size_t i = 0; i < L - remain_.chars_; ++i)
99  {
100  if (pos == end_)
101  break;
102  buf = buf << 6;
103  buf |= (static_cast<uint8_t>(*pos) - 0x30);
104  ++pos;
105  }
106  return buf;
107  }
108  };
109 
110  explicit Decoder(const std::string& line, const DecoderRemain& remain = DecoderRemain())
111  : begin_(line.begin())
112  , end_(line.end())
113  , remain_(remain)
114  {
115  }
117  {
118  return Iterator(begin_, end_, remain_);
119  }
121  {
122  return Iterator(end_, end_);
123  }
124  uint8_t getChecksum() const
125  {
126  uint8_t sum = 0;
127  for (std::string::const_iterator it = begin_; it != end_; ++it)
128  {
129  sum += *it;
130  }
131  return sum;
132  }
133 };
134 } // namespace scip2
135 
136 #endif // SCIP2_DECODE_H
scip2::Decoder::Iterator
Definition: decode.h:52
scip2::DecoderRemain::buf_
uint64_t buf_
Definition: decode.h:28
scip2::DecoderRemain::DecoderRemain
DecoderRemain(const uint64_t &buf, const uint32_t &chars)
Definition: decode.h:36
scip2::Decoder::Iterator::remain_
DecoderRemain remain_
Definition: decode.h:57
scip2::DecoderRemain::chars_
uint32_t chars_
Definition: decode.h:29
scip2::DecoderRemain::DecoderRemain
DecoderRemain()
Definition: decode.h:31
scip2::Decoder::Iterator::getRemain
DecoderRemain getRemain() const
Definition: decode.h:90
scip2
Definition: connection.h:30
scip2::Decoder::getChecksum
uint8_t getChecksum() const
Definition: decode.h:124
scip2::Decoder::Iterator::pos_
std::string::const_iterator pos_
Definition: decode.h:55
scip2::DecoderRemain
Definition: decode.h:25
scip2::Decoder::Iterator::operator++
void operator++()
Definition: decode.h:77
scip2::Decoder
Definition: decode.h:44
scip2::Decoder::end
Iterator end()
Definition: decode.h:120
scip2::Decoder::remain_
const DecoderRemain remain_
Definition: decode.h:49
scip2::Decoder::Iterator::operator!=
bool operator!=(const Iterator &it) const
Definition: decode.h:73
scip2::Decoder::Iterator::operator==
bool operator==(const Iterator &it) const
Definition: decode.h:69
scip2::Decoder::Iterator::operator*
const uint64_t operator*()
Definition: decode.h:94
scip2::Decoder::Iterator::end_
std::string::const_iterator end_
Definition: decode.h:56
scip2::Decoder::end_
const std::string::const_iterator end_
Definition: decode.h:48
scip2::Decoder::Iterator::Iterator
Iterator(const std::string::const_iterator &pos, const std::string::const_iterator &end, const DecoderRemain &remain=DecoderRemain())
Definition: decode.h:60
scip2::Decoder::begin_
const std::string::const_iterator begin_
Definition: decode.h:47
scip2::Decoder::Decoder
Decoder(const std::string &line, const DecoderRemain &remain=DecoderRemain())
Definition: decode.h:110
scip2::Decoder::begin
Iterator begin()
Definition: decode.h:116


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Wed Dec 18 2024 03:10:57