• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef COMMON_INTERFACES_OBJECTS_TRAITS_H
17 #define COMMON_INTERFACES_OBJECTS_TRAITS_H
18 
19 #include "common_interfaces/objects/base_object.h"
20 #include <type_traits>
21 
22 namespace common::objects_traits {
23 
24 template <typename U>
25 constexpr bool is_heap_object_v = std::is_base_of_v<BaseObject, std::remove_pointer_t<U>>;
26 
27 // WriteBarrier: void (void*, size_t, U)
28 template <typename F>
29 constexpr bool is_write_barrier_callable_v =
30     std::is_invocable_r_v<void, F, void*, size_t, BaseObject*>;
31 
32 // ReadBarrier: U (void*, size_t)
33 template <typename F, typename U>
34 constexpr bool is_read_barrier_callable_v =
35     is_heap_object_v<U> &&
36     std::is_invocable_v<F, void*, size_t> &&
37     std::is_convertible_v<std::invoke_result_t<F, void*, size_t>, U>;
38 
39 // Allocator: U (size_t, CommonType)
40 template <typename F, typename U>
41 constexpr bool is_allocate_callable_v =
42     is_heap_object_v<U> && std::is_invocable_r_v<U, F, size_t, CommonType>;
43 
44 // ---- enable_if_is_* traits ----
45 template <typename F>
46 using enable_if_is_write_barrier = std::enable_if_t<is_write_barrier_callable_v<F>, int>;
47 
48 template <typename F, typename U>
49 using enable_if_is_read_barrier = std::enable_if_t<is_read_barrier_callable_v<F, U>, int>;
50 
51 template <typename F, typename U>
52 using enable_if_is_allocate = std::enable_if_t<is_allocate_callable_v<F, U>, int>;
53 
54 template <typename Container, typename T>
55 struct is_std_vector_of : std::false_type {};
56 
57 template <typename Alloc, typename T>
58 struct is_std_vector_of<std::vector<T, Alloc>, T> : std::true_type {};
59 
60 template <typename Container, typename T>
61 constexpr bool is_std_vector_of_v = is_std_vector_of<Container, T>::value;
62 
63 
64 template <typename Vec>
65 using get_allocator_type_t = typename std::decay_t<Vec>::allocator_type;
66 
67 template <typename Alloc, typename U>
68 using rebind_alloc_t = typename std::allocator_traits<Alloc>::template rebind_alloc<U>;
69 
70 template <typename Vec, typename NewT>
71 using vector_with_same_alloc_t =
72     std::vector<NewT, rebind_alloc_t<get_allocator_type_t<Vec>, NewT>>;
73 
74 
75 } // namespace common::objects_traits
76 
77 
78 #endif //COMMON_INTERFACES_OBJECTS_TRAITS_H
79