SSP21-CPP
RSeq.h
1 /*
2  * Copyright (c) 2018, Automatak LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
6  * following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
9  * disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
12  * disclaimer in the documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
15  * products derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
23  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #ifndef SER4CPP_RSEQ_H
26 #define SER4CPP_RSEQ_H
27 
28 #include "ser4cpp/container/HasLength.h"
29 #include "ser4cpp/util/Comparisons.h"
30 
31 #include <limits>
32 #include <cstdint>
33 #include <cstring>
34 
35 /**
36 * @brief ser4cpp header-only library namespace
37 */
38 namespace ser4cpp
39 {
40 
41 /**
42 * Represents a readonly sequence of bytes with a parameterized length type (L)
43 */
44 template <class L>
45 class RSeq : public HasLength<L>
46 {
47  static_assert(std::numeric_limits<L>::is_integer&& !std::numeric_limits<L>::is_signed, "Must be an unsigned integer");
48 
49 public:
50  static RSeq empty()
51  {
52  return RSeq(nullptr, 0);
53  }
54 
55  RSeq() : HasLength<L>(0)
56  {}
57 
58  RSeq(uint8_t const* buffer, L length) :
59  HasLength<L>(length),
60  buffer_(buffer)
61  {}
62 
63  void make_empty()
64  {
65  this->buffer_ = nullptr;
66  this->m_length = 0;
67  }
68 
69  RSeq take(L count) const
70  {
71  return RSeq(this->buffer_, (count < this->length()) ? count : this->length());
72  }
73 
74  RSeq skip(L count) const
75  {
76  auto num = ser4cpp::min(this->length(), count);
77  return RSeq(this->buffer_ + num, this->length() - num);
78  }
79 
80  void advance(L count)
81  {
82  auto num = ser4cpp::min(this->length(), count);
83  this->buffer_ += num;
84  this->m_length -= num;
85  }
86 
87  operator uint8_t const* () const
88  {
89  return this->buffer_;
90  };
91 
92  bool equals(const RSeq& rhs) const
93  {
94  if (this->m_length == rhs.m_length)
95  {
96  return memcmp(this->buffer_, rhs.buffer_, this->m_length) == 0;
97  }
98  else
99  {
100  return false;
101  }
102  }
103 
104 protected:
105  uint8_t const* buffer_ = nullptr;
106 };
107 
108 }
109 
110 #endif
ser4cpp header-only library namespace
Definition: Buffer.h:33