<commit_before>/*************************************************************************
> File Name: SyncBuffer.h
> Project Name: Hearthstonepp
> Author: <NAME>
> Purpose: Synchronized Buffer
> Created Time: 2018/05/20
> Copyright (c) 2018, <NAME>
*************************************************************************/
#ifndef HEARTHSTONEPP_SYNCBUFFER_H
#define HEARTHSTONEPP_SYNCBUFFER_H

#include <condition_variable>
#include <mutex>

namespace Hearthstonepp
{
// BufferType must be copy-assignable and movable
template <typename BufferType>
class SyncBuffer
{
 public:
    SyncBuffer() : m_readable(false)
    {
        // Do Nothing
    }

    // Not copy-assignable
    SyncBuffer(SyncBuffer&&) = delete;

    SyncBuffer(const SyncBuffer&) = delete;

    SyncBuffer& operator=(SyncBuffer&&) = delete;

    SyncBuffer& operator=(const SyncBuffer&) = delete;

    void WriteBuffer(BufferType&& buffer)
    {
        std::unique_lock<std::mutex> lock(m_mtx);
        m_cond.wait(lock, [=]() { return !m_readable; });

        m_buffer = std::move(buffer);

        m_readable = true;
        m_cond.notify_one();
    }

    void WriteBuffer(const BufferType& buffer)
    {
        std::unique_lock<std::mutex> lock(m_mtx);
        m_cond.wait(lock, [=]() { return !m_readable; });

        m_buffer = buffer;

        m_readable = true;
        m_cond.notify_one();
    }

    void ReadBuffer(BufferType& buffer)
    {
        std::unique_lock<std::mutex> lock(m_mtx);
        m_cond.wait(lock, [=]() { return m_readable; });

        buffer = std::move(m_buffer);

        m_readable = false;
        m_cond.notify_one();
    }

    bool isReadable() const
    {
        return m_readable;
    }

 private:<commit_msg>[ci skip] [WIP] docs: Add comments for doxygen<commit_after>#ifndef HEARTHSTONEPP_SYNCBUFFER_H
#define HEARTHSTONEPP_SYNCBUFFER_H

#include <condition_variable>
#include <mutex>

namespace Hearthstonepp
{
//!
//! \brief SyncBuffer class.
//!
//! This class reads/writes synchronized buffer.
//! \tparam BufferType The type of buffer. (copy-assignable and movable)
//!
template <typename BufferType>
class SyncBuffer
{
 public:
    //! Default constructor.
    SyncBuffer() : m_readable(false)
    {
        // Do Nothing
    }

    //! Deleted copy constructor.
    SyncBuffer(const SyncBuffer&) = delete;

    //! Deleted move constructor.
    SyncBuffer(SyncBuffer&&) = delete;

    //! Deleted copy assignment operator.
    SyncBuffer& operator=(const SyncBuffer&) = delete;

    //! Deleted move assignment operator.
    SyncBuffer& operator=(SyncBuffer&&) = delete;

    //! Writes \p buffer to synchronized buffer.
    void WriteBuffer(BufferType&& buffer)
    {
        std::unique_lock<std::mutex> lock(m_mtx);
        m_cond.wait(lock, [=]() { return !m_readable; });

        m_buffer = std::move(buffer);

        m_readable = true;
        m_cond.notify_one();
    }

    //! Writes \p buffer to synchronized buffer.
    void WriteBuffer(const BufferType& buffer)
    {
        std::unique_lock<std::mutex> lock(m_mtx);
        m_cond.wait(lock, [=]() { return !m_readable; });

        m_buffer = buffer;

        m_readable = true;
        m_cond.notify_one();
    }

    //! Reads \p buffer from synchronized buffer.
    void ReadBuffer(BufferType& buffer)
    {
        std::unique_lock<std::mutex> lock(m_mtx);
        m_cond.wait(lock, [=]() { return m_readable; });

        buffer = std::move(m_buffer);

        m_readable = false;
        m_cond.notify_one();
    }

    //! Returns whether it is readable.
    bool isReadable() const
    {
        return m_readable;
    }

 private:
{'fraction_non_alphanumeric': 0.16290588882773804, 'fraction_numerical': 0.003302146395156852, 'mean_word_length': 2.981380065717415, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, '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': '6486534', 'n_tokens_mistral': 1193, 'n_tokens_neox': 1107, 'n_words': 245}