00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef TOOL_MAPPING_HXX_
00023 # define TOOL_MAPPING_HXX_
00024
00025 # include <utility>
00026
00027 # include "mapping.hh"
00028
00029 namespace tool
00030 {
00031
00032 template <class K, class V, class F>
00033 mapping<K, V, F>::mapping(const std::string& filename,
00034 const F& factory): datafile (filename),
00035 F (factory)
00036 {
00037 std::ifstream f (filename.c_str());
00038
00039 if (f)
00040 load(f);
00041 }
00042
00043 template <class K, class V, class F>
00044 const V&
00045 mapping<K, V, F>::operator [] (const K& a)
00046 {
00047 const typename map_type::const_iterator i = map_.find(a);
00048
00049 if (i == map_.end())
00050 {
00051 const std::pair<typename map_type::iterator, bool> r =
00052 map_.insert(std::make_pair(a, this->new_value(a, map_)));
00053 assert(r.second);
00054
00055 const V& v = r.first->second;
00056
00057 *this << v << '\t' << a << '\n';
00058 return v;
00059 }
00060 return i->second;
00061 }
00062
00063 template <class K, class V, class F>
00064 void
00065 mapping<K, V, F>::load(std::istream& in)
00066 {
00067 while (true)
00068 {
00069 K a;
00070 V i;
00071
00072 in >> i;
00073 if (in.eof())
00074 break;
00075 if (not in)
00076 throw std::invalid_argument ("Invalid mapping file");
00077 in >> a;
00078 if (not in or in.eof())
00079 throw std::invalid_argument ("Invalid mapping file");
00080 map_.insert(std::make_pair(a, i));
00081 this->value_loaded(a, i, map_);
00082 }
00083 }
00084
00085 }
00086
00087 #endif // ! TOOL_MAPPING_HXX_