array.hpp

Go to the documentation of this file.
00001 /* $Id: array.hpp 23842 2008-02-16 08:47:16Z mordante $ */
00002 /*
00003    Copyright (C) 2003 - 2008 by David White <dave@whitevine.net>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License version 2
00008    or at your option any later version.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 
00015 //! @file array.hpp 
00016 //! Template for arrays.
00017 
00018 #ifndef ARRAY_HPP_INCLUDED
00019 #define ARRAY_HPP_INCLUDED
00020 
00021 #include <algorithm>
00022 
00023 namespace util
00024 {
00025 
00026 template<typename T,size_t N>
00027 class array
00028 {
00029 public:
00030     typedef T value_type;
00031     typedef T* iterator;
00032     typedef const T* const_iterator;
00033     typedef T& reference;
00034     typedef const T& const_reference;
00035     typedef size_t size_type;
00036 
00037     array() {}
00038     array(const T& o)
00039     {
00040         std::fill(begin(),end(),o);
00041     }
00042 
00043     iterator begin() { return a; }
00044     iterator end() { return a + N; }
00045 
00046     const_iterator begin() const { return a; }
00047     const_iterator end() const { return a + N; }
00048 
00049     reference operator[](size_type n) { return a[n]; }
00050     const_reference operator[](size_type n) const { return a[n]; }
00051 
00052     reference front() { return a[0]; }
00053     reference back() { return a[N-1]; }
00054 
00055     const_reference front() const { return a[0]; }
00056     const_reference back() const { return a[N-1]; }
00057 
00058     size_type size() const { return N; }
00059 
00060     bool empty() const { return size() == 0; }
00061 
00062     T* data() { return a; }
00063     const T* data() const { return a; }
00064 
00065 private:
00066     T a[N];
00067 };
00068 
00069 } // end namespace util
00070 
00071 #endif
00072 

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs