/*

 Safe C++, Or How to Avoid Most Common Mistakes in C++ Code
 by Vladimir Kushnir, (OÕReilly).
 
 Copyright 2012 Vladimir Kushnir, ISBN 9781449320935.
 
 If you feel your use of code examples falls outside fair use or the
 permission given above, feel free to contact us at permissions@oreilly.com.
 
*/

#ifndef __SCPP_VECTOR_HPP_INCLUDED__
#define __SCPP_VECTOR_HPP_INCLUDED__

#include <vector>
#include "scpp_assert.h"


namespace scpp {
	
// Wrapper around std::vector, has temporary sanity checks in the operators [].
template <typename T>
class vector : public std::vector<T> {
 public:
	//typedef unsigned size_type;
	typedef size_t size_type;
	
	// Most commonly used constructors:
	explicit vector( size_type n = 0  )
	: std::vector<T>(n)
	{}
	
	vector( size_type n, const T& value )
	: std::vector<T>(n, value)
	{}

	template <class InputIterator> vector ( InputIterator first, InputIterator last )
	: std::vector<T>(first, last)
	{}
	
	// Note: we do not provide a copy-ctor and assignment operator.
	// we rely on default versions of these methods generated by the compiler.
	
	T& operator [] (int index) {
		SCPP_TEST_ASSERT(index >= 0 && static_cast<size_type>(index) < std::vector<T>::size(),
			"Indice " << index << " deve estar entre 0 e "
			<< std::vector<T>::size());
		return std::vector<T>::operator[](static_cast<size_type>(index));
	}

	const T& operator [] (int index) const {
		SCPP_TEST_ASSERT(index >= 0 && static_cast<size_type>(index) < std::vector<T>::size(),
					"Indice " << index << " deve estar entre 0 e "
					<< std::vector<T>::size());
		return std::vector<T>::operator[](static_cast<size_type>(index));
	}
};
} // namespace scpp


template <typename T>
inline
std::ostream& operator << (std::ostream& os, const scpp::vector<T>& v) {
	for(unsigned i=0; i<v.size(); ++i) {
		os << v[i];
		if( i + 1 < v.size() )
			os << " ";
	}
	return os;
}

#endif // __SCPP_VECTOR_HPP_INCLUDED__
