<commit_before>// Copyright (c) 2009 <NAME> <<EMAIL>>
//
// This file is part of libsfz, a free software project.  You can redistribute it and/or modify it
// under the terms of the MIT License.

#ifndef SFZ_PRINT_TARGET_HPP_
#define SFZ_PRINT_TARGET_HPP_

#include <stdlib.h>
#include "sfz/Rune.hpp"

namespace sfz {

class BytesPiece;
class Encoding;
class String;
class StringPiece;

class PrintTarget {
  public:
    template <typename T>
    PrintTarget(T* target) {
        new (impl_) Derived<T>(target);
    }

    inline void append(const String& string) { base()->append(string); }
    inline void append(const StringPiece& string) { base()->append(string); }
    inline void append(const BytesPiece& bytes, const Encoding& encoding) {
        base()->append(bytes, encoding); }
    inline void append(size_t num, Rune rune) { base()->append(num, rune); }

  private:
    struct Base {
        virtual ~Base() { }
        virtual void append(const String& string) = 0;
        virtual void append(const StringPiece& string) = 0;
        virtual void append(const BytesPiece& bytes, const Encoding& encoding) = 0;
        virtual void append(size_t num, Rune rune) = 0;
    };

    template <typename T>
    struct Derived : public Base {
        Derived(T* target)
            : target_(target) { }

        virtual void append(const String& string) { target_->append(string); }
        virtual void append(const StringPiece& string) { target_->append(string); }
        virtual void append(const BytesPiece& bytes, const Encoding& encoding) {
            target_->append(bytes, encoding); }
        virtual void append(size_t num, Rune rune) { target_->append(num, rune); }

        T* const target_;
    };

    inline Base* base() {
        return reinterpret_cast<Base*>(&impl_);
    };

    intptr_t impl_[2];

    // ALLOW_COPY_AND_ASSIGN
};

}  // namespace sfz

#endif  // SFZ_PRINT_TARGET_HPP_<commit_msg>Change dispatch style in PrintTarget.<commit_after>// Copyright (c) 2009 <NAME> <<EMAIL>>
//
// This file is part of libsfz, a free software project.  You can redistribute it and/or modify it
// under the terms of the MIT License.

#ifndef SFZ_PRINT_TARGET_HPP_
#define SFZ_PRINT_TARGET_HPP_

#include <stdlib.h>
#include "sfz/Rune.hpp"

namespace sfz {

class BytesPiece;
class Encoding;
class String;
class StringPiece;

class PrintTarget {
  public:
    template <typename T> PrintTarget(T* t);

    inline void append(const String& string);
    inline void append(const StringPiece& string);
    inline void append(const BytesPiece& bytes, const Encoding& encoding);
    inline void append(size_t num, Rune rune);

  private:
    struct DispatchTable {
        void (*append_string)(void* target, const String& string);
        void (*append_string_piece)(void* target, const StringPiece& string);
        void (*append_bytes)(void* target, const BytesPiece& bytes, const Encoding& encoding);
        void (*append_runes)(void* target, size_t num, Rune rune);
    };

    template <typename T> struct Dispatch;

    void* const _target;
    const DispatchTable* const _dispatch_table;

    // ALLOW_COPY_AND_ASSIGN
};

template <typename T>
struct PrintTarget::Dispatch {
    static void append_string(void* target, const String& string) {
        reinterpret_cast<T*>(target)->append(string);
    }
    static void append_string_piece(void* target, const StringPiece& string) {
        reinterpret_cast<T*>(target)->append(string);
    }
    static void append_bytes(void* target, const BytesPiece& bytes, const Encoding& encoding) {
        reinterpret_cast<T*>(target)->append(bytes, encoding);
    }
    static void append_runes(void* target, size_t num, Rune rune) {
        reinterpret_cast<T*>(target)->append(num, rune);
    }
    static const DispatchTable table;
};

template <typename T>
const PrintTarget::DispatchTable PrintTarget::Dispatch<T>::table = {
    append_string,
    append_string_piece,
    append_bytes,
    append_runes,
};

template <typename T>
PrintTarget::PrintTarget(T* t)
    : _target(t),
      _dispatch_table(&Dispatch<T>::table) { }

inline void PrintTarget::append(const String& string) {
    _dispatch_table->append_string(_target, string);
}

inline void PrintTarget::append(const StringPiece& string) {
    _dispatch_table->append_string_piece(_target, string);
}

inline void PrintTarget::append(const BytesPiece& bytes, const Encoding& encoding) {
    _dispatch_table->append_bytes(_target, bytes, encoding);
}

inline void PrintTarget::append(size_t num, Rune rune) {
    _dispatch_table->append_runes(_target, num, rune);
}

}  // namespace sfz

#endif  // SFZ_PRINT_TARGET_HPP_
{'fraction_non_alphanumeric': 0.11244893571274994, 'fraction_numerical': 0.0027950978284239946, 'mean_word_length': 3.7615148413510746, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 42, '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': '4909373', 'n_tokens_mistral': 1450, 'n_tokens_neox': 1413, 'n_words': 406}