Visual Computing Library
Loading...
Searching...
No Matches
polymorphism.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_CONCEPTS_POLYMORPHISM_H
24#define VCL_CONCEPTS_POLYMORPHISM_H
25
26#include "const_correctness.h"
27
28#include <concepts>
29#include <memory>
30
31namespace vcl {
32
52template<typename T>
53concept Cloneable = requires (T&& obj) {
54 // TODO: Right now, this concept can be used only with a base class that has
55 // a clone method, because the concept requires that the return type of the
56 // clone method is a shared pointer to the same class as the object. This is
57 // not always the case, especially when the clone method is overridden in
58 // derived classes. We need to find a way to make this concept work with
59 // overridden clone methods.
60 //
61 // However, we should also consider the consequences of this change for the
62 // class vcl::PolymorphicObjectVector, which relies on this concept to
63 // determine if an object can be cloned, and stores objects of the Base
64 // class in a vector.
65 //
66 // Example:
67 // class Base {
68 // public:
69 // virtual std::shared_ptr<Base> clone() const = 0;
70 // };
71 //
72 // class Derived : public Base {
73 // public:
74 // std::shared_ptr<Derived> clone() const { ... }
75 // };
76 //
77 // static_assert(vcl::Cloneable<Base>, ""); // OK
78 // static_assert(vcl::Cloneable<Derived>, ""); // Error, but should work
79 { obj.clone() } -> std::same_as<std::shared_ptr<std::remove_cvref_t<T>>>;
80};
81
82} // namespace vcl
83
84#endif // VCL_CONCEPTS_POLYMORPHISM_H
Concept that is evaluated true if T is a cloneable object.
Definition polymorphism.h:53