Lagrange
SharedSpan.h
1/*
2 * Copyright 2022 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <memory>
15
16#include <lagrange/utils/span.h>
17
18namespace lagrange {
19
22
34template <typename T>
36{
37public:
38 using element_type = T;
39 using value_type = std::remove_cv_t<T>;
40
41public:
46 : m_data(nullptr)
47 , m_size(0)
48 {}
49
56 SharedSpan(std::shared_ptr<T> data, size_t size)
57 : m_data(data)
58 , m_size(size)
59 {}
60
64 span<T> ref() { return {m_data.get(), m_size}; }
65
69 span<const T> get() const { return {m_data.get(), m_size}; }
70
74 std::shared_ptr<const T> owner() const { return m_data; }
75
79 size_t size() const { return m_size; }
80
81protected:
82 // Technically, we should be using `std::shared_ptr<T[]>`. However, some versions of Apple
83 // Clang (e.g. clang-1200.0.32.28) has a bug in inferring the correct `element_type`.
84 std::shared_ptr<T> m_data;
85 size_t m_size = 0;
86};
87
100template <typename T, typename Y>
101SharedSpan<T> make_shared_span(const std::shared_ptr<Y>& r, T* element_ptr, size_t size)
102{
103 return {std::shared_ptr<T>(r, element_ptr), size};
104}
105
107
108} // namespace lagrange
Shared span with ownership tracking.
Definition: SharedSpan.h:36
std::shared_ptr< const T > owner() const
Definition: SharedSpan.h:74
size_t size() const
Definition: SharedSpan.h:79
SharedSpan()
Default constructor for empty buffer.
Definition: SharedSpan.h:45
span< const T > get() const
Definition: SharedSpan.h:69
SharedSpan(std::shared_ptr< T > data, size_t size)
Constructing a SharedSpan object from a shared buffer.
Definition: SharedSpan.h:56
span< T > ref()
Definition: SharedSpan.h:64
SharedSpan< T > make_shared_span(const std::shared_ptr< Y > &r, T *element_ptr, size_t size)
Created a SharedSpan object around an internal buffer of a parent object.
Definition: SharedSpan.h:101
::nonstd::span< T, Extent > span
A bounds-safe view for sequences of objects.
Definition: span.h:27
Main namespace for Lagrange.
Definition: AABBIGL.h:30