mapped_value.h
Go to the documentation of this file.
00001 //
00002 //  Copyright (c) Benjamin Kaufmann 2010
00003 //
00004 //  This is free software; you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation; either version 2 of the License, or
00007 //  (at your option) any later version. 
00008 // 
00009 //  This file is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this file; if not, write to the Free Software
00016 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 //
00018 //
00019 // NOTE: ProgramOptions is inspired by Boost.Program_options
00020 //       see: www.boost.org/libs/program_options
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 // ValueMap
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


clasp
Author(s): Benjamin Kaufmann
autogenerated on Thu Aug 27 2015 12:41:39