<commit_before>// Copyright (C) 2010-2013 <NAME>.
// See the file COPYING for copying permission.

#pragma once

#include <windows.h>

#include <hadesmem/error.hpp>
#include <hadesmem/config.hpp>
#include <hadesmem/detail/assert.hpp>

namespace hadesmem
{

namespace detail
{

template <typename Policy>
class SmartHandleImpl
{
public:
  typedef typename Policy::HandleT HandleT;

  HADESMEM_CONSTEXPR SmartHandleImpl() HADESMEM_NOEXCEPT
    : handle_(nullptr)
  { }
  
  explicit HADESMEM_CONSTEXPR SmartHandleImpl(HandleT handle) HADESMEM_NOEXCEPT
    : handle_(handle)
  { }

  SmartHandleImpl& operator=(HandleT handle) HADESMEM_NOEXCEPT
  {
    CleanupUnchecked();

    handle_ = handle;

    return *this;
  }

  SmartHandleImpl(SmartHandleImpl&& other) HADESMEM_NOEXCEPT
    : handle_(other.handle_)
  {
    other.handle_ = GetInvalid();
  }

  SmartHandleImpl& operator=(SmartHandleImpl&& other) HADESMEM_NOEXCEPT
  {
    CleanupUnchecked();

    handle_ = other.handle_;
    other.handle_ = other.GetInvalid();

    return *this;
  }

  ~SmartHandleImpl() HADESMEM_NOEXCEPT
  {
    CleanupUnchecked();
  }

  HandleT GetHandle() const HADESMEM_NOEXCEPT
  {
    return handle_;
  }

  HADESMEM_CONSTEXPR HandleT GetInvalid() const HADESMEM_NOEXCEPT
  {
    return Policy::GetInvalid();
  }

  bool IsValid() const HADESMEM_NOEXCEPT
  {
    return GetHandle() != GetInvalid();
  }

  void Cleanup()
  {
    if (handle_ == GetInvalid())
    {
      return;
    }

    if (!Policy::Cleanup(handle_))
    {
      DWORD const last_error = ::GetLastError();
      HADESMEM_THROW_EXCEPTION(Error() << 
        ErrorString("SmartHandle cleanup failed.") << 
        ErrorCodeWinLast(last_error));
    }

    handle_ = GetInvalid();
  }

private:
  SmartHandleImpl(SmartHandleImpl const& other) HADESMEM_DELETED_FUNCTION;
  SmartHandleImpl& operator=(SmartHandleImpl const& other) 
    HADESMEM_DELETED_FUNCTION;

  void CleanupUnchecked() HADESMEM_NOEXCEPT
  {
    try
    {
      Cleanup();
    }
    catch (std::exception const& e)
    {
      (void)e;

      // WARNING: Handle is leaked if 'Cleanup' fails.
      HADESMEM_ASSERT(boost::diagnostic_information(e).c_str() && false);

      handle_ = GetInvalid();
    }
  }

  HandleT handle_;
  Policy policy_;
};

struct HandlePolicy
{
  typedef HANDLE HandleT;

  static HADESMEM_CONSTEXPR HandleT GetInvalid() HADESMEM_NOEXCEPT
  {
    return nullptr;
  }

  static bool Cleanup(HandleT handle)
  {
    return ::CloseHandle(handle) != 0;
  }
};

typedef SmartHandleImpl<HandlePolicy> SmartHandle;

struct SnapPolicy
{
  typedef HANDLE HandleT;

  static HADESMEM_CONSTEXPR HandleT GetInvalid() HADESMEM_NOEXCEPT
  {
    return INVALID_HANDLE_VALUE;
  }

  static bool Cleanup(HandleT handle)
  {
    return ::CloseHandle(handle) != 0;
  }
};

typedef SmartHandleImpl<SnapPolicy> SmartSnapHandle;

struct LibraryPolicy
{
  typedef HMODULE HandleT;

  static HADESMEM_CONSTEXPR HandleT GetInvalid() HADESMEM_NOEXCEPT
  {
    return nullptr;
  }

  static bool Cleanup(HandleT handle)
  {
    return ::FreeLibrary(handle) != 0;
  }
};

typedef SmartHandleImpl<LibraryPolicy> SmartModuleHandle;
<commit_msg>* Remove invalid constexpr addition. INVALID_HANDLE_VALUE uses a reinterpret_cast.<commit_after>// Copyright (C) 2010-2013 <NAME>.
// See the file COPYING for copying permission.

#pragma once

#include <windows.h>

#include <hadesmem/error.hpp>
#include <hadesmem/config.hpp>
#include <hadesmem/detail/assert.hpp>

namespace hadesmem
{

namespace detail
{

template <typename Policy>
class SmartHandleImpl
{
public:
  typedef typename Policy::HandleT HandleT;

  HADESMEM_CONSTEXPR SmartHandleImpl() HADESMEM_NOEXCEPT
    : handle_(nullptr)
  { }
  
  explicit HADESMEM_CONSTEXPR SmartHandleImpl(HandleT handle) HADESMEM_NOEXCEPT
    : handle_(handle)
  { }

  SmartHandleImpl& operator=(HandleT handle) HADESMEM_NOEXCEPT
  {
    CleanupUnchecked();

    handle_ = handle;

    return *this;
  }

  SmartHandleImpl(SmartHandleImpl&& other) HADESMEM_NOEXCEPT
    : handle_(other.handle_)
  {
    other.handle_ = GetInvalid();
  }

  SmartHandleImpl& operator=(SmartHandleImpl&& other) HADESMEM_NOEXCEPT
  {
    CleanupUnchecked();

    handle_ = other.handle_;
    other.handle_ = other.GetInvalid();

    return *this;
  }

  ~SmartHandleImpl() HADESMEM_NOEXCEPT
  {
    CleanupUnchecked();
  }

  HandleT GetHandle() const HADESMEM_NOEXCEPT
  {
    return handle_;
  }

  HandleT GetInvalid() const HADESMEM_NOEXCEPT
  {
    return Policy::GetInvalid();
  }

  bool IsValid() const HADESMEM_NOEXCEPT
  {
    return GetHandle() != GetInvalid();
  }

  void Cleanup()
  {
    if (handle_ == GetInvalid())
    {
      return;
    }

    if (!Policy::Cleanup(handle_))
    {
      DWORD const last_error = ::GetLastError();
      HADESMEM_THROW_EXCEPTION(Error() << 
        ErrorString("SmartHandle cleanup failed.") << 
        ErrorCodeWinLast(last_error));
    }

    handle_ = GetInvalid();
  }

private:
  SmartHandleImpl(SmartHandleImpl const& other) HADESMEM_DELETED_FUNCTION;
  SmartHandleImpl& operator=(SmartHandleImpl const& other) 
    HADESMEM_DELETED_FUNCTION;

  void CleanupUnchecked() HADESMEM_NOEXCEPT
  {
    try
    {
      Cleanup();
    }
    catch (std::exception const& e)
    {
      (void)e;

      // WARNING: Handle is leaked if 'Cleanup' fails.
      HADESMEM_ASSERT(boost::diagnostic_information(e).c_str() && false);

      handle_ = GetInvalid();
    }
  }

  HandleT handle_;
  Policy policy_;
};

struct HandlePolicy
{
  typedef HANDLE HandleT;

  static HADESMEM_CONSTEXPR HandleT GetInvalid() HADESMEM_NOEXCEPT
  {
    return nullptr;
  }

  static bool Cleanup(HandleT handle)
  {
    return ::CloseHandle(handle) != 0;
  }
};

typedef SmartHandleImpl<HandlePolicy> SmartHandle;

struct SnapPolicy
{
  typedef HANDLE HandleT;

  static HandleT GetInvalid() HADESMEM_NOEXCEPT
  {
    return INVALID_HANDLE_VALUE;
  }

  static bool Cleanup(HandleT handle)
  {
    return ::CloseHandle(handle) != 0;
  }
};

typedef SmartHandleImpl<SnapPolicy> SmartSnapHandle;

struct LibraryPolicy
{
  typedef HMODULE HandleT;

  static HADESMEM_CONSTEXPR HandleT GetInvalid() HADESMEM_NOEXCEPT
  {
    return nullptr;
  }

  static bool Cleanup(HandleT handle)
  {
    return ::FreeLibrary(handle) != 0;
  }
};

typedef SmartHandleImpl<LibraryPolicy> SmartModuleHandle;

{'fraction_non_alphanumeric': 0.09938573003622618, 'fraction_numerical': 0.003465112616160025, 'mean_word_length': 3.679439941046426, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2882593', 'n_tokens_mistral': 2227, 'n_tokens_neox': 2054, 'n_words': 436}