Skip to content

Commit

Permalink
rc: make_rc(): ensure function can only be used for in-place construc…
Browse files Browse the repository at this point in the history
…tion, and not for making copies
  • Loading branch information
sharkautarch committed Sep 6, 2024
1 parent 2e799b2 commit 761b73f
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/rc.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ namespace gamescope
{
template <typename T, bool Public>
class Rc;

template <typename A, typename B>
concept NotTheSameAs = !std::is_same<std::remove_cvref_t<std::remove_pointer_t<A>>, std::remove_cvref_t<std::remove_pointer_t<B>>>::value;

#define ENABLE_IN_PLACE_RC using gamescope::RcObject::RcObject; \
template <class T, class... Args> \
template <class T, gamescope::NotTheSameAs<T>... Args> \
friend gamescope::Rc<T, true> gamescope::make_rc(Args&&... args);
class RcObject
{
template <class T, class... Args>
template <class T, NotTheSameAs<T>... Args>
friend gamescope::Rc<T, true> make_rc(Args&&... args);
protected:
explicit constexpr RcObject(std::in_place_t) : m_uRefCount{1}, m_uRefPrivate{1} {}
Expand Down Expand Up @@ -104,7 +108,7 @@ namespace gamescope
static void DecRef( T* pObject ) { pObject->DecRefPrivate(); }
};

template <class T, class... Args>
template <class T, NotTheSameAs<T>... Args>
Rc<T, true> make_rc(Args&&... args) {
T* pObj = new T(std::in_place_t{}, std::forward<Args>(args)...);
return Rc<T, true>{std::in_place_t{}, pObj};
Expand All @@ -116,7 +120,7 @@ namespace gamescope
template <typename Tx, bool Publicx>
friend class Rc;

template <class Tx, class... Args>
template <class Tx, NotTheSameAs<Tx>... Args>
friend Rc<Tx, true> gamescope::make_rc(Args&&... args);

using RcRef = RcRef_<T, Public>;
Expand Down

0 comments on commit 761b73f

Please sign in to comment.