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 <string>
21 
22 namespace scip2
23 {
25 {
26 public:
27  uint64_t buf_;
28  uint32_t chars_;
29 
31  : buf_(0)
32  , chars_(0)
33  {
34  }
35  DecoderRemain(const uint64_t& buf, const uint32_t& chars)
36  : buf_(buf)
37  , chars_(chars)
38  {
39  }
40 };
41 
42 template <int L>
43 class Decoder
44 {
45 protected:
46  const std::string::const_iterator begin_;
47  const std::string::const_iterator end_;
49 
50 public:
51  class Iterator
52  {
53  protected:
54  std::string::const_iterator pos_;
55  std::string::const_iterator end_;
57 
58  public:
60  const std::string::const_iterator& pos,
61  const std::string::const_iterator& end,
62  const DecoderRemain& remain = DecoderRemain())
63  : pos_(pos)
64  , end_(end)
65  , remain_(remain)
66  {
67  }
68  bool operator==(const Iterator& it) const
69  {
70  return pos_ == it.pos_;
71  }
72  bool operator!=(const Iterator& it) const
73  {
74  return !operator==(it);
75  }
76  void operator++()
77  {
78  pos_ += L - remain_.chars_;
79  if (pos_ + L > end_)
80  {
81  remain_ = DecoderRemain(operator*(), end_ - pos_);
82  pos_ = end_;
83  }
84  else
85  {
86  remain_ = DecoderRemain();
87  }
88  }
90  {
91  return remain_;
92  }
93  const uint64_t operator*()
94  {
95  std::string::const_iterator pos(pos_);
96  uint64_t buf(remain_.buf_);
97  for (size_t i = 0; i < L - remain_.chars_; ++i)
98  {
99  if (pos == end_)
100  break;
101  buf = buf << 6;
102  buf |= (static_cast<uint8_t>(*pos) - 0x30);
103  ++pos;
104  }
105  return buf;
106  }
107  };
108 
109  explicit Decoder(const std::string& line, const DecoderRemain& remain = DecoderRemain())
110  : begin_(line.begin())
111  , end_(line.end())
112  , remain_(remain)
113  {
114  }
116  {
117  return Iterator(begin_, end_, remain_);
118  }
120  {
121  return Iterator(end_, end_);
122  }
123  uint8_t getChecksum() const
124  {
125  uint8_t sum = 0;
126  for (std::string::const_iterator it = begin_; it != end_; ++it)
127  {
128  sum += *it;
129  }
130  return sum;
131  }
132 };
133 } // namespace scip2
134 
135 #endif // SCIP2_DECODE_H
uint8_t getChecksum() const
Definition: decode.h:123
const std::string::const_iterator begin_
Definition: decode.h:46
bool operator!=(const Iterator &it) const
Definition: decode.h:72
std::string::const_iterator pos_
Definition: decode.h:54
bool operator==(const Iterator &it) const
Definition: decode.h:68
DecoderRemain remain_
Definition: decode.h:56
std::string::const_iterator end_
Definition: decode.h:55
const DecoderRemain remain_
Definition: decode.h:48
uint32_t chars_
Definition: decode.h:28
Iterator end()
Definition: decode.h:119
DecoderRemain(const uint64_t &buf, const uint32_t &chars)
Definition: decode.h:35
uint64_t buf_
Definition: decode.h:27
Decoder(const std::string &line, const DecoderRemain &remain=DecoderRemain())
Definition: decode.h:109
const std::string::const_iterator end_
Definition: decode.h:47
DecoderRemain getRemain() const
Definition: decode.h:89
const uint64_t operator*()
Definition: decode.h:93
Iterator begin()
Definition: decode.h:115
Iterator(const std::string::const_iterator &pos, const std::string::const_iterator &end, const DecoderRemain &remain=DecoderRemain())
Definition: decode.h:59


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Tue May 11 2021 02:14:05