Skip to content

Instantly share code, notes, and snippets.

@malcom malcom/vector_ptr.h
Created Sep 9, 2009

Embed
What would you like to do?
/*
* VectorPtr - Thin Template idiom for the vector of pointers
* http://projects.malcom.pl/code/vector_ptr.xhtml
*
* Copyright (C) 2009 Marcin 'Malcom' Malich <me@malcom.pl>
*
* Released under the MIT License.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* 2009-03-20 11:16:18
*/
#ifndef _VECTOR_PTR_H_
#define _VECTOR_PTR_H_
#include <vector>
template <typename T>
class vector_ptr : private std::vector<void*> {
typedef std::vector<void*> base;
template<typename Base, typename Val, typename Ptr, typename Ref>
class iterator_ptr : public Base {
public:
typedef Val value_type;
typedef Ptr pointer;
typedef Ref reference;
iterator_ptr()
: Base() {}
explicit iterator_ptr(const pointer& p)
: Base(p) {}
iterator_ptr(const Base& it)
: Base(it) {}
reference operator*() const {
return reinterpret_cast<reference>(Base::operator*());
}
pointer operator->() const {
return reinterpret_cast<pointer>(Base::operator->());
}
};
public:
typedef T* value_type;
typedef T** pointer;
typedef T* const* const_pointer;
typedef T*& reference;
typedef T* const& const_reference;
typedef iterator_ptr<base::iterator, value_type, pointer, reference> iterator;
typedef iterator_ptr<base::const_iterator, value_type, const_pointer, const_reference> const_iterator;
typedef iterator_ptr<base::reverse_iterator, value_type, pointer, reference> reverse_iterator;
typedef iterator_ptr<base::const_reverse_iterator, value_type, const_pointer, const_reference> const_reverse_iterator;
typedef typename base::size_type size_type;
typedef typename base::difference_type difference_type;
typedef typename base::allocator_type allocator_type;
explicit vector_ptr(const allocator_type& a = allocator_type())
: base(a) {}
vector_ptr(size_type n, const value_type& value, const allocator_type& a = allocator_type())
: base(n, value, a) {}
explicit vector_ptr(size_type n)
: base(n) {}
vector_ptr(const vector_ptr& x)
: base(x) {}
template<typename InputIterator>
vector_ptr(InputIterator first, InputIterator last, const allocator_type& a = allocator_type())
: base(first, last, a) {}
~vector_ptr() {}
vector_ptr& operator=(const vector_ptr& x) {
base::operator=(x);
return *this;
}
void assign(size_type n, const value_type& val) {
base::assign(n, val);
}
template<typename InputIterator>
void assign(InputIterator first, InputIterator last) {
base::assign(first, last);
}
// iterators
iterator begin() {
return base::begin();
}
const_iterator begin() const {
return base::begin();
}
iterator end() {
return base::end();
}
const_iterator end() const {
return base::end();
}
reverse_iterator rbegin() {
return base::rbegin();
}
const_reverse_iterator rbegin() const {
return base::rbegin();
}
reverse_iterator rend() {
return base::rend();
}
const_reverse_iterator rend() const {
return base::rend();
}
// capacity
using base::size;
using base::max_size;
void resize(size_type new_size, const value_type& x) {
base::resize(new_size, x);
}
using base::resize;
using base::capacity;
using base::empty;
using base::reserve;
// element access
reference operator[](size_type n) {
return reinterpret_cast<reference>(base::operator[](n));
}
const_reference operator[](size_type n) const {
return reinterpret_cast<const_reference>(base::operator[](n));
}
reference at(size_type n) {
return reinterpret_cast<reference>(base::at(n));
}
const_reference at(size_type n) const {
return reinterpret_cast<const_reference>(base::at(n));
}
reference front() {
return reinterpret_cast<reference>(base::front());
}
const_reference front() const {
return reinterpret_cast<const_reference>(base::front());
}
reference back() {
return reinterpret_cast<reference>(base::back());
}
const_reference back() const {
return reinterpret_cast<const_reference>(base::back());
}
// modifiers
void push_back(const value_type& x) {
base::push_back(x);
}
void pop_back() {
base::pop_back();
}
iterator insert(iterator position, const value_type& x) {
base::insert(position, x);
}
void insert(iterator position, size_type n, const value_type& x) {
base::insert(position, n, x);
}
template<typename InputIterator>
void insert(iterator position, InputIterator first, InputIterator last) {
base::insert(position, first, last);
}
iterator erase(iterator position) {
return base::erase(position);
}
iterator erase(iterator first, iterator last) {
return base::erase(first, last);
}
void swap(vector_ptr& x) {
base::swap(x);
}
using base::clear;
};
template<>
class vector_ptr<void*> : public std::vector<void*> {};
#endif // _VECTOR_PTR_H_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.