rocprofiler-sdk/cxx/container/c_array.hpp Source File

rocprofiler-sdk/cxx/container/c_array.hpp Source File#

ROCprofiler-SDK developer API: rocprofiler-sdk/cxx/container/c_array.hpp Source File
ROCprofiler-SDK developer API 1.0.0
ROCm Profiling API and tools
c_array.hpp
1// MIT License
2//
3// Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22//
23
24#pragma once
25
26#include <cstddef>
27#include <cstdint>
28#include <stdexcept>
29#include <string>
30
31namespace rocprofiler
32{
33namespace sdk
34{
35namespace container
36{
37template <typename Tp>
38struct c_array
39{
40 // Construct an array wrapper from a base pointer and array size
41 c_array(Tp* _base, size_t _size)
42 : m_base{_base}
43 , m_size{_size}
44 {}
45
46 ~c_array() = default;
47 c_array(const c_array&) = default;
48 c_array(c_array&&) noexcept = default;
49 c_array& operator=(const c_array&) = default;
50 c_array& operator=(c_array&&) noexcept = default;
51
52 // Get the size of the wrapped array
53 size_t size() const { return m_size; }
54
55 // Access an element by index
56 Tp& operator[](size_t i) { return m_base[i]; }
57
58 // Access an element by index
59 const Tp& operator[](size_t i) const { return m_base[i]; }
60
61 // Access an element by index with bounds check
62 Tp& at(size_t i)
63 {
64 if(i < m_size) return m_base[i];
65 throw std::out_of_range(std::string{typeid(*this).name()} + std::to_string(i) +
66 " exceeds size " + std::to_string(m_size));
67 }
68
69 // Access an element by index with bounds check
70 const Tp& at(size_t i) const
71 {
72 if(i < m_size) return m_base[i];
73 throw std::out_of_range(std::string{typeid(*this).name()} + std::to_string(i) +
74 " exceeds size " + std::to_string(m_size));
75 }
76
77 // Get a slice of this array, from a start index (inclusive) to end index (exclusive)
78 c_array<Tp> slice(size_t start, size_t end) { return c_array<Tp>(&m_base[start], end - start); }
79
80 void pop_front()
81 {
82 ++m_base;
83 --m_size;
84 }
85
86 void pop_back() { --m_size; }
87
88 operator Tp*() const { return m_base; }
89
90 // Iterator class for convenient range-based for loop support
91 template <typename Up>
92 struct iterator
93 {
94 // Start the iterator at a given pointer
95 iterator(Tp* p)
96 : m_ptr{p}
97 {}
98
99 // Advance to the next element
100 void operator++() { ++m_ptr; }
101 void operator++(int) { m_ptr++; }
102
103 // Get the current element
104 Up& operator*() const { return *m_ptr; }
105
106 // Compare iterators
107 bool operator==(const iterator& rhs) const { return m_ptr == rhs.m_ptr; }
108 bool operator!=(const iterator& rhs) const { return m_ptr != rhs.m_ptr; }
109
110 private:
111 Tp* m_ptr = nullptr;
112 };
113
114 // Get an iterator positioned at the beginning of the wrapped array
115 iterator<Tp> begin() { return iterator<Tp>{m_base}; }
116 iterator<const Tp> begin() const { return iterator<const Tp>{m_base}; }
117
118 // Get an iterator positioned at the end of the wrapped array
119 iterator<Tp> end() { return iterator<Tp>{&m_base[m_size]}; }
120 iterator<const Tp> end() const { return iterator<const Tp>{&m_base[m_size]}; }
121
122private:
123 Tp* m_base = nullptr;
124 size_t m_size = 0;
125};
126
127// Function for automatic template argument deduction
128template <typename Tp>
129c_array<Tp>
130make_c_array(Tp* base, size_t size)
131{
132 return c_array<Tp>(base, size);
133}
134} // namespace container
135} // namespace sdk
136} // namespace rocprofiler