<commit_before>#pragma once

#include "ts_assert.h"
#include "Source_Location.h"

namespace ts
{

namespace detail
{
    struct success_helper{};
}

typedef int detail::success_helper::*success_t;
success_t const success = (static_cast<success_t>(0));

//////////////////////////////////////////////////////////////////////////

class Error
{
public:

    Error(const Source_Location& source_location, const std::string& what)
        : m_source_location(source_location)
        , m_what(what)
	{}

    Error(const std::string& what)
        : m_source_location(Source_Location::from_cpp_source(__FILE__, __LINE__))
        , m_what(what)
	{}

    Source_Location const& source_location() const{ return m_source_location; }
    std::string const& what() const { return m_what; }

protected:

    Source_Location m_source_location;
    std::string m_what;
};

//////////////////////////////////////////////////////////////////////////

template<typename T>
class Result final
{
public:

    inline ~Result()
	{
        if (m_is_success)
		{
			reinterpret_cast<T*>(&m_data)->~T();
		}
		else
		{
            reinterpret_cast<Error*>(&m_data)->~Error();
		}
#ifndef NDEBUG
        TS_ASSERT(!m_check_pending);
#endif
	}

    inline Result(const Error& error)
        : m_is_success(false)
	{
        new (&m_data) Error(error);
	}

    inline Result(Error&& error)
        : m_is_success(false)
	{
        new (&m_data) Error(std::move(error));
	}

	template<typename T2>
    inline Result(T2&& payload)
        : m_is_success(true)
	{
		new (&m_data) T(std::forward<T2>(payload));
	}

	template<typename T2>
    inline Result(const Result<T2>& other) = delete;

	template<typename T2>
    inline Result(Result<T2>&& other)
        : m_is_success(other.m_is_success)
	{
    #ifndef NDEBUG
        m_check_pending = other.m_check_pending;
        other.m_check_pending = false;
	#endif

        if (other.m_is_success)
		{
			new (&m_data) T(std::move(*reinterpret_cast<T2*>(&other.m_data)));
		}
		else
		{
            new (&m_data) Error(std::move(*reinterpret_cast<Error*>(&other.m_data)));
		}
	}

	template<typename T2>
    inline Result<T>& operator = (const Result<T2>& other) = delete;

	template<typename T2>
    inline Result<T>& operator = (Result<T2>&& other)
	{
#ifndef NDEBUG
        m_check_pending = other.m_check_pending;
        other.m_check_pending = false;
#endif

        if (m_is_success)
		{
			reinterpret_cast<T*>(&m_data)->~T();
		}
		else
		{
            reinterpret_cast<Error*>(&m_data)->~Error();
		}
		
        m_is_success = other.m_is_success;

        if (other.m_is_success)
		{
			new (&m_data) T(std::move(*reinterpret_cast<T2*>(&other.m_data)));
		}
		else
		{
            new (&m_data) T(std::move(*reinterpret_cast<Error*>(&other.m_data)));
		}
		return *this;
	}

	inline T& payload()
	{
        TS_ASSERT(m_is_success);
		return *reinterpret_cast<T*>(&m_data);
	}


	inline T&& extract_payload()
	{
        TS_ASSERT(m_is_success);
		return std::move(*reinterpret_cast<T*>(&m_data));
	}

    inline const Error& error() const
	{
        TS_ASSERT(!m_is_success);
        return *reinterpret_cast<const Error*>(&m_data);
	}

//	explicit inline operator bool() const<commit_msg>Fixed construction bug in the equality op<commit_after>#pragma once

#include "ts_assert.h"
#include "Source_Location.h"

namespace ts
{

namespace detail
{
    struct success_helper{};
}

typedef int detail::success_helper::*success_t;
success_t const success = (static_cast<success_t>(0));

//////////////////////////////////////////////////////////////////////////

class Error
{
public:

    Error(const Source_Location& source_location, const std::string& what)
        : m_source_location(source_location)
        , m_what(what)
	{}

    Error(const std::string& what)
        : m_source_location(Source_Location::from_cpp_source(__FILE__, __LINE__))
        , m_what(what)
	{}

    Source_Location const& source_location() const{ return m_source_location; }
    std::string const& what() const { return m_what; }

protected:

    Source_Location m_source_location;
    std::string m_what;
};

//////////////////////////////////////////////////////////////////////////

template<typename T>
class Result final
{
public:

    inline ~Result()
	{
        if (m_is_success)
		{
			reinterpret_cast<T*>(&m_data)->~T();
		}
		else
		{
            reinterpret_cast<Error*>(&m_data)->~Error();
		}
#ifndef NDEBUG
        TS_ASSERT(!m_check_pending);
#endif
	}

    inline Result(const Error& error)
        : m_is_success(false)
	{
        new (&m_data) Error(error);
	}

    inline Result(Error&& error)
        : m_is_success(false)
	{
        new (&m_data) Error(std::move(error));
	}

	template<typename T2>
    inline Result(T2&& payload)
        : m_is_success(true)
	{
		new (&m_data) T(std::forward<T2>(payload));
	}

	template<typename T2>
    inline Result(const Result<T2>& other) = delete;

	template<typename T2>
    inline Result(Result<T2>&& other)
        : m_is_success(other.m_is_success)
	{
    #ifndef NDEBUG
        m_check_pending = other.m_check_pending;
        other.m_check_pending = false;
	#endif

        if (other.m_is_success)
		{
			new (&m_data) T(std::move(*reinterpret_cast<T2*>(&other.m_data)));
		}
		else
		{
            new (&m_data) Error(std::move(*reinterpret_cast<Error*>(&other.m_data)));
		}
	}

	template<typename T2>
    inline Result<T>& operator = (const Result<T2>& other) = delete;

	template<typename T2>
    inline Result<T>& operator = (Result<T2>&& other)
	{
#ifndef NDEBUG
        m_check_pending = other.m_check_pending;
        other.m_check_pending = false;
#endif

        if (m_is_success)
		{
			reinterpret_cast<T*>(&m_data)->~T();
		}
		else
		{
            reinterpret_cast<Error*>(&m_data)->~Error();
		}
		
        m_is_success = other.m_is_success;

        if (other.m_is_success)
		{
			new (&m_data) T(std::move(*reinterpret_cast<T2*>(&other.m_data)));
		}
		else
		{
            new (&m_data) Error(std::move(*reinterpret_cast<Error*>(&other.m_data)));
		}
		return *this;
	}

	inline T& payload()
	{
        TS_ASSERT(m_is_success);
		return *reinterpret_cast<T*>(&m_data);
	}


	inline T&& extract_payload()
	{
        TS_ASSERT(m_is_success);
		return std::move(*reinterpret_cast<T*>(&m_data));
	}

    inline const Error& error() const
	{
        TS_ASSERT(!m_is_success);
        return *reinterpret_cast<const Error*>(&m_data);
	}

//	explicit inline operator bool() const
{'fraction_non_alphanumeric': 0.18001855861429014, 'fraction_numerical': 0.004330343334364367, 'mean_word_length': 2.784084259801053, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 61, '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': '506063', 'n_tokens_mistral': 2317, 'n_tokens_neox': 2196, 'n_words': 385}