libkazv
copy-helper.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of libkazv.
3  * SPDX-FileCopyrightText: 2020-2021 Tusooa Zhu <tusooa@kazv.moe>
4  * SPDX-License-Identifier: AGPL-3.0-or-later
5  */
6 
7 #pragma once
8 #include "libkazv-config.hpp"
9 
10 #define KAZV_DECLARE_COPYABLE(typeName) \
11  typeName(const typeName &that); \
12  typeName(typeName &&that); \
13  typeName &operator=(const typeName &that); \
14  typeName &operator=(typeName &&that);
15 
16 #define KAZV_DEFINE_COPYABLE_UNIQUE_PTR(typeName, privateName) \
17  typeName::typeName(const typeName &that) \
18  : privateName(std::make_unique<decltype(privateName)::element_type>(*(that.privateName))) \
19  { \
20  } \
21  typeName::typeName(typeName &&that) \
22  : privateName(std::move(that.privateName)) \
23  { \
24  } \
25  typeName &typeName::operator=(const typeName &that) \
26  { \
27  if (privateName != that.privateName) { \
28  privateName.reset(); \
29  privateName = std::make_unique<decltype(privateName)::element_type>(*(that.privateName)); \
30  } \
31  return *this; \
32  } \
33  typeName &typeName::operator=(typeName &&that) \
34  { \
35  if (privateName != that.privateName) { \
36  privateName.reset(); \
37  privateName = std::move(that.privateName); \
38  } \
39  return *this; \
40  }