xref: /aosp_15_r20/system/libcppbor/include/cppbor/span.h (revision 0963554132e37a14524024fa04dc9e883c7a8221)
1 /*
2  * Copyright 2024 Google LLC
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  *     https://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 #pragma once
18 
19 #include <cstddef>
20 
21 #if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L || __cplusplus >= 202002L
22 #error This trivial span.h should not be used if the platform supports std::span
23 #endif  // ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L || __cplusplus >= 202002L
24 
25 namespace cppbor {
26 
27 template <class T>
28 class span {
29   public:
span()30     constexpr span() : mBegin(nullptr), mLen(0) {}
span(T * begin,size_t len)31     explicit constexpr span(T* begin, size_t len) : mBegin(begin), mLen(len) {}
32 
begin()33     constexpr T* begin() const noexcept { return mBegin; }
end()34     constexpr T* end() const noexcept { return mBegin + mLen; }
data()35     constexpr T* data() const noexcept { return mBegin; }
36 
size()37     constexpr size_t size() const noexcept { return mLen; }
38 
39   private:
40     T* mBegin;
41     size_t mLen;
42 };
43 
44 }  // namespace cppbor
45