Cppcheck
smallvector.h
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2023 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef smallvectorH
20 #define smallvectorH
21 
22 #include <cstddef>
23 
24 static constexpr std::size_t DefaultSmallVectorSize = 3;
25 
26 #ifdef HAVE_BOOST
27 #include <boost/container/small_vector.hpp>
28 
29 template<typename T, std::size_t N = DefaultSmallVectorSize>
30 using SmallVector = boost::container::small_vector<T, N>;
31 #else
32 #include <utility>
33 #include <vector>
34 
35 template<class T, std::size_t N>
36 struct TaggedAllocator : std::allocator<T>
37 {
38  template<class ... Ts>
39  // cppcheck-suppress noExplicitConstructor
40  // NOLINTNEXTLINE(google-explicit-constructor)
41  TaggedAllocator(Ts&&... ts)
42  : std::allocator<T>(std::forward<Ts>(ts)...)
43  {}
44 
45  template<class U>
46  // cppcheck-suppress noExplicitConstructor
47  // NOLINTNEXTLINE(google-explicit-constructor)
49 
50  template<class U>
51  struct rebind
52  {
54  };
55 };
56 
57 template<typename T, std::size_t N = DefaultSmallVectorSize>
58 class SmallVector : public std::vector<T, TaggedAllocator<T, N>>
59 {
60 public:
61  template<class ... Ts>
62  // NOLINTNEXTLINE(google-explicit-constructor)
63  SmallVector(Ts&&... ts)
64  : std::vector<T, TaggedAllocator<T, N>>(std::forward<Ts>(ts)...)
65  {
66  this->reserve(N);
67  }
68 };
69 #endif
70 
71 #endif
SmallVector(Ts &&... ts)
Definition: smallvector.h:63
static constexpr std::size_t DefaultSmallVectorSize
Definition: smallvector.h:24
TaggedAllocator(Ts &&... ts)
Definition: smallvector.h:41
TaggedAllocator(const TaggedAllocator< U, N >)
Definition: smallvector.h:48