Branch data Line data Source code
1 : : /*
2 : : * Copyright 2025 Jinwoo Sung
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 : : * http://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 <cstdint>
20 : : #include <functional>
21 : : #include <memory>
22 : : #include <stdexcept>
23 : : #include <string>
24 : : #include <string_view>
25 : : #include <type_traits>
26 : : #include <vector>
27 : :
28 : : #include "wirestead/base/visibility.hpp"
29 : : #include "wirestead/memory/safe_span.hpp"
30 : :
31 : : namespace wirestead {
32 : : namespace memory {
33 : :
34 : : /**
35 : : * @brief Safe data buffer for type-safe data transfer
36 : : *
37 : : * This class provides a safe wrapper around binary data that can be
38 : : * constructed from various sources and provides safe access methods.
39 : : */
40 : : class WIRESTEAD_API SafeDataBuffer {
41 : : public:
42 : : // Constructors
43 : : explicit SafeDataBuffer(const std::string& data);
44 : : explicit SafeDataBuffer(std::string_view data);
45 : : explicit SafeDataBuffer(std::vector<uint8_t> data);
46 : : explicit SafeDataBuffer(const uint8_t* data, size_t size);
47 : : explicit SafeDataBuffer(const char* data, size_t size);
48 : : explicit SafeDataBuffer(ConstByteSpan span);
49 : :
50 : : // Copy and move constructors
51 : 3 : SafeDataBuffer(const SafeDataBuffer&) = default;
52 : : SafeDataBuffer& operator=(const SafeDataBuffer&) = default;
53 : 442 : SafeDataBuffer(SafeDataBuffer&&) = default;
54 : 0 : SafeDataBuffer& operator=(SafeDataBuffer&&) = default;
55 : :
56 : : // Destructor
57 : 614 : ~SafeDataBuffer() = default;
58 : :
59 : : // Access methods
60 : : std::string as_string() const;
61 : : ConstByteSpan as_span() const noexcept;
62 : : const uint8_t* data() const noexcept;
63 : : size_t size() const noexcept;
64 : : bool empty() const noexcept;
65 : :
66 : : // Safe element access
67 : : const uint8_t& operator[](size_t index) const;
68 : : const uint8_t& at(size_t index) const;
69 : :
70 : : // Comparison operators
71 : : bool operator==(const SafeDataBuffer& other) const noexcept;
72 : : bool operator!=(const SafeDataBuffer& other) const noexcept;
73 : :
74 : : // Utility methods
75 : : void clear() noexcept;
76 : : void reserve(size_t capacity);
77 : : void resize(size_t new_size);
78 : :
79 : : // Validation
80 : : bool valid() const noexcept;
81 : : void validate() const;
82 : :
83 : : private:
84 : : std::vector<uint8_t> data_;
85 : :
86 : : // Helper methods
87 : : void validate_index(size_t index) const;
88 : : void validate_construction(const void* ptr, size_t size) const;
89 : : };
90 : :
91 : : /**
92 : : * @brief Safe data handler type for callbacks
93 : : */
94 : : using SafeDataHandler = std::function<void(const SafeDataBuffer&)>;
95 : :
96 : : /**
97 : : * @brief Factory functions for creating SafeDataBuffer instances
98 : : */
99 : : namespace safe_buffer_factory {
100 : : SafeDataBuffer from_string(const std::string& str);
101 : : SafeDataBuffer from_c_string(const char* str);
102 : : SafeDataBuffer from_vector(const std::vector<uint8_t>& vec);
103 : : SafeDataBuffer from_raw_data(const uint8_t* data, size_t size);
104 : : SafeDataBuffer from_span(ConstByteSpan span);
105 : : } // namespace safe_buffer_factory
106 : :
107 : : } // namespace memory
108 : : } // namespace wirestead
|