shared.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2022 CNRS-UM LIRMM, CNRS-AIST JRL
3  */
4 
5 #pragma once
6 
7 #include <memory>
8 #include <type_traits>
9 
10 namespace mc_rtc
11 {
12 
26 template<typename T, typename BaseT = void>
27 struct shared : public std::conditional<std::is_same<BaseT, void>::value, std::enable_shared_from_this<T>, BaseT>::type
28 {
29  static_assert(std::is_same<BaseT, void>::value || std::is_base_of<shared<BaseT>, BaseT>::value,
30  "shared<T, BaseT> requires a void base or a base that derives from shared<BaseT>");
31  operator std::shared_ptr<T>() { return std::static_pointer_cast<T>(static_cast<T *>(this)->shared_from_this()); }
32 
33  operator std::shared_ptr<const T>() const
34  {
35  return std::static_pointer_cast<const T>(static_cast<const T *>(this)->shared_from_this());
36  }
37 
39  using ActualBase =
40  typename std::conditional<std::is_same<BaseT, void>::value, std::enable_shared_from_this<T>, BaseT>::type;
41  using ActualBase::ActualBase;
42 };
43 
44 } // namespace mc_rtc
mc_rtc::shared
Definition: shared.h:27
mc_rtc::shared::ActualBase
typename std::conditional< std::is_same< BaseT, void >::value, std::enable_shared_from_this< T >, BaseT >::type ActualBase
Definition: shared.h:40
mc_rtc
Definition: Contact.h:87