buffer.h
Go to the documentation of this file.00001 #ifndef __UTIL__BUFFER_HPP__
00002 #define __UTIL__BUFFER_HPP__
00003
00009 #include <config.h>
00010 #include <stdexcept>
00011 #include <iostream>
00012
00013 namespace util {
00020 template <class T, unsigned int size> class Buffer {
00021 public:
00022 Buffer(const T& val = T());
00023 virtual ~Buffer();
00024
00025 T& operator[](unsigned int i) throw (std::out_of_range);
00026 T* c_data();
00027
00028 private:
00029 T data[size];
00030 };
00031
00041 template <class T, unsigned int size>
00042 std::istream& operator>>(std::istream& is, Buffer<T, size>& b) {
00043 for (unsigned int i = 0; i < size; ++i)
00044 is >> std::ws >> b[i];
00045 return is;
00046 }
00047
00057 template <class T, unsigned int size>
00058 std::ostream& operator<<(std::ostream& os, Buffer<T, size>& b) {
00059 for (unsigned int i = 0; i < size; ++i) {
00060 if (i) os << " ";
00061 os << b[i];
00062 }
00063 return os;
00064 }
00065 }
00066
00071 template <class T, unsigned int size>
00072 util::Buffer<T, size>::Buffer(const T& val) {
00073 for (unsigned int i = 0; i < size; i++) data[i] = val;
00074 }
00075
00077 template <class T, unsigned int size>
00078 util::Buffer<T, size>::~Buffer() {}
00079
00087 template <class T, unsigned int size>
00088 T& util::Buffer<T, size>::operator[](unsigned int i) throw (std::out_of_range) {
00089 if (i < size) return data[i];
00090 else throw std::out_of_range("Buffer index is out of range.");
00091 }
00092
00100 template <class T, unsigned int size>
00101 T* util::Buffer<T, size>::c_data() {
00102 return data;
00103 }
00104
00105 #endif