<commit_before>/*************************************************************************
> File Name: TaskMeta.h
> Project Name: Hearthstonepp
> Author: <NAME>
> Purpose: Meta data structure for managing task
> Created Time: 2018/05/20
> Copyright (c) 2018, <NAME>
*************************************************************************/
#ifndef HEARTHSTONEPP_TASKMETA_H
#define HEARTHSTONEPP_TASKMETA_H

#include <Enums/TaskEnums.h>
#include <Managers/Player.h>
#include <Tasks/MetaData.h>

#include <Flatbuffers/generated/FlatData_generated.h>

#include <memory>

namespace Hearthstonepp
{
using BYTE = unsigned char;

// Abstract of TaskMeta, store default meta data
// `id(TaskID)`, `status(MetaData)`, `accountID(unsigned char)`
struct TaskMetaTrait
{
    static constexpr BYTE USER_INVALID = Player::USER_INVALID;

    TaskID id;
    MetaData status;
    BYTE userID;

    TaskMetaTrait();
    TaskMetaTrait(TaskID id);
    TaskMetaTrait(TaskID id, MetaData status);
    TaskMetaTrait(TaskID id, MetaData status, BYTE userID);

    TaskMetaTrait(const TaskMetaTrait& trait);
    TaskMetaTrait& operator=(const TaskMetaTrait&);

    bool operator==(const TaskMetaTrait& trait) const;
};

// Meta data of run Task.
class TaskMeta : public TaskMetaTrait
{
 public:
    TaskMeta();
    TaskMeta(const TaskMetaTrait& trait);

    TaskMeta(const TaskMetaTrait& trait, size_t size, const BYTE* buffer);
    TaskMeta(const TaskMetaTrait& trait, size_t size,
             std::unique_ptr<BYTE[]>&& buffer);

    // Non copy-assignable object, only movable.
    // noexcept by move constructor of std::unique_ptr
    TaskMeta(TaskMeta&& meta) noexcept;
    TaskMeta(const TaskMeta&) = delete;

    TaskMeta& operator=(TaskMeta&& meta);
    TaskMeta& operator=(const TaskMeta&) = delete;

    bool operator==(const TaskMeta& meta) const;

    // Deep copy of TaskMeta
    static TaskMeta CopyFrom(const TaskMeta& meta);
    // Convert from FlatData::TaskMeta, deep copy of byte data
    static TaskMeta ConvertFrom(const FlatData::TaskMeta* meta);

    template <typename T>
    static inline const T* ConvertTo(const TaskMeta& meta)
    {
        const auto& buffer = meta.GetConstBuffer();
        return flatbuffers::GetRoot<T>(buffer.get());
    }

    // Reset std::unique_ptr and size data
    void reset();
    size_t GetBufferSize() const;

    std::unique_ptr<BYTE[]>&& MoveBuffer();
    const std::unique_ptr<BYTE[]>& GetConstBuffer() const;

 private:
    size_t m_size;
    std::unique_ptr<BYTE[]> m_buffer;
};
}  // namespace Hearthstonepp

#endif  // HEARTHSTONEPP_TASKMETA_H
<commit_msg>Update TaskMeta - Add Null pointer validation<commit_after>/*************************************************************************
> File Name: TaskMeta.h
> Project Name: Hearthstonepp
> Author: <NAME>
> Purpose: Meta data structure for managing task
> Created Time: 2018/05/20
> Copyright (c) 2018, <NAME>
*************************************************************************/
#ifndef HEARTHSTONEPP_TASKMETA_H
#define HEARTHSTONEPP_TASKMETA_H

#include <Enums/TaskEnums.h>
#include <Managers/Player.h>
#include <Tasks/MetaData.h>

#include <Flatbuffers/generated/FlatData_generated.h>

#include <memory>

namespace Hearthstonepp
{
using BYTE = unsigned char;

// Abstract of TaskMeta, store default meta data
// `id(TaskID)`, `status(MetaData)`, `accountID(unsigned char)`
struct TaskMetaTrait
{
    static constexpr BYTE USER_INVALID = Player::USER_INVALID;

    TaskID id;
    MetaData status;
    BYTE userID;

    TaskMetaTrait();
    TaskMetaTrait(TaskID id);
    TaskMetaTrait(TaskID id, MetaData status);
    TaskMetaTrait(TaskID id, MetaData status, BYTE userID);

    TaskMetaTrait(const TaskMetaTrait& trait);
    TaskMetaTrait& operator=(const TaskMetaTrait&);

    bool operator==(const TaskMetaTrait& trait) const;
};

// Meta data of run Task.
class TaskMeta : public TaskMetaTrait
{
 public:
    TaskMeta();
    TaskMeta(const TaskMetaTrait& trait);

    TaskMeta(const TaskMetaTrait& trait, size_t size, const BYTE* buffer);
    TaskMeta(const TaskMetaTrait& trait, size_t size,
             std::unique_ptr<BYTE[]>&& buffer);

    // Non copy-assignable object, only movable.
    // noexcept by move constructor of std::unique_ptr
    TaskMeta(TaskMeta&& meta) noexcept;
    TaskMeta(const TaskMeta&) = delete;

    TaskMeta& operator=(TaskMeta&& meta);
    TaskMeta& operator=(const TaskMeta&) = delete;

    bool operator==(const TaskMeta& meta) const;

    // Deep copy of TaskMeta
    static TaskMeta CopyFrom(const TaskMeta& meta);
    // Convert from FlatData::TaskMeta, deep copy of byte data
    static TaskMeta ConvertFrom(const FlatData::TaskMeta* meta);

    template <typename T>
    static inline const T* ConvertTo(const TaskMeta& meta)
    {
        const auto& buffer = meta.GetConstBuffer();
        if (buffer.get() == nullptr)
        {
            return nullptr;
        }
        return flatbuffers::GetRoot<T>(buffer.get());
    }

    // Reset std::unique_ptr and size data
    void reset();
    size_t GetBufferSize() const;

    std::unique_ptr<BYTE[]>&& MoveBuffer();
    const std::unique_ptr<BYTE[]>& GetConstBuffer() const;

 private:
    size_t m_size;
    std::unique_ptr<BYTE[]> m_buffer;
};
}  // namespace Hearthstonepp

#endif  // HEARTHSTONEPP_TASKMETA_H

{'fraction_non_alphanumeric': 0.1611811171713372, 'fraction_numerical': 0.00451382358472823, 'mean_word_length': 4.218842001962709, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 41, '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': '7358331', 'n_tokens_mistral': 1678, 'n_tokens_neox': 1601, 'n_words': 434}