Apache Ignite C++ Client
Loading...
Searching...
No Matches
bytes_view.h
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#pragma once
19
20#include "detail/byte_traits.h"
21
22#include <array>
23#include <cstddef>
24#include <string>
25#include <string_view>
26#include <vector>
27
28namespace ignite {
29
31struct bytes_view : std::basic_string_view<std::byte, detail::byte_traits> {
32 using base_type = std::basic_string_view<std::byte, detail::byte_traits>;
33
34 constexpr bytes_view() noexcept = default;
35
36 constexpr bytes_view(const std::byte *data, std::size_t size) noexcept
37 : base_type(data, size) {}
38
39 constexpr bytes_view(const void *data, std::size_t size) noexcept
40 : base_type(static_cast<const std::byte *>(data), size) {}
41
42 constexpr bytes_view(const base_type &v) noexcept // NOLINT(google-explicit-constructor)
43 : base_type(v.data(), v.size()) {}
44
45 template<std::size_t SIZE>
46 constexpr bytes_view(const char (&v)[SIZE]) noexcept // NOLINT(google-explicit-constructor)
47 : base_type(reinterpret_cast<const std::byte *>(v), SIZE) {}
48
49 template<std::size_t SIZE>
50 constexpr bytes_view(const std::array<std::byte, SIZE> &v) noexcept // NOLINT(google-explicit-constructor)
51 : base_type(v.data(), v.size()) {}
52
53 bytes_view(const std::string &v) noexcept // NOLINT(google-explicit-constructor)
54 : base_type(reinterpret_cast<const std::byte *>(v.data()), v.size()) {}
55
56 bytes_view(const std::string_view &v) noexcept // NOLINT(google-explicit-constructor)
57 : base_type(reinterpret_cast<const std::byte *>(v.data()), v.size()) {}
58
59 bytes_view(const std::vector<std::byte> &v) noexcept // NOLINT(google-explicit-constructor)
60 : base_type(v.data(), v.size()) {}
61
62 explicit operator std::string() const { return {reinterpret_cast<const char *>(data()), size()}; }
63
64 explicit operator std::vector<std::byte>() const { return {begin(), end()}; }
65};
66
67} // namespace ignite