Line data Source code
1 : // Copyright (C) 2026 Free Software Foundation, Inc.
2 :
3 : // This file is part of GCC.
4 :
5 : // GCC is free software; you can redistribute it and/or modify it under
6 : // the terms of the GNU General Public License as published by the Free
7 : // Software Foundation; either version 3, or (at your option) any later
8 : // version.
9 :
10 : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : // for more details.
14 :
15 : // You should have received a copy of the GNU General Public License
16 : // along with GCC; see the file COPYING3. If not see
17 : // <http://www.gnu.org/licenses/>.
18 :
19 : #ifndef RUST_CLONEABLE
20 : #define RUST_CLONEABLE
21 :
22 : #include "rust-system.h"
23 :
24 : namespace Rust {
25 :
26 : // used to automatically copy cloneable types
27 :
28 : template <typename T> struct CloneableDelegate
29 : {
30 : };
31 :
32 344 : template <typename T> class Cloneable
33 : {
34 : public:
35 : template <typename... Args>
36 91509 : Cloneable (Args &&...args) : inner (std::forward<Args> (args)...)
37 : {}
38 :
39 62745 : Cloneable (const Cloneable &other)
40 62745 : : Cloneable (CloneableDelegate<T>::clone (other.inner))
41 62745 : {}
42 :
43 0 : template <typename Arg> Cloneable &operator= (Arg &&arg)
44 : {
45 0 : inner = std::forward<Arg> (arg);
46 : return *this;
47 : }
48 :
49 : Cloneable &operator= (const Cloneable &other)
50 : {
51 : inner = CloneableDelegate<T>::clone (other.inner);
52 : return *this;
53 : }
54 :
55 : Cloneable (Cloneable &&) = default;
56 : Cloneable &operator= (Cloneable &&) = default;
57 :
58 54814 : T &get () { return inner; }
59 514 : const T &get () const { return inner; }
60 :
61 594 : bool operator== (decltype (nullptr)) const { return inner == nullptr; }
62 :
63 598897 : bool operator!= (decltype (nullptr)) const { return inner != nullptr; }
64 :
65 : private:
66 : T inner;
67 : };
68 :
69 : // general specializations
70 :
71 : template <typename T> struct CloneableDelegate<std::vector<T>>
72 : {
73 4841 : static std::vector<T> clone (const std::vector<T> &other)
74 : {
75 4841 : std::vector<T> ret;
76 4841 : ret.reserve (other.size ());
77 11997 : for (auto &ent : other)
78 7156 : ret.push_back (CloneableDelegate<T>::clone (ent));
79 4841 : return ret;
80 : }
81 : };
82 :
83 : } // namespace Rust
84 :
85 : #endif // RUST_CLONEABLE
|