<commit_before>// Copyright 2010-2013 RethinkDB, all rights reserved.
#ifndef CONTAINERS_COUNTED_HPP_
#define CONTAINERS_COUNTED_HPP_

#include <stddef.h>
#include <stdint.h>

#include <utility>

#include "errors.hpp"

// Yes, this is a clone of boost::counted_t.  This will probably
// not be the case in the future.

// Now it supports .unique(), and in order to use it, your type needs
// to provide an counted_t_use_count implementation.

template <class T>
class counted_t {
public:
    counted_t() : p_(NULL) { }
    explicit counted_t(T *p) : p_(p) {
        if (p_) { counted_t_add_ref(p_); }
    }

    counted_t(const counted_t &copyee) : p_(copyee.p_) {
        if (p_) { counted_t_add_ref(p_); }
    }

    // TODO: Add noexcept on versions of compilers that support it.  noexcept is
    // good to have because types like std::vectors use it to see whether to
    // call the copy constructor or move constructor.
    counted_t(counted_t &&movee) : p_(movee.p_) {
        movee.p_ = NULL;
    }

    // TODO: Add noexcept on versions of compilers that support it.
    template <class U>
    counted_t(const counted_t<U> &&movee) : p_(movee.p_) {
        movee.p_ = NULL;
    }

    ~counted_t() {
        if (p_) { counted_t_release(p_); }
    }

    void swap(counted_t &other) {
        T *tmp = p_;
        p_ = other.p_;
        other.p_ = tmp;
    }

    // TODO: Add noexcept on versions of compilers that support it.
    counted_t &operator=(counted_t &&other) {
        counted_t tmp(std::move(other));
        swap(tmp);
        return *this;
    }

    // TODO: Add noexcept on versions of compilers that support it.
    template <class U>
    counted_t &operator=(counted_t<U> &&other) {
        counted_t tmp(std::move(other));
        swap(tmp);
        return *this;
    }

    counted_t &operator=(const counted_t &other) {
        counted_t tmp(other);
        swap(tmp);
        return *this;
    }

    void reset() {
        counted_t tmp;
        swap(tmp);
    }

    void reset(T *other) {
        counted_t tmp(other);
        swap(tmp);  // NOLINT
    }

    T *operator->() const {
        return p_;
    }

    T &operator*() const {
        return *p_;
    }

    T *get() const {
        return p_;
    }

    bool has() const {
        return p_ != NULL;
    }

    bool unique() const {
        return counted_t_use_count(p_) == 1;
    }

    class hidden_t {
        hidden_t();
    };
    typedef void booleanesque_t(hidden_t);

    operator booleanesque_t*() const {
        return p_ ? &counted_t<T>::dummy_method : 0;
    }

private:
    static void dummy_method(hidden_t) { }

    T *p_;
};

template <class> class single_threaded_shared_mixin_t;

template <class T>
inline void counted_t_add_ref(single_threaded_shared_mixin_t<T> *p);
template <class T>
inline void counted_t_release(single_threaded_shared_mixin_t<T> *p);
template <class T>
inline intptr_t counted_t_use_count(const single_threaded_shared_mixin_t<T> *p);

template <class T>
class single_threaded_shared_mixin_t {
public:
    single_threaded_shared_mixin_t() : refcount_(0) { }

protected:
    ~single_threaded_shared_mixin_t() { }

private:
    friend void counted_t_add_ref<T>(single_threaded_shared_mixin_t<T> *p);
    friend void counted_t_release<T>(single_threaded_shared_mixin_t<T> *p);
    friend intptr_t counted_t_use_count<T>(const single_threaded_shared_mixin_t<T> *p);

    intptr_t refcount_;
    DISABLE_COPYING(single_threaded_shared_mixin_t);
};

template <class T>
inline void counted_t_add_ref(single_threaded_shared_mixin_t<T> *p) {
    p->refcount_ += 1;
    rassert(p->refcount_ > 0);
}

template <class T>
inline void counted_t_release(single_threaded_shared_mixin_t<T> *p) {
    rassert(p->refcount_ > 0);
    --p->refcount_;
    if (p->refcount_ == 0) {
        delete static_cast<T *>(p);
    }
}

template <class T>
inline intptr_t counted_t_use_count(const single_threaded_shared_mixin_t<T> *p) {
    return p->refcount_;
}







template <class> class slow_shared_mixin_t;

template <class T>
inline void counted_t_add_ref(slow_shared_mixin_t<T> *p);
template <class T>
inline void counted_t_release(slow_shared_mixin_t<T> *p);
template <class T>
inline intptr_t counted_t_use_count(const slow_shared_mixin_t<T> *p);
<commit_msg>Made single_threaded_shared_mixin_t support const types in template parameter.<commit_after>// Copyright 2010-2013 RethinkDB, all rights reserved.
#ifndef CONTAINERS_COUNTED_HPP_
#define CONTAINERS_COUNTED_HPP_

#include <stddef.h>
#include <stdint.h>

#include <utility>

#include "errors.hpp"

// Yes, this is a clone of boost::counted_t.  This will probably
// not be the case in the future.

// Now it supports .unique(), and in order to use it, your type needs
// to provide an counted_t_use_count implementation.

template <class T>
class counted_t {
public:
    counted_t() : p_(NULL) { }
    explicit counted_t(T *p) : p_(p) {
        if (p_) { counted_t_add_ref(p_); }
    }

    counted_t(const counted_t &copyee) : p_(copyee.p_) {
        if (p_) { counted_t_add_ref(p_); }
    }

    // TODO: Add noexcept on versions of compilers that support it.  noexcept is
    // good to have because types like std::vectors use it to see whether to
    // call the copy constructor or move constructor.
    counted_t(counted_t &&movee) : p_(movee.p_) {
        movee.p_ = NULL;
    }

    // TODO: Add noexcept on versions of compilers that support it.
    template <class U>
    counted_t(const counted_t<U> &&movee) : p_(movee.p_) {
        movee.p_ = NULL;
    }

    ~counted_t() {
        if (p_) { counted_t_release(p_); }
    }

    void swap(counted_t &other) {
        T *tmp = p_;
        p_ = other.p_;
        other.p_ = tmp;
    }

    // TODO: Add noexcept on versions of compilers that support it.
    counted_t &operator=(counted_t &&other) {
        counted_t tmp(std::move(other));
        swap(tmp);
        return *this;
    }

    // TODO: Add noexcept on versions of compilers that support it.
    template <class U>
    counted_t &operator=(counted_t<U> &&other) {
        counted_t tmp(std::move(other));
        swap(tmp);
        return *this;
    }

    counted_t &operator=(const counted_t &other) {
        counted_t tmp(other);
        swap(tmp);
        return *this;
    }

    void reset() {
        counted_t tmp;
        swap(tmp);
    }

    void reset(T *other) {
        counted_t tmp(other);
        swap(tmp);  // NOLINT
    }

    T *operator->() const {
        return p_;
    }

    T &operator*() const {
        return *p_;
    }

    T *get() const {
        return p_;
    }

    bool has() const {
        return p_ != NULL;
    }

    bool unique() const {
        return counted_t_use_count(p_) == 1;
    }

    class hidden_t {
        hidden_t();
    };
    typedef void booleanesque_t(hidden_t);

    operator booleanesque_t*() const {
        return p_ ? &counted_t<T>::dummy_method : 0;
    }

private:
    static void dummy_method(hidden_t) { }

    T *p_;
};

template <class> class single_threaded_shared_mixin_t;

template <class T>
inline void counted_t_add_ref(single_threaded_shared_mixin_t<T> *p);
template <class T>
inline void counted_t_release(single_threaded_shared_mixin_t<T> *p);
template <class T>
inline intptr_t counted_t_use_count(const single_threaded_shared_mixin_t<T> *p);

template <class T>
class single_threaded_shared_mixin_t {
public:
    single_threaded_shared_mixin_t() : refcount_(0) { }

protected:
    ~single_threaded_shared_mixin_t() { }

private:
    friend void counted_t_add_ref<T>(single_threaded_shared_mixin_t<T> *p);
    friend void counted_t_release<T>(single_threaded_shared_mixin_t<T> *p);
    friend intptr_t counted_t_use_count<T>(const single_threaded_shared_mixin_t<T> *p);

    mutable intptr_t refcount_;
    DISABLE_COPYING(single_threaded_shared_mixin_t);
};

template <class T>
inline void counted_t_add_ref(const single_threaded_shared_mixin_t<T> *p) {
    p->refcount_ += 1;
    rassert(p->refcount_ > 0);
}

template <class T>
inline void counted_t_release(const single_threaded_shared_mixin_t<T> *p) {
    rassert(p->refcount_ > 0);
    --p->refcount_;
    if (p->refcount_ == 0) {
        delete static_cast<T *>(p);
    }
}

template <class T>
inline intptr_t counted_t_use_count(const single_threaded_shared_mixin_t<T> *p) {
    return p->refcount_;
}






template <class> class slow_shared_mixin_t;

template <class T>
inline void counted_t_add_ref(slow_shared_mixin_t<T> *p);
template <class T>
inline void counted_t_release(slow_shared_mixin_t<T> *p);
template <class T>
inline intptr_t counted_t_use_count(const slow_shared_mixin_t<T> *p);

{'fraction_non_alphanumeric': 0.1117156749247511, 'fraction_numerical': 0.003473026163463765, 'mean_word_length': 3.135471517472475, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 95, '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': '3920176', 'n_tokens_mistral': 3152, 'n_tokens_neox': 3090, 'n_words': 746}