Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:57

0001 //===-- Cloneable.h ---------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_UTILITY_CLONEABLE_H
0010 #define LLDB_UTILITY_CLONEABLE_H
0011 
0012 #include <memory>
0013 #include <type_traits>
0014 
0015 namespace lldb_private {
0016 
0017 /// \class Cloneable Cloneable.h "lldb/Utility/Cloneable.h"
0018 /// A class that implements CRTP-based "virtual constructor" idiom.
0019 ///
0020 /// Example:
0021 /// @code
0022 /// class Base {
0023 ///   using TopmostBase = Base;
0024 /// public:
0025 ///   virtual std::shared_ptr<Base> Clone() const = 0;
0026 /// };
0027 /// @endcode
0028 ///
0029 /// To define a class derived from the Base with overridden Clone:
0030 /// @code
0031 /// class Intermediate : public Cloneable<Intermediate, Base> {};
0032 /// @endcode
0033 ///
0034 /// To define a class at the next level of inheritance with overridden Clone:
0035 /// @code
0036 /// class Derived : public Cloneable<Derived, Intermediate> {};
0037 /// @endcode
0038 
0039 template <typename Derived, typename Base>
0040 class Cloneable : public Base {
0041 public:
0042   using Base::Base;
0043 
0044   std::shared_ptr<typename Base::TopmostBase> Clone() const override {
0045     // std::is_base_of requires derived type to be complete, that's why class
0046     // scope static_assert cannot be used.
0047     static_assert(std::is_base_of<Cloneable, Derived>::value,
0048                   "Derived class must be derived from this.");
0049 
0050     return std::make_shared<Derived>(static_cast<const Derived &>(*this));
0051   }
0052 };
0053 
0054 } // namespace lldb_private
0055 
0056 #endif // LLDB_UTILITY_CLONEABLE_H