Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 #ifndef PROGRAM_OPTIONS_MAPPED_VALUE_H_INCLUDED
00023 #define PROGRAM_OPTIONS_MAPPED_VALUE_H_INCLUDED
00024 #ifdef _MSC_VER
00025 #pragma warning (disable : 4786)
00026 #pragma warning (disable : 4503)
00027 #endif
00028 #include "typed_value.h"
00029 #include "value_store.h"
00030 #include <string>
00031 #include <cstddef>
00032 #include <map>
00033 #if defined(_MSC_VER) && _MSC_VER <= 1200
00034 namespace std { using ::size_t; }
00035 #endif
00036 
00037 namespace ProgramOptions { 
00039 
00042 
00045 class ValueMap {
00046 public:
00047         ValueMap() {}
00048         ~ValueMap(){}
00049         bool               empty() const { return map_.empty(); }
00050         size_t             size()  const { return map_.size(); }
00051         size_t             count(const std::string& name) const { return map_.count(name); }
00052         void               clear()       { map_.clear(); }
00053         const ValueStore& operator[](const std::string& name) const {
00054                 MapType::const_iterator it = map_.find(name);
00055                 if (it == map_.end()) {
00056                         throw UnknownOption("ValueMap", name);
00057                 }
00058                 return it->second;
00059         }
00060         template <class T>
00061         static bool add(ValueMap* this_, const std::string& name, const T* value) {
00062                 MapType::iterator it = this_->map_.find(name);
00063                 if (it == this_->map_.end()) {
00064                         it = this_->map_.insert(it, MapType::value_type(name, ValueStore()));
00065                 }
00066                 if (it->second.extract_raw() != value) {
00067                         it->second.assimilate(const_cast<T*>(value));
00068                 }
00069                 return true;
00070         }
00071 private:
00072         ValueMap(const ValueMap&);
00073         ValueMap& operator=(const ValueMap&);
00074         typedef std::map<std::string, ValueStore> MapType;
00075         MapType map_;
00076 };
00077 
00083 template <class T>
00084 inline NotifiedValue<T>* store(ValueMap& map, typename detail::Parser<T>::type p = &string_cast<T>) {
00085         return notify<T>(&map, &ValueMap::add<T>, p);
00086 }
00087 
00088 inline NotifiedValue<bool>* flag(ValueMap& map, FlagAction a = store_true) {
00089         return flag(&map, &ValueMap::add<bool>, a);
00090 }
00091 
00092 }
00093 #endif