TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
factory.hpp
Go to the documentation of this file.
1// Copyright (c) 2013-2018 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2013-2018 Centre national de la recherche scientifique (CNRS)
3// Copyright (c) 2018 Simons Foundation
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 may obtain a copy of the License at
16// https://www.gnu.org/licenses/gpl-3.0.txt
17//
18// Authors: Olivier Parcollet, Nils Wentzell
19
24
25#pragma once
26
27#include "./macros.hpp"
28
29#include <utility>
30#include <vector>
31
32namespace triqs::utility {
33
34 namespace detail {
35
36 // Generic factory to construct a given type.
37 template <typename T> struct factories {
38 template <typename U> static T invoke(U &&x) { return T(std::forward<U>(x)); }
39 };
40
41 // Specialization of factories to construct a std::vector<T> from a std::vector<U>.
42 template <typename T> struct factories<std::vector<T>> {
43 using R = std::vector<T>;
44
45 // Copy and move vectors of the same type.
46 static R invoke(R &&x) { return R(std::move(x)); }
47 static R invoke(R const &x) { return R(x); }
48 static R invoke(R &x) { return R(x); }
49
50 // Convert vectors of different types element-wise, moving the elements out of an rvalue source.
51 template <typename U> static R invoke(std::vector<U> &&v) {
52 auto tmp = std::move(v);
53 R r;
54 r.reserve(tmp.size());
55 for (auto &x : tmp) r.push_back(factories<T>::invoke(std::move(x)));
56 return r;
57 }
58
59 template <typename U> static R invoke(std::vector<U> &v) {
60 R r;
61 r.reserve(v.size());
62 for (auto &x : v) r.push_back(factories<T>::invoke(x));
63 return r;
64 }
65
66 template <typename U> static R invoke(std::vector<U> const &v) {
67 R r;
68 r.reserve(v.size());
69 for (auto &x : v) r.push_back(factories<T>::invoke(x));
70 return r;
71 }
72 };
73
74 } // namespace detail
75
95 template <typename T, typename... U> T factory(U &&...x) { return detail::factories<T>::invoke(std::forward<U>(x)...); }
96
97} // namespace triqs::utility
T factory(U &&...x)
Generic factory to construct an object of a given type from an arbitrary parameter pack of arguments.
Definition factory.hpp:95
Common macros used in TRIQS.