<commit_before>/*
 * Copyright 2017-present Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 *
 * Author: <NAME> <<EMAIL>>
 */

#include <folly/Portability.h>

namespace folly {

template <class Fn>
struct exception_wrapper::arg_type_
    : public arg_type_<decltype(&Fn::operator())> {
};
template <class Ret, class Class, class Arg>
struct exception_wrapper::arg_type_<Ret (Class::*)(Arg)> {
  using type = Arg;
};
template <class Ret, class Class, class Arg>
struct exception_wrapper::arg_type_<Ret (Class::*)(Arg) const> {
  using type = Arg;
};
template <class Ret, class Arg>
struct exception_wrapper::arg_type_<Ret (Arg)> {
  using type = Arg;
};
template <class Ret, class Arg>
struct exception_wrapper::arg_type_<Ret (*)(Arg)> {
  using type = Arg;
};
template <class Ret, class Class>
struct exception_wrapper::arg_type_<Ret (Class::*)(...)> {
  using type = AnyException;
};
template <class Ret, class Class>
struct exception_wrapper::arg_type_<Ret (Class::*)(...) const> {
  using type = AnyException;
};
template <class Ret>
struct exception_wrapper::arg_type_<Ret (...)> {
  using type = AnyException;
};
template <class Ret>
struct exception_wrapper::arg_type_<Ret (*)(...)> {
  using type = AnyException;
};

template <class Ret, class... Args>
inline Ret exception_wrapper::noop_(Args...) {
  return Ret();
}

inline std::type_info const* exception_wrapper::uninit_type_(
    exception_wrapper const*) {
  return &typeid(void);
}

template <class Ex, typename... As>
inline exception_wrapper::Buffer::Buffer(in_place_type_t<Ex>, As&&... as_) {
  ::new (static_cast<void*>(&buff_)) Ex(std::forward<As>(as_)...);
}

template <class Ex>
inline Ex& exception_wrapper::Buffer::as() noexcept {
  return *static_cast<Ex*>(static_cast<void*>(&buff_));
}
template <class Ex>
inline Ex const& exception_wrapper::Buffer::as() const noexcept {
  return *static_cast<Ex const*>(static_cast<void const*>(&buff_));
}

inline std::exception const* exception_wrapper::as_exception_or_null_(
    std::exception const& ex) {
  return &ex;
}
inline std::exception const* exception_wrapper::as_exception_or_null_(
    AnyException) {
  return nullptr;
}

static_assert(
    !kIsWindows || sizeof(void*) == 8,
    "exception_wrapper is untested on 32 bit Windows.");
static_assert(
    !kIsWindows || (kMscVer >= 1900 && kMscVer <= 2000),
    "exception_wrapper is untested and possibly broken on your version of "
    "MSVC");

inline std::uintptr_t exception_wrapper::ExceptionPtr::as_int_(
    std::exception_ptr const& ptr,
    std::exception const& e) {
  if (!kIsWindows) {
    return reinterpret_cast<std::uintptr_t>(&e);
  } else {
    // On Windows, as of MSVC2017, all thrown exceptions are copied to the stack
    // first. Thus, we cannot depend on exception references associated with an
    // exception_ptr to be live for the duration of the exception_ptr. We need
    // to directly access the heap allocated memory inside the exception_ptr.
    //
    // std::exception_ptr is an opaque reinterpret_cast of
    // std::shared_ptr<__ExceptionPtr>
    // __ExceptionPtr is a non-virtual class with two members, a union and a
    // bool. The union contains the now-undocumented EHExceptionRecord, which
    // contains a struct which contains a void* which points to the heap
    // allocated exception.
    // We derive the offset to pExceptionObject via manual means.
    FOLLY_PACK_PUSH
    struct Win32ExceptionPtr {
      char offset[40];
      void* exceptionObject;
    } FOLLY_PACK_ATTR;
    FOLLY_PACK_POP

    auto* win32ExceptionPtr =
        reinterpret_cast<std::shared_ptr<Win32ExceptionPtr> const*>(
            &ptr)->get();
    return reinterpret_cast<std::uintptr_t>(win32ExceptionPtr->exceptionObject);
  }
}
inline std::uintptr_t exception_wrapper::ExceptionPtr::as_int_(
    std::exception_ptr const&,
    AnyException e) {
  return reinterpret_cast<std::uintptr_t>(e.typeinfo_) + 1;
}
inline bool exception_wrapper::ExceptionPtr::has_exception_() const {
  return 0 == exception_or_type_ % 2;
}
inline std::exception const* exception_wrapper::ExceptionPtr::as_exception_()
    const {
  return reinterpret_cast<std::exception const*>(exception_or_type_);
}
inline std::type_info const* exception_wrapper::ExceptionPtr::as_type_() const {
  return reinterpret_cast<std::type_info const*>(exception_or_type_ - 1);
}

inline void exception_wrapper::ExceptionPtr::copy_(
    exception_wrapper const* from, exception_wrapper* to) {
  ::new (static_cast<void*>(&to->eptr_)) ExceptionPtr(from->eptr_);
}
inline void exception_wrapper::ExceptionPtr::move_(<commit_msg>Support 32-bit Windows in exception wrapper<commit_after>/*
 * Copyright 2017-present Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 *
 * Author: <NAME> <<EMAIL>>
 */

#include <folly/Portability.h>

namespace folly {

template <class Fn>
struct exception_wrapper::arg_type_
    : public arg_type_<decltype(&Fn::operator())> {
};
template <class Ret, class Class, class Arg>
struct exception_wrapper::arg_type_<Ret (Class::*)(Arg)> {
  using type = Arg;
};
template <class Ret, class Class, class Arg>
struct exception_wrapper::arg_type_<Ret (Class::*)(Arg) const> {
  using type = Arg;
};
template <class Ret, class Arg>
struct exception_wrapper::arg_type_<Ret (Arg)> {
  using type = Arg;
};
template <class Ret, class Arg>
struct exception_wrapper::arg_type_<Ret (*)(Arg)> {
  using type = Arg;
};
template <class Ret, class Class>
struct exception_wrapper::arg_type_<Ret (Class::*)(...)> {
  using type = AnyException;
};
template <class Ret, class Class>
struct exception_wrapper::arg_type_<Ret (Class::*)(...) const> {
  using type = AnyException;
};
template <class Ret>
struct exception_wrapper::arg_type_<Ret (...)> {
  using type = AnyException;
};
template <class Ret>
struct exception_wrapper::arg_type_<Ret (*)(...)> {
  using type = AnyException;
};

template <class Ret, class... Args>
inline Ret exception_wrapper::noop_(Args...) {
  return Ret();
}

inline std::type_info const* exception_wrapper::uninit_type_(
    exception_wrapper const*) {
  return &typeid(void);
}

template <class Ex, typename... As>
inline exception_wrapper::Buffer::Buffer(in_place_type_t<Ex>, As&&... as_) {
  ::new (static_cast<void*>(&buff_)) Ex(std::forward<As>(as_)...);
}

template <class Ex>
inline Ex& exception_wrapper::Buffer::as() noexcept {
  return *static_cast<Ex*>(static_cast<void*>(&buff_));
}
template <class Ex>
inline Ex const& exception_wrapper::Buffer::as() const noexcept {
  return *static_cast<Ex const*>(static_cast<void const*>(&buff_));
}

inline std::exception const* exception_wrapper::as_exception_or_null_(
    std::exception const& ex) {
  return &ex;
}
inline std::exception const* exception_wrapper::as_exception_or_null_(
    AnyException) {
  return nullptr;
}

static_assert(
    !kIsWindows || (kMscVer >= 1900 && kMscVer <= 2000),
    "exception_wrapper is untested and possibly broken on your version of "
    "MSVC");

inline std::uintptr_t exception_wrapper::ExceptionPtr::as_int_(
    std::exception_ptr const& ptr,
    std::exception const& e) {
  if (!kIsWindows) {
    return reinterpret_cast<std::uintptr_t>(&e);
  } else {
    // On Windows, as of MSVC2017, all thrown exceptions are copied to the stack
    // first. Thus, we cannot depend on exception references associated with an
    // exception_ptr to be live for the duration of the exception_ptr. We need
    // to directly access the heap allocated memory inside the exception_ptr.
    //
    // std::exception_ptr is an opaque reinterpret_cast of
    // std::shared_ptr<__ExceptionPtr>
    // __ExceptionPtr is a non-virtual class with two members, a union and a
    // bool. The union contains the now-undocumented EHExceptionRecord, which
    // contains a struct which contains a void* which points to the heap
    // allocated exception.
    // We derive the offset to pExceptionObject via manual means.
    FOLLY_PACK_PUSH
    struct Win32ExceptionPtr {
      char offset[8 + 4 * sizeof(void*)];
      void* exceptionObject;
    } FOLLY_PACK_ATTR;
    FOLLY_PACK_POP

    auto* win32ExceptionPtr =
        reinterpret_cast<std::shared_ptr<Win32ExceptionPtr> const*>(
            &ptr)->get();
    return reinterpret_cast<std::uintptr_t>(win32ExceptionPtr->exceptionObject);
  }
}
inline std::uintptr_t exception_wrapper::ExceptionPtr::as_int_(
    std::exception_ptr const&,
    AnyException e) {
  return reinterpret_cast<std::uintptr_t>(e.typeinfo_) + 1;
}
inline bool exception_wrapper::ExceptionPtr::has_exception_() const {
  return 0 == exception_or_type_ % 2;
}
inline std::exception const* exception_wrapper::ExceptionPtr::as_exception_()
    const {
  return reinterpret_cast<std::exception const*>(exception_or_type_);
}
inline std::type_info const* exception_wrapper::ExceptionPtr::as_type_() const {
  return reinterpret_cast<std::type_info const*>(exception_or_type_ - 1);
}

inline void exception_wrapper::ExceptionPtr::copy_(
    exception_wrapper const* from, exception_wrapper* to) {
  ::new (static_cast<void*>(&to->eptr_)) ExceptionPtr(from->eptr_);
}
inline void exception_wrapper::ExceptionPtr::move_(
{'fraction_non_alphanumeric': 0.12325672755843646, 'fraction_numerical': 0.00716951483009232, 'mean_word_length': 4.756359525155455, 'pattern_counts': {'":': 0, '<': 89, '<?xml version=': 0, '>': 97, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6932042', 'n_tokens_mistral': 3103, 'n_tokens_neox': 3035, 'n_words': 909}