Instantly share code, notes, and snippets.
Last active
August 8, 2018 05:52
-
Save ktnyt/0dec86637a6488d2cc985872e3d9af38 to your computer and use it in GitHub Desktop.
A simple wrapper for a reference counted vector container.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __LAPLUS_SHARED_VECTOR_HPP__ | |
#define __LAPLUS_SHARED_VECTOR_HPP__ | |
#include <memory> | |
#include <vector> | |
namespace laplus { | |
template <class T, class Allocator = std::allocator<T>> | |
class shared_vector { | |
public: | |
using vector_type = std::vector<T, Allocator>; | |
using value_type = typename vector_type::value_type; | |
using allocator_type = typename vector_type::allocator_type; | |
using size_type = typename vector_type::size_type; | |
using difference_type = typename vector_type::difference_type; | |
using reference = typename vector_type::reference; | |
using const_reference = typename vector_type::const_reference; | |
using pointer = typename vector_type::pointer; | |
using const_pointer = typename vector_type::const_pointer; | |
using iterator = typename vector_type::iterator; | |
using const_iterator = typename vector_type::const_iterator; | |
using reverse_iterator = typename vector_type::reverse_iterator; | |
using const_reverse_iterator = typename vector_type::const_reverse_iterator; | |
shared_vector() : _ptr(std::make_shared<vector_type>()) {} | |
explicit shared_vector(const Allocator& alloc) | |
: _ptr(std::make_shared<vector_type>(alloc)) {} | |
explicit shared_vector(size_type count, const T& value = T(), | |
const Allocator& alloc = Allocator()) | |
: _ptr(std::make_shared<vector_type>(count, value, alloc)) {} | |
explicit shared_vector(size_type count) | |
: _ptr(std::make_shared<vector_type>(count)) {} | |
template <class InputIt> | |
shared_vector(InputIt first, InputIt last, | |
const Allocator& alloc = Allocator()) | |
: _ptr(std::make_shared<vector_type>(first, last, alloc)) {} | |
shared_vector(const shared_vector&) = default; | |
shared_vector(shared_vector&&) = default; | |
shared_vector(std::initializer_list<T> init, | |
const Allocator& alloc = Allocator()) | |
: _ptr(std::make_shared<vector_type>(init, alloc)) {} | |
shared_vector& operator=(const shared_vector&) = default; | |
shared_vector& operator=(shared_vector&&) = default; | |
shared_vector& operator=(std::initializer_list<T> ilist) { *_ptr = ilist; } | |
void assign(size_type count, const T& value) { _ptr->assign(count, value); } | |
template <class InputIt> | |
void assign(InputIt first, InputIt last) { | |
_ptr->assign(first, last); | |
} | |
void assign(std::initializer_list<T> ilist) { _ptr->assign(ilist); } | |
allocator_type get_allocator() const { return _ptr->get_allocator(); } | |
reference at(size_type pos) { return _ptr->at(pos); } | |
const_reference at(size_type pos) const { return _ptr->at(pos); } | |
reference operator[](size_type pos) { return _ptr->operator[](pos); } | |
const_reference operator[](size_type pos) const { | |
return _ptr->operator[](pos); | |
} | |
reference front() { return _ptr->front(); } | |
const_reference front() const { return _ptr->front(); } | |
reference back() { return _ptr->back(); } | |
const_reference back() const { return _ptr->back(); } | |
T* data() noexcept { return _ptr->data(); } | |
const T* data() const noexcept { return _ptr->data(); } | |
iterator begin() noexcept { return _ptr->begin(); } | |
const_iterator begin() const noexcept { return _ptr->begin(); } | |
const_iterator cbegin() const noexcept { return _ptr->cbegin(); } | |
iterator end() noexcept { return _ptr->end(); } | |
const_iterator end() const noexcept { return _ptr->end(); } | |
const_iterator cend() const noexcept { return _ptr->cend(); } | |
iterator rbegin() noexcept { return _ptr->rbegin(); } | |
const_iterator rbegin() const noexcept { return _ptr->rbegin(); } | |
const_iterator crbegin() const noexcept { return _ptr->crbegin(); } | |
iterator rend() noexcept { return _ptr->rend(); } | |
const_iterator rend() const noexcept { return _ptr->rend(); } | |
const_iterator crend() const noexcept { return _ptr->crend(); } | |
bool empty() const noexcept { return _ptr->empty(); } | |
size_type size() const noexcept { return _ptr->size(); } | |
size_type max_size() const noexcept { return _ptr->max_size(); } | |
void reserve(size_type new_cap) { _ptr->reserve(new_cap); } | |
size_type capacity() const noexcept { return _ptr->capacity(); } | |
void shrink_to_fit() { return _ptr->shrink_to_fit(); } | |
void clear() noexcept { _ptr->clear(); } | |
iterator insert(const_iterator pos, const T& value) { | |
return _ptr->insert(pos, value); | |
} | |
iterator insert(const_iterator pos, T&& value) { | |
return _ptr->insert(pos, value); | |
} | |
iterator insert(const_iterator pos, size_type count, const T& value) { | |
return _ptr->insert(pos, count, value); | |
} | |
template <class InputIt> | |
iterator insert(const_iterator pos, InputIt first, InputIt last) { | |
return _ptr->insert(pos, first, last); | |
} | |
iterator insert(const_iterator pos, std::initializer_list<T> ilist) { | |
return _ptr->insert(pos, ilist); | |
} | |
template <class... Args> | |
iterator emplace(const_iterator pos, Args&&... args) { | |
return _ptr->emplace(pos, std::forward<Args>(args)...); | |
} | |
iterator erase(const_iterator pos) { return _ptr->erase(pos); } | |
iterator erase(const_iterator first, const_iterator last) { | |
return _ptr->erase(first, last); | |
} | |
template <class... Args> | |
iterator emplace_back(Args&&... args) { | |
return _ptr->emplace_back(std::forward<Args>(args)...); | |
} | |
void pop_back() { _ptr->pop_back(); } | |
void resize(size_type count) { _ptr->resize(count); } | |
void resize(size_type count, const value_type& value) { | |
_ptr->resize(count, value); | |
} | |
void reset() noexcept { _ptr.reset(); } | |
template <class Y> | |
void reset(Y* ptr) { | |
_ptr.reset(ptr); | |
} | |
template <class Y, class Deleter> | |
void reset(Y* ptr, Deleter d) { | |
_ptr.reset(ptr, d); | |
} | |
template <class Y, class Deleter, class Alloc> | |
void reset(Y* ptr, Deleter d, Alloc alloc) { | |
_ptr.reset(ptr, d, alloc); | |
} | |
void swap(shared_vector& other) { _ptr.swap(other._ptr); } | |
vector_type operator*() const noexcept { return _ptr.operator*(); } | |
vector_type operator->() const noexcept { return _ptr.operator->(); } | |
long use_count() const noexcept { return _ptr.use_count(); } | |
operator bool() const noexcept { return static_cast<bool>(_ptr); } | |
template <class Y, class Alloc> | |
bool owner_before(const shared_vector<Y, Alloc>& other) { | |
return _ptr.owner_before(other._ptr); | |
} | |
bool operator==(shared_vector& rhs) { return *_ptr == *(rhs._ptr); } | |
bool operator!=(shared_vector& rhs) { return *_ptr != *(rhs._ptr); } | |
bool operator<(shared_vector& rhs) { return *_ptr < *(rhs._ptr); } | |
bool operator<=(shared_vector& rhs) { return *_ptr <= *(rhs._ptr); } | |
bool operator>(shared_vector& rhs) { return *_ptr > *(rhs._ptr); } | |
bool operator>=(shared_vector& rhs) { return *_ptr >= *(rhs._ptr); } | |
private: | |
std::shared_ptr<vector_type> _ptr; | |
}; | |
} // namespace laplus | |
#endif // __LAPLUS_SHARED_VECTOR_HPP__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment