<commit_before>/// \file ROOT/TDrawable.h
/// \ingroup Base ROOT7
/// \author <NAME> <<EMAIL>>
/// \date 2015-08-07
/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!

/*************************************************************************
 * Copyright (C) 1995-2015, <NAME> and <NAME>.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT7_TDrawable
#define ROOT7_TDrawable

#include <memory>

namespace ROOT {
namespace Experimental {
class TCanvas;

namespace Internal {

/** \class TDrawable
  Base class for drawable entities: objects that can be painted on a `TPad`.
  */

class TDrawable {
public:
  virtual ~TDrawable();

  /// Paint the object
  virtual void Paint(TCanvas& onCanv) = 0;
};

/// \class TAnyPtr
/// Models a shared pointer or a unique pointer.

template <class T>
class TUniWeakPtr {
   union {
      std::unique_ptr<T> fUnique;
      std::weak_ptr<T> fWeak;
   };
   bool fIsWeak; ///< fUnique or fWeak?

public:
   /// \class Accessor
   /// Gives transparent access to the shared or unique pointer.
   /// Locks if needed.
   class Accessor {
      union {
         std::shared_ptr<T> fShared;
         T* fRaw;
      };
      bool fIsShared;  ///< fRaw or fShared?

   public:
      Accessor(const TUniWeakPtr& uniweak):
         fIsShared(uniweak.fIsWeak) {
         if (fIsShared)
            fShared = uniweak.fWeak.lock();
         else
            fRaw = uniweak.fUnique.get();
      }

      Accessor(Accessor&& rhs): fIsShared(rhs.fIsShared) {
         if (fIsShared)
            fShared.swap(rhs.fShared);
         else
            fRaw = rhs.fRaw;
      }

      T* operator->() const { return fIsShared ? fRaw : fShared.get(); }
      T& operator*() const { return *operator->(); }
      operator bool() const { return fIsShared ? (bool)fRaw : (bool)fShared; }

      ~Accessor() {
         if (fIsShared)
            fShared.~shared_ptr();
      }
   };

   TUniWeakPtr(const std::shared_ptr<T>& ptr): fWeak(ptr), fIsWeak(true) {}
   TUniWeakPtr(std::unique_ptr<T>&& ptr): fUnique(std::move(ptr)), fIsWeak(false) {}
   TUniWeakPtr(TUniWeakPtr&& rhs): fIsWeak(rhs.fIsWeak) {
      if (fIsWeak)
         fWeak.swap(rhs.fWeak);
      else
         fUnique.swap(rhs.fUnique);
   }

   ~TUniWeakPtr() {
      if (fIsWeak)
         fWeak.~weak_ptr();
      else
         fUnique.~unique_ptr();
   }

   Accessor Get() const { return Accessor(*this); }
   void Reset() {
      if (fIsWeak)
         fWeak.reset();
      else
         fUnique.reset();
   }
};

} // namespace Internal
} // namespace Experimental
} // namespace ROOT

#endif
<commit_msg>Do not swap with an uninit object. Thanks, Timur!<commit_after>/// \file ROOT/TDrawable.h
/// \ingroup Base ROOT7
/// \author <NAME> <<EMAIL>>
/// \date 2015-08-07
/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!

/*************************************************************************
 * Copyright (C) 1995-2015, <NAME> and <NAME>.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT7_TDrawable
#define ROOT7_TDrawable

#include <memory>

namespace ROOT {
namespace Experimental {
class TCanvas;

namespace Internal {

/** \class TDrawable
  Base class for drawable entities: objects that can be painted on a `TPad`.
  */

class TDrawable {
public:
  virtual ~TDrawable();

  /// Paint the object
  virtual void Paint(TCanvas& onCanv) = 0;
};

/// \class TAnyPtr
/// Models a shared pointer or a unique pointer.

template <class T>
class TUniWeakPtr {
   union {
      std::unique_ptr<T> fUnique;
      std::weak_ptr<T> fWeak;
   };
   bool fIsWeak; ///< fUnique or fWeak?

public:
   /// \class Accessor
   /// Gives transparent access to the shared or unique pointer.
   /// Locks if needed.
   class Accessor {
      union {
         std::shared_ptr<T> fShared;
         T* fRaw;
      };
      bool fIsShared;  ///< fRaw or fShared?

   public:
      Accessor(const TUniWeakPtr& uniweak):
         fIsShared(uniweak.fIsWeak) {
         if (fIsShared)
            fShared = uniweak.fWeak.lock();
         else
            fRaw = uniweak.fUnique.get();
      }

      Accessor(Accessor&& rhs): fIsShared(rhs.fIsShared) {
         if (fIsShared)
            fShared.swap(rhs.fShared);
         else
            fRaw = rhs.fRaw;
      }

      T* operator->() const { return fIsShared ? fRaw : fShared.get(); }
      T& operator*() const { return *operator->(); }
      operator bool() const { return fIsShared ? (bool)fRaw : (bool)fShared; }

      ~Accessor() {
         if (fIsShared)
            fShared.~shared_ptr();
      }
   };

   TUniWeakPtr(const std::shared_ptr<T>& ptr): fWeak(ptr), fIsWeak(true) {}
   TUniWeakPtr(std::unique_ptr<T>&& ptr): fUnique(std::move(ptr)), fIsWeak(false) {}
   TUniWeakPtr(TUniWeakPtr&& rhs): fIsWeak(rhs.fIsWeak) {
      if (fIsWeak) {
         fWeak.weak_ptr(std::move(rhs.fWeak));
      } else
         fWeak.unique_ptr(std::move(rhs.fUnique));
   }

   ~TUniWeakPtr() {
      if (fIsWeak)
         fWeak.~weak_ptr();
      else
         fUnique.~unique_ptr();
   }

   Accessor Get() const { return Accessor(*this); }
   void Reset() {
      if (fIsWeak)
         fWeak.reset();
      else
         fUnique.reset();
   }
};

} // namespace Internal
} // namespace Experimental
} // namespace ROOT

#endif

{'fraction_non_alphanumeric': 0.16862937520444882, 'fraction_numerical': 0.0068694798822374874, 'mean_word_length': 2.126278118609407, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5089911', 'n_tokens_mistral': 1959, 'n_tokens_neox': 1843, 'n_words': 476}