<commit_before>#pragma once
#include <memory>
#include <typeinfo>
#include <type_traits>
#ifdef _MSC_VER
  #ifdef _HAS_CXX17
    #include <optional>
    #define ANY_SHARED_PTR_HAS_LIB_OPTIONAL 
  #endif
#else // GCC
  #if __has_include(<optional>) // requires GCC 5 or greater
    #include <optional>
    #define ANY_SHARED_PTR_HAS_LIB_OPTIONAL 
  #else
    #include <experimental/optional>
    namespace std {
      using std::experimental::optional;
    } // namespace std
    #define ANY_SHARED_PTR_HAS_LIB_OPTIONAL 
  #endif
#endif

#if defined(__GNUC__)
#pragma GCC diagnostic push
// Silence the 'unused-function' warning that first appeared when using GCC 6.2.
// This warning wasn't emitted by GCC 4.9 so I suspect it's a false positive.
#pragma GCC diagnostic ignored "-Wunused-function"
#endif

namespace xxx {

  class bad_any_shared_ptr_cast : public std::bad_cast
  {
  public:
    virtual const char* what() const noexcept override { return "bad any_shared_ptr cast"; }
  };

  inline namespace ver_1 {

    class any_shared_ptr
    {
    public:
      //-----------------------------------------------------
      // Canonical "Rule of 5" ctors/dtor and copy operators

      ~any_shared_ptr();

      any_shared_ptr(any_shared_ptr const & other) noexcept;

      any_shared_ptr(any_shared_ptr && other) noexcept;

      any_shared_ptr& operator=(any_shared_ptr const& other) noexcept;

      any_shared_ptr& operator=(any_shared_ptr && other) noexcept;

      //-----------------------------------------------------
      // ctors

      template<typename T>
      any_shared_ptr(std::shared_ptr<T> ptr) noexcept;

      any_shared_ptr() noexcept;

      //-----------------------------------------------------
      // Modifiers

      void swap(any_shared_ptr & other) noexcept;

      // reset to empty state 
      void reset() noexcept { ::new (this) any_shared_ptr(); }

      //-----------------------------------------------------
      // Observers

      // return true if not empty
      bool has_value() const noexcept { return my_type_info != nullptr; }

      // Return the T's type-info for the held by shared_ptr<T>
      const std::type_info & type() const noexcept;

      // Return true if the held shared_ptr use_count is 1 (mimics std::shared_ptr::unique())
      bool  unique() const noexcept;

    private:

      template<typename T>
      using HeldBaseType = T;

      template<typename T>
      using HeldType = std::shared_ptr<T>;


      template <typename T>
      std::shared_ptr<T> cast(bool & cast_ok) const noexcept;

      // Base class for the class holds shared_ptr
      struct IHolder
      {
        virtual ~IHolder() = default;

        virtual bool                    unique() const noexcept = 0;
        virtual const IHolder *         clone(void* const inplaceMemory) const noexcept = 0;
        virtual void                    try_up_cast_by_throwing() const = 0;
        virtual std::shared_ptr<void>   make_shared_ptr_alias(void * ptr) const noexcept = 0;
      };


      template<typename T>
      struct Holder final : public IHolder
      {
        Holder()
          : my_ptr()
        {}

        Holder(std::shared_ptr<T> ptr)
          : my_ptr(std::move(ptr))
        {}

        bool                    unique() const noexcept final { return my_ptr.unique(); }

        const IHolder *         clone(void* const inplaceMemory) const noexcept final { return ::new (inplaceMemory) Holder(my_ptr); }

        void                    try_up_cast_by_throwing() const final { throw my_ptr.get(); }

        std::shared_ptr<void>   make_shared_ptr_alias(void* p) const noexcept final 
        { 
          return std::shared_ptr<void>(my_ptr, p);  
        }

        // The held shared_ptr
        std::shared_ptr<T>  my_ptr;

      };

      using storage_t = typename std::aligned_storage<sizeof(Holder<void>), std::alignment_of<Holder<void>>::value>::type;

      const std::type_info *  my_type_info{ nullptr };
      // Inplace storage to hold Holder<T>
      storage_t               my_inplace_storage;

      // Pointer to my_inplace_storage that holds Holder<T> 
      const IHolder *  holder() const noexcept 
      { 
        // [[gsl::suppress(type.1)]]  // warning C26490 : Don't use reinterpret_cast. (type.1)
        return reinterpret_cast<const IHolder *>(&my_inplace_storage); 
      }

      template<typename T>
      friend std::shared_ptr<T> any_shared_ptr_cast(any_shared_ptr const & anySharedPtr);

      template<typename T>
      friend std::optional<std::shared_ptr<T>> any_shared_ptr_cast(any_shared_ptr const * anySharedPtr) noexcept;

    };

    //-----------------------------------------------------------------------------------------------------
    // Implementation

    template<typename T>
    any_shared_ptr::any_shared_ptr(std::shared_ptr<T> ptr) noexcept
      : my_type_info( & typeid(HeldType<T>) )
    {
      ::new (&my_inplace_storage) Holder<T>(std::move(ptr));
    }

    // [[gsl::suppress(type.6)]] //  warning C26495 : Variable 'xxx::ver_1::any_shared_ptr::my_inplace_storage' is uninitialized.Always initialize a member variable. (type.6)
    inline any_shared_ptr::any_shared_ptr() noexcept
    {
      ::new (&my_inplace_storage) Holder<void>();
    }

    inline any_shared_ptr::~any_shared_ptr()
    {
      holder()->~IHolder();<commit_msg>Fix VS2015 build<commit_after>#pragma once
#include <memory>
#include <typeinfo>
#include <type_traits>
#ifdef _MSC_VER
  #if _HAS_CXX17 != 0
    #include <optional>
    #define ANY_SHARED_PTR_HAS_LIB_OPTIONAL 
  #endif
#else // GCC
  #if __has_include(<optional>) // requires GCC 5 or greater
    #include <optional>
    #define ANY_SHARED_PTR_HAS_LIB_OPTIONAL 
  #else
    #include <experimental/optional>
    namespace std {
      using std::experimental::optional;
    } // namespace std
    #define ANY_SHARED_PTR_HAS_LIB_OPTIONAL 
  #endif
#endif

#if defined(__GNUC__)
#pragma GCC diagnostic push
// Silence the 'unused-function' warning that first appeared when using GCC 6.2.
// This warning wasn't emitted by GCC 4.9 so I suspect it's a false positive.
#pragma GCC diagnostic ignored "-Wunused-function"
#endif

namespace xxx {

  class bad_any_shared_ptr_cast : public std::bad_cast
  {
  public:
    virtual const char* what() const noexcept override { return "bad any_shared_ptr cast"; }
  };

  inline namespace ver_1 {

    class any_shared_ptr
    {
    public:
      //-----------------------------------------------------
      // Canonical "Rule of 5" ctors/dtor and copy operators

      ~any_shared_ptr();

      any_shared_ptr(any_shared_ptr const & other) noexcept;

      any_shared_ptr(any_shared_ptr && other) noexcept;

      any_shared_ptr& operator=(any_shared_ptr const& other) noexcept;

      any_shared_ptr& operator=(any_shared_ptr && other) noexcept;

      //-----------------------------------------------------
      // ctors

      template<typename T>
      any_shared_ptr(std::shared_ptr<T> ptr) noexcept;

      any_shared_ptr() noexcept;

      //-----------------------------------------------------
      // Modifiers

      void swap(any_shared_ptr & other) noexcept;

      // reset to empty state 
      void reset() noexcept { ::new (this) any_shared_ptr(); }

      //-----------------------------------------------------
      // Observers

      // return true if not empty
      bool has_value() const noexcept { return my_type_info != nullptr; }

      // Return the T's type-info for the held by shared_ptr<T>
      const std::type_info & type() const noexcept;

      // Return true if the held shared_ptr use_count is 1 (mimics std::shared_ptr::unique())
      bool  unique() const noexcept;

    private:

      template<typename T>
      using HeldBaseType = T;

      template<typename T>
      using HeldType = std::shared_ptr<T>;


      template <typename T>
      std::shared_ptr<T> cast(bool & cast_ok) const noexcept;

      // Base class for the class holds shared_ptr
      struct IHolder
      {
        virtual ~IHolder() = default;

        virtual bool                    unique() const noexcept = 0;
        virtual const IHolder *         clone(void* const inplaceMemory) const noexcept = 0;
        virtual void                    try_up_cast_by_throwing() const = 0;
        virtual std::shared_ptr<void>   make_shared_ptr_alias(void * ptr) const noexcept = 0;
      };


      template<typename T>
      struct Holder final : public IHolder
      {
        Holder()
          : my_ptr()
        {}

        Holder(std::shared_ptr<T> ptr)
          : my_ptr(std::move(ptr))
        {}

        bool                    unique() const noexcept final { return my_ptr.unique(); }

        const IHolder *         clone(void* const inplaceMemory) const noexcept final { return ::new (inplaceMemory) Holder(my_ptr); }

        void                    try_up_cast_by_throwing() const final { throw my_ptr.get(); }

        std::shared_ptr<void>   make_shared_ptr_alias(void* p) const noexcept final 
        { 
          return std::shared_ptr<void>(my_ptr, p);  
        }

        // The held shared_ptr
        std::shared_ptr<T>  my_ptr;

      };

      using storage_t = typename std::aligned_storage<sizeof(Holder<void>), std::alignment_of<Holder<void>>::value>::type;

      const std::type_info *  my_type_info{ nullptr };
      // Inplace storage to hold Holder<T>
      storage_t               my_inplace_storage;

      // Pointer to my_inplace_storage that holds Holder<T> 
      const IHolder *  holder() const noexcept 
      { 
        // [[gsl::suppress(type.1)]]  // warning C26490 : Don't use reinterpret_cast. (type.1)
        return reinterpret_cast<const IHolder *>(&my_inplace_storage); 
      }

      template<typename T>
      friend std::shared_ptr<T> any_shared_ptr_cast(any_shared_ptr const & anySharedPtr);

#ifdef ANY_SHARED_PTR_HAS_LIB_OPTIONAL
      template<typename T>
      friend std::optional<std::shared_ptr<T>> any_shared_ptr_cast(any_shared_ptr const * anySharedPtr) noexcept;
#else // replace std::optional<std::shared_ptr<T>> with std::pair<std::shared_ptr<T>,bool>
      template<typename T>
      std::pair<std::shared_ptr<T>, bool> any_shared_ptr_cast(any_shared_ptr const * anySharedPtr) noexcept;
#endif

    };

    //-----------------------------------------------------------------------------------------------------
    // Implementation

    template<typename T>
    any_shared_ptr::any_shared_ptr(std::shared_ptr<T> ptr) noexcept
      : my_type_info( & typeid(HeldType<T>) )
    {
      ::new (&my_inplace_storage) Holder<T>(std::move(ptr));
    }

    // [[gsl::suppress(type.6)]] //  warning C26495 : Variable 'xxx::ver_1::any_shared_ptr::my_inplace_storage' is uninitialized.Always initialize a member variable. (type.6)
    inline any_shared_ptr::any_shared_ptr() noexcept
    {
      ::new (&my_inplace_storage) Holder<void>();
    }

    inline any_shared_ptr::~any_shared_ptr()
    {
      holder()->~IHolder();
{'fraction_non_alphanumeric': 0.15597747093023256, 'fraction_numerical': 0.005723110465116279, 'mean_word_length': 2.233186490455213, 'pattern_counts': {'":': 0, '<': 86, '<?xml version=': 0, '>': 88, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7286023', 'n_tokens_mistral': 3398, 'n_tokens_neox': 3194, 'n_words': 794}