<commit_before>/*
   Copyright (C) 2014 <NAME> <<EMAIL>>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LGPL.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.

   Alternatively, this file is available under the Mozilla Public License
   Version 1.1.  You may obtain a copy of the License at
   http://www.mozilla.org/MPL/
*/

#ifndef MESSAGE_P_H
#define MESSAGE_P_H

#include "message.h"

#include "arguments.h"
#include "error.h"
#include "iconnectionclient.h"

class ICompletionClient;

class VarHeaderStorage {
public:
    VarHeaderStorage();
    VarHeaderStorage(const VarHeaderStorage &other);
    ~VarHeaderStorage();

    bool hasHeader(Message::VariableHeader header) const;

    bool hasStringHeader(Message::VariableHeader header) const;
    std::string stringHeader(Message::VariableHeader header) const;
    cstring stringHeaderRaw(Message::VariableHeader header);
    void setStringHeader(Message::VariableHeader header, const std::string &value);
    void clearStringHeader(Message::VariableHeader header);

    bool hasIntHeader(Message::VariableHeader header) const;
    uint32 intHeader(Message::VariableHeader header) const;
    void setIntHeader(Message::VariableHeader header, uint32 value);
    void clearIntHeader(Message::VariableHeader header);

    // for use during header deserialization: returns false if a header occurs twice,
    // but does not check if the given header is of the right type (int / string).
    bool setIntHeader_deser(Message::VariableHeader header, uint32 value);
    bool setStringHeader_deser(Message::VariableHeader header, cstring value);

    const std::string *stringHeaders() const
    {
        return reinterpret_cast<const std::string *>(m_stringStorage);
    }
    std::string *stringHeaders()
    {
        return reinterpret_cast<std::string *>(m_stringStorage);
    }

    static const int s_stringHeaderCount = 7;
    static const int s_intHeaderCount = 2;

    // Uninitialized storage for strings, to avoid con/destructing strings we'd never touch otherwise.
    // Since unsigned char * is incompatible with char *, it gives better compiler messages than char if
    // there is a problem; std::string deals with char * a lot.

    // the awkward position of alignas() is the only one that works with both g++ and clang++...
    unsigned char m_stringStorage alignas(std::string)
        [sizeof(std::string[VarHeaderStorage::s_stringHeaderCount])];
    uint32 m_intHeaders[s_intHeaderCount];
    uint32 m_headerPresenceBitmap = 0;
};

class MessagePrivate : public IConnectionClient
{
public:
    static MessagePrivate *get(Message *m) { return m->d; }

    MessagePrivate(Message *parent);
    MessagePrivate(const MessagePrivate &other, Message *parent);
    ~MessagePrivate();

    void notifyConnectionReadyRead() override;
    void notifyConnectionReadyWrite() override;
<commit_msg>VarHeaderStorage: use std::aligned_storage for its intended purpose.<commit_after>/*
   Copyright (C) 2014 <NAME> <<EMAIL>>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LGPL.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.

   Alternatively, this file is available under the Mozilla Public License
   Version 1.1.  You may obtain a copy of the License at
   http://www.mozilla.org/MPL/
*/

#ifndef MESSAGE_P_H
#define MESSAGE_P_H

#include "message.h"

#include "arguments.h"
#include "error.h"
#include "iconnectionclient.h"

#include <type_traits>

class ICompletionClient;

class VarHeaderStorage {
public:
    VarHeaderStorage();
    VarHeaderStorage(const VarHeaderStorage &other);
    ~VarHeaderStorage();

    bool hasHeader(Message::VariableHeader header) const;

    bool hasStringHeader(Message::VariableHeader header) const;
    std::string stringHeader(Message::VariableHeader header) const;
    cstring stringHeaderRaw(Message::VariableHeader header);
    void setStringHeader(Message::VariableHeader header, const std::string &value);
    void clearStringHeader(Message::VariableHeader header);

    bool hasIntHeader(Message::VariableHeader header) const;
    uint32 intHeader(Message::VariableHeader header) const;
    void setIntHeader(Message::VariableHeader header, uint32 value);
    void clearIntHeader(Message::VariableHeader header);

    // for use during header deserialization: returns false if a header occurs twice,
    // but does not check if the given header is of the right type (int / string).
    bool setIntHeader_deser(Message::VariableHeader header, uint32 value);
    bool setStringHeader_deser(Message::VariableHeader header, cstring value);

    const std::string *stringHeaders() const
    {
        return reinterpret_cast<const std::string *>(m_stringStorage);
    }
    std::string *stringHeaders()
    {
        return reinterpret_cast<std::string *>(m_stringStorage);
    }

    static const int s_stringHeaderCount = 7;
    static const int s_intHeaderCount = 2;

    // Uninitialized storage for strings, to avoid con/destructing strings we'd never touch otherwise.
    std::aligned_storage<sizeof(std::string)>::type m_stringStorage[VarHeaderStorage::s_stringHeaderCount];
    uint32 m_intHeaders[s_intHeaderCount];
    uint32 m_headerPresenceBitmap = 0;
};

class MessagePrivate : public IConnectionClient
{
public:
    static MessagePrivate *get(Message *m) { return m->d; }

    MessagePrivate(Message *parent);
    MessagePrivate(const MessagePrivate &other, Message *parent);
    ~MessagePrivate();

    void notifyConnectionReadyRead() override;
    void notifyConnectionReadyWrite() override;

{'fraction_non_alphanumeric': 0.07859751643535427, 'fraction_numerical': 0.009057706355003652, 'mean_word_length': 4.3652037617554855, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5077793', 'n_tokens_mistral': 1853, 'n_tokens_neox': 1771, 'n_words': 680}