00001 // Copyright (C) 2001 Petter Urkedal (petter.urkedal@matfys.lth.se)
00002 //
00003 // This file is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 //
00008 // This file is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00016 //
00017 // As a special exception, you may use this file as part of a free
00018 // software library without restriction. Specifically, if other files
00019 // instantiate templates or use macros or inline functions from this
00020 // file, or you compile this file and link it with other files to
00021 // produce an executable, this file does not by itself cause the
00022 // resulting executable to be covered by the GNU General Public
00023 // License. This exception does not however invalidate any other
00024 // reasons why the executable file might be covered by the GNU General
00025 // Public License.
00026 //
00027 // $Id: null_container.h,v 1.1 2002/05/30 18:01:37 petter_urkedal Exp $
00028
00029
00030 #ifndef MORE_GEN_NULL_CONTAINER_H
00031 #define MORE_GEN_NULL_CONTAINER_H
00032
00033
00034 #include <iterator>
00035 #include <stdexcept>
00036
00037
00038 namespace more {
00039 namespace gen {
00040
00041 /** \class null_iterator null_container.h more/gen/null_container.h
00042 * A random access iterator with only one value, a past-the-end
00043 * iterator. */
00044 template<typename Value>
00045 struct null_iterator {
00046 typedef std::random_access_iterator_tag iterator_category;
00047 typedef std::ptrdiff_t difference_type;
00048 typedef Value value_type;
00049 typedef value_type& reference;
00050 typedef value_type* pointer;
00051
00052 null_iterator() {}
00053 // Provide conversion from the const valued iterator, or if
00054 // value is const, this just redundantly define the copy ctor
00055 // and assignment.
00056 null_iterator(null_iterator<Value const> const&) {}
00057 null_iterator& operator=(null_iterator<Value const> const&) {
00058 return *this;
00059 }
00060
00061 null_iterator& operator++() { return *this; }
00062 null_iterator& operator++(int) { return *this; }
00063 null_iterator& operator--() { return *this; }
00064 null_iterator& operator--(int) { return *this; }
00065 null_iterator& operator+=(difference_type) { return *this; }
00066 null_iterator& operator-=(difference_type) { return *this; }
00067 null_iterator operator+(difference_type) { return *this; }
00068 null_iterator operator-(difference_type) { return *this; }
00069 friend null_iterator
00070 operator+(difference_type, null_iterator x) { return x; }
00071 difference_type operator-(null_iterator) { return 0; }
00072
00073 bool operator==(null_iterator const&) const { return true; }
00074 bool operator!=(null_iterator const&) const { return false; }
00075 bool operator<(null_iterator const&) const { return false; }
00076 bool operator>(null_iterator const&) const { return false; }
00077 bool operator<=(null_iterator const&) const { return true; }
00078 bool operator>=(null_iterator const&) const { return true; }
00079
00080 reference operator*() {
00081 throw std::logic_error("Dereference of past-the-end iterator.");
00082 }
00083 pointer operator->() { return &**this; }
00084 reference operator[](difference_type) { return **this; }
00085 };
00086
00087 /** \class null_container null_container.h more/gen/null_container.h
00088 * A container which is always empty. */
00089 template<typename Value>
00090 struct null_container {
00091 typedef Value value_type;
00092 typedef value_type& reference;
00093 typedef value_type const& const_reference;
00094 typedef null_iterator<value_type> iterator;
00095 typedef null_iterator<value_type const> const_iterator;
00096 typedef std::ptrdiff_t difference_type;
00097 typedef std::size_t size_type;
00098
00099 null_container() {}
00100 // null_container(null_container const&); implied
00101 // null_container& operator=(null_container const&); implied
00102 const_iterator begin() const { return const_iterator(); }
00103 const_iterator end() const { return const_iterator(); }
00104 iterator begin() { return iterator(); }
00105 iterator end() { return iterator(); }
00106
00107 bool operator==(null_container const&) const { return true; }
00108 bool operator!=(null_container const&) const { return false; }
00109 void swap(null_container&) {}
00110 };
00111 }} // more::gen
00112
00113 #endif