PALISADE Lattice Crypto Library  1.11.9
A lattice crypto library for software engineers by software engineers.
memory.h
1 // @file memory.h Memory utilities for Palisade.
2 // @author TPOC: contact@palisade-crypto.org
3 //
4 // @copyright Copyright (c) 2019, New Jersey Institute of Technology (NJIT)
5 // All rights reserved.
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution. THIS SOFTWARE IS
13 // PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
14 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
15 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
16 // EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
17 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 
24 #ifndef LBCRYPTO_UTILS_MEMORY_H
25 #define LBCRYPTO_UTILS_MEMORY_H
26 
27 #include <algorithm>
28 #include <iterator>
29 #include <memory>
30 #include <utility>
31 #include <vector>
32 
33 using std::unique_ptr;
34 using std::vector;
35 
36 namespace lbcrypto {
37 
38 // make_unique was left out of c++11, these are the accepted implementation
39 #if _MSC_VER == 1700
40 
41 // MSVC11 does not support variadic templates
42 #define _MAKE_UNIQUE(TEMPLATE_LIST, PADDING_LIST, LIST, COMMA, X1, X2, X3, X4) \
43  \
44  template <class T COMMA LIST(_CLASS_TYPE)> \
45  inline std::unique_ptr<T> make_unique(LIST(_TYPE_REFREF_ARG)) { \
46  return std::unique_ptr<T>(new T(LIST(_FORWARD_ARG))); \
47  }
48 _VARIADIC_EXPAND_0X(_MAKE_UNIQUE, , , , )
49 #undef _MAKE_UNIQUE
50 
51 #else
52 
53 // *nix implementation
54 template <typename T, typename... Args>
55 std::unique_ptr<T> make_unique(Args&&... args) {
56  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
57 }
58 
59 #endif
60 
61 template <class X>
62 void MoveAppend(std::vector<X>& dst, std::vector<X>& src) {
63  if (dst.empty()) {
64  dst = std::move(src);
65  } else {
66  dst.reserve(dst.size() + src.size());
67  std::move(std::begin(src), std::end(src), std::back_inserter(dst));
68  src.clear();
69  }
70 }
71 
72 } // namespace lbcrypto
73 
74 #endif // LBCRYPTO_UTILS_MEMORY_H
Definition: binfhecontext.h:36