Memory.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2008 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
16 
17 #ifndef COM_BORA_SOFTWARE__BALAU_UTIL__MEMORY
18 #define COM_BORA_SOFTWARE__BALAU_UTIL__MEMORY
19 
20 #include <Balau/Type/ToString.hpp>
21 
22 #include <cstring>
23 #include <memory>
24 #include <vector>
25 
26 namespace Balau::Util {
27 
31 struct Memory final {
33 
48  template <typename T, typename R = T, typename F, typename ... P>
49  static std::vector<std::unique_ptr<R>> makeUnique(F transform, const T & first, const P & ... remaining) {
50  std::vector<std::unique_ptr<R>> allItems;
51  allItems.emplace_back(transform(first));
52  std::vector<std::unique_ptr<R>> remainingItems = makeUnique<T, R, F>(transform, remaining ...);
53  std::move(remainingItems.begin(), remainingItems.end(), std::back_inserter(allItems));
54  return allItems;
55  }
56 
66  template <typename T, typename R = T, typename F>
67  static std::vector<std::unique_ptr<R>> makeUnique(F ) {
68  return std::vector<std::unique_ptr<R>>();
69  }
70 
72 
88  template <typename T, typename R = T, typename F, typename ... P>
89  static std::vector<std::unique_ptr<R>> makeUniqueV(F transform, const T & first, const P & ... remaining) {
90  std::vector<std::unique_ptr<R>> allItems = transform(first);
91  std::vector<std::unique_ptr<R>> remainingItems = makeUniqueV<T, R, F>(transform, remaining ...);
92  std::move(remainingItems.begin(), remainingItems.end(), std::back_inserter(allItems));
93  return allItems;
94  }
95 
104  template <typename T, typename R = T, typename F>
105  static std::vector<std::shared_ptr<R>> makeUniqueV(F ) {
106  return std::vector<std::shared_ptr<R>>();
107  }
108 
110 
125  template <typename T, typename R = T, typename F, typename ... P>
126  static std::vector<std::shared_ptr<R>> makeShared(F transform, const T & first, const P & ... remaining) {
127  std::vector<std::shared_ptr<R>> allItems;
128  allItems.emplace_back(transform(first));
129  std::vector<std::shared_ptr<R>> remainingItems = makeShared<T, R, F>(transform, remaining ...);
130  std::move(remainingItems.begin(), remainingItems.end(), std::back_inserter(allItems));
131  return allItems;
132  }
133 
142  template <typename T, typename R = T, typename F>
143  static std::vector<std::shared_ptr<R>> makeShared(F ) {
144  return std::vector<std::shared_ptr<R>>();
145  }
146 
148 
164  template <typename T, typename R = T, typename F, typename ... P>
165  static std::vector<std::shared_ptr<R>> makeSharedV(F transform, const T & first, const P & ... remaining) {
166  std::vector<std::shared_ptr<R>> allItems = transform(first);
167  std::vector<std::shared_ptr<R>> remainingItems = makeSharedV<T, R, F>(transform, remaining ...);
168  std::move(remainingItems.begin(), remainingItems.end(), std::back_inserter(allItems));
169  return allItems;
170  }
171 
180  template <typename T, typename R = T, typename F>
181  static std::vector<std::shared_ptr<R>> makeSharedV(F ) {
182  return std::vector<std::shared_ptr<R>>();
183  }
184 
186 
187  Memory() = delete;
188  Memory(const Memory &) = delete;
189  Memory & operator = (const Memory &) = delete;
190 };
191 
192 } // namespace Balau::Util
193 
194 #endif // COM_BORA_SOFTWARE__BALAU_UTIL__MEMORY
Pre-defined universal to-string functions.
static std::vector< std::unique_ptr< R > > makeUnique(F transform, const T &first, const P &... remaining)
Create a vector of unique pointers after transforming the arguments (scalar transform version)...
Definition: Memory.hpp:49
static std::vector< std::shared_ptr< R > > makeShared(F)
[End case] Create a vector of shared pointers after transforming the arguments (scalar transform vers...
Definition: Memory.hpp:143
Utility functions.
static std::vector< std::shared_ptr< R > > makeShared(F transform, const T &first, const P &... remaining)
Create a vector of shared pointers after transforming the arguments (scalar transform version)...
Definition: Memory.hpp:126
static std::vector< std::unique_ptr< R > > makeUnique(F)
[End case] Create a vector of unique pointers after transforming the arguments (scalar transform vers...
Definition: Memory.hpp:67
static std::vector< std::unique_ptr< R > > makeUniqueV(F transform, const T &first, const P &... remaining)
Create a vector of unique pointers after transforming the arguments (vector transform version)...
Definition: Memory.hpp:89
Utilities for memory.
Definition: Memory.hpp:31
static std::vector< std::shared_ptr< R > > makeSharedV(F transform, const T &first, const P &... remaining)
Create a vector of shared pointers after transforming the arguments (vector transform version)...
Definition: Memory.hpp:165
static std::vector< std::shared_ptr< R > > makeSharedV(F)
[End case] Create a vector of shared pointers after transforming the arguments (vector transform vers...
Definition: Memory.hpp:181
static std::vector< std::shared_ptr< R > > makeUniqueV(F)
[End case] Create a vector of unique pointers after transforming the arguments (vector transform vers...
Definition: Memory.hpp:105