decode.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2018 The urg_stamped Authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef SCIP2_DECODE_H
00018 #define SCIP2_DECODE_H
00019 
00020 #include <string>
00021 
00022 namespace scip2
00023 {
00024 class DecoderRemain
00025 {
00026 public:
00027   uint64_t buf_;
00028   uint32_t chars_;
00029 
00030   DecoderRemain()
00031     : buf_(0)
00032     , chars_(0)
00033   {
00034   }
00035   DecoderRemain(const uint64_t &buf, const uint32_t &chars)
00036     : buf_(buf)
00037     , chars_(chars)
00038   {
00039   }
00040 };
00041 
00042 template <int L>
00043 class Decoder
00044 {
00045 protected:
00046   const std::string::const_iterator begin_;
00047   const std::string::const_iterator end_;
00048   const DecoderRemain remain_;
00049 
00050 public:
00051   class Iterator
00052   {
00053   protected:
00054     std::string::const_iterator pos_;
00055     std::string::const_iterator end_;
00056     DecoderRemain remain_;
00057 
00058   public:
00059     Iterator(
00060         const std::string::const_iterator &pos,
00061         const std::string::const_iterator &end,
00062         const DecoderRemain &remain = DecoderRemain())
00063       : pos_(pos)
00064       , end_(end)
00065       , remain_(remain)
00066     {
00067     }
00068     bool operator==(const Iterator &it) const
00069     {
00070       return pos_ == it.pos_;
00071     }
00072     bool operator!=(const Iterator &it) const
00073     {
00074       return !operator==(it);
00075     }
00076     void operator++()
00077     {
00078       pos_ += L - remain_.chars_;
00079       if (pos_ + L > end_)
00080       {
00081         remain_ = DecoderRemain(operator*(), end_ - pos_);
00082         pos_ = end_;
00083       }
00084       else
00085       {
00086         remain_ = DecoderRemain();
00087       }
00088     }
00089     DecoderRemain getRemain() const
00090     {
00091       return remain_;
00092     }
00093     const uint64_t operator*()
00094     {
00095       std::string::const_iterator pos(pos_);
00096       uint64_t buf(remain_.buf_);
00097       for (size_t i = 0; i < L - remain_.chars_; ++i)
00098       {
00099         if (pos == end_)
00100           break;
00101         buf = buf << 6;
00102         buf |= (static_cast<uint8_t>(*pos) - 0x30);
00103         ++pos;
00104       }
00105       return buf;
00106     }
00107   };
00108 
00109   explicit Decoder(const std::string &line, const DecoderRemain &remain = DecoderRemain())
00110     : begin_(line.begin())
00111     , end_(line.end())
00112     , remain_(remain)
00113   {
00114   }
00115   Iterator begin()
00116   {
00117     return Iterator(begin_, end_, remain_);
00118   }
00119   Iterator end()
00120   {
00121     return Iterator(end_, end_);
00122   }
00123   uint8_t getChecksum() const
00124   {
00125     uint8_t sum = 0;
00126     for (std::string::const_iterator it = begin_; it != end_; ++it)
00127     {
00128       sum += *it;
00129     }
00130     return sum;
00131   }
00132 };
00133 }  // namespace scip2
00134 
00135 #endif  // SCIP2_DECODE_H


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Thu Jun 6 2019 18:59:51