SSP21-CPP
StaticBuffer.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_STATICBUFFER_H
26 #define SER4CPP_STATICBUFFER_H
27 
28 #include "ser4cpp/util/Comparisons.h"
29 #include "ser4cpp/util/Uncopyable.h"
30 #include "ser4cpp/container/SequenceTypes.h"
31 
32 #include <limits>
33 #include <cstdint>
34 
35 namespace ser4cpp
36 {
37 
38 template <class T, T LENGTH>
39 class StaticBuffer final
40 {
41  static_assert(!std::numeric_limits<T>::is_signed&& std::numeric_limits<T>::is_integer, "Must be an unsigned integer");
42 
43 public:
44  StaticBuffer() {}
45 
46  inline RSeq<T> as_seq() const
47  {
48  return RSeq<T>(this->buffer, LENGTH);
49  }
50 
51  inline RSeq<T> as_seq(T max_size) const
52  {
53  return this->as_seq().take(max_size);
54  }
55 
56  inline wseq_t as_wseq()
57  {
58  return wseq_t(this->buffer, LENGTH);
59  }
60 
61  inline wseq_t as_wseq(uint32_t max_size)
62  {
63  return wseq_t(this->buffer, ser4cpp::min(LENGTH, max_size));
64  }
65 
66  inline T length() const
67  {
68  return LENGTH;
69  }
70 
71 private:
72  uint8_t buffer[LENGTH] = { 0 };
73 };
74 
75 }
76 
77 #endif
ser4cpp header-only library namespace
Definition: Buffer.h:33