mdds
types_util.hpp
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*************************************************************************
3 *
4 * Copyright (c) 2022 Kohei Yoshida
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 *
27 ************************************************************************/
28
29#pragma once
30
31#include <vector>
32
33namespace mdds { namespace mtv { namespace detail {
34
35template<typename T>
37{
38 using yes_type = char;
39 using no_type = int;
40
41 template<typename U, std::size_t (U::*)() const>
43 {
44 };
45
46 template<typename U>
47 static yes_type test(test_has_method<U, &U::capacity>*);
48 template<typename U>
49 static no_type test(...);
50
51 using type = std::conditional_t<sizeof(test<T>(0)) == sizeof(yes_type), std::true_type, std::false_type>;
52};
53
54template<typename T>
55std::size_t get_block_capacity(const T& blk, std::true_type)
56{
57 return blk.capacity();
58}
59
60template<typename T>
61std::size_t get_block_capacity(const T&, std::false_type)
62{
63 return 0;
64}
65
66template<typename T>
67std::size_t get_block_capacity(const T& blk)
68{
69 typename has_capacity_method<T>::type v;
70 return get_block_capacity(blk, v);
71}
72
73template<typename T>
75{
76 using yes_type = char;
77 using no_type = int;
78
79 template<typename U, void (U::*)(typename T::size_type)>
81 {
82 };
83
84 template<typename U>
85 static yes_type test(test_has_method<U, &U::reserve>*);
86 template<typename U>
87 static no_type test(...);
88
89 using type = std::conditional_t<sizeof(test<T>(0)) == sizeof(yes_type), std::true_type, std::false_type>;
90};
91
92template<typename T>
93void reserve(T& blk, typename T::size_type size, std::true_type)
94{
95 return blk.reserve(size);
96}
97
98template<typename T>
99void reserve(T&, typename T::size_type, std::false_type)
100{}
101
102template<typename T>
103void reserve(T& blk, typename T::size_type size)
104{
105 typename has_reserve_method<T>::type v;
106 reserve(blk, size, v);
107}
108
109template<typename T>
111{
112 using yes_type = char;
113 using no_type = int;
114
115 template<typename U, void (U::*)()>
117 {
118 };
119
120 template<typename U>
121 static yes_type test(test_has_method<U, &U::shrink_to_fit>*);
122 template<typename U>
123 static no_type test(...);
124
125 using type = std::conditional_t<sizeof(test<T>(0)) == sizeof(yes_type), std::true_type, std::false_type>;
126};
127
128template<typename T>
129void shrink_to_fit(T& blk, std::true_type)
130{
131 return blk.shrink_to_fit();
132}
133
134template<typename T>
135void shrink_to_fit(T&, std::false_type)
136{}
137
138template<typename T>
139void shrink_to_fit(T& blk)
140{
141 typename has_shrink_to_fit_method<T>::type v;
142 shrink_to_fit(blk, v);
143}
144
145template<typename T>
147{
148 using type = std::false_type;
149};
150
151template<>
152struct is_std_vector_bool_store<std::vector<bool>>
153{
154 using type = std::true_type;
155};
156
157template<typename Blk>
159{
160 using type = typename is_std_vector_bool_store<typename Blk::store_type>::type;
161};
162
163}}} // namespace mdds::mtv::detail
164
165/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition: types_util.hpp:37
Definition: types_util.hpp:75
Definition: types_util.hpp:111
Definition: types_util.hpp:159
Definition: types_util.hpp:147