clingo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
mapped_value.h
Go to the documentation of this file.
1 //
2 // Copyright (c) Benjamin Kaufmann 2010
3 //
4 // This is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This file is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this file; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 //
18 //
19 // NOTE: ProgramOptions is inspired by Boost.Program_options
20 // see: www.boost.org/libs/program_options
21 //
22 #ifndef PROGRAM_OPTIONS_MAPPED_VALUE_H_INCLUDED
23 #define PROGRAM_OPTIONS_MAPPED_VALUE_H_INCLUDED
24 #ifdef _MSC_VER
25 #pragma warning (disable : 4786)
26 #pragma warning (disable : 4503)
27 #endif
28 #include "typed_value.h"
29 #include "value_store.h"
30 #include <string>
31 #include <cstddef>
32 #include <map>
33 #if defined(_MSC_VER) && _MSC_VER <= 1200
34 namespace std { using ::size_t; }
35 #endif
36 
37 namespace ProgramOptions {
39 // ValueMap
42 
45 class ValueMap {
46 public:
47  ValueMap() {}
49  bool empty() const { return map_.empty(); }
50  size_t size() const { return map_.size(); }
51  size_t count(const std::string& name) const { return map_.count(name); }
52  void clear() { map_.clear(); }
53  const ValueStore& operator[](const std::string& name) const {
54  MapType::const_iterator it = map_.find(name);
55  if (it == map_.end()) {
56  throw UnknownOption("ValueMap", name);
57  }
58  return it->second;
59  }
60  template <class T>
61  static bool add(ValueMap* this_, const std::string& name, const T* value) {
62  MapType::iterator it = this_->map_.find(name);
63  if (it == this_->map_.end()) {
64  it = this_->map_.insert(it, MapType::value_type(name, ValueStore()));
65  }
66  if (it->second.extract_raw() != value) {
67  it->second.assimilate(const_cast<T*>(value));
68  }
69  return true;
70  }
71 private:
72  ValueMap(const ValueMap&);
73  ValueMap& operator=(const ValueMap&);
74  typedef std::map<std::string, ValueStore> MapType;
75  MapType map_;
76 };
77 
83 template <class T>
84 inline NotifiedValue<T>* store(ValueMap& map, typename detail::Parser<T>::type p = &string_cast<T>) {
85  return notify<T>(&map, &ValueMap::add<T>, p);
86 }
87 
88 inline NotifiedValue<bool>* flag(ValueMap& map, FlagAction a = store_true) {
89  return flag(&map, &ValueMap::add<bool>, a);
90 }
91 
92 }
93 #endif
ValueMap()
Definition: mapped_value.h:47
tuple a
Definition: pyclingo.py:6
Definition: typed_value.h:163
NotifiedValue< T > * store(ValueMap &map, typename detail::Parser< T >::type p=&string_cast< T >)
Definition: mapped_value.h:84
static bool add(ValueMap *this_, const std::string &name, const T *value)
Definition: mapped_value.h:61
size_t count(const std::string &name) const
Definition: mapped_value.h:51
A type that can hold any kind of value type.
Definition: value_store.h:40
bool empty() const
Definition: mapped_value.h:49
void clear()
Definition: mapped_value.h:52
size_t size() const
Definition: mapped_value.h:50
Type for storing anonymous values.
Definition: mapped_value.h:45
~ValueMap()
Definition: mapped_value.h:48
tuple p
Definition: server.py:49
const ValueStore & operator[](const std::string &name) const
Definition: mapped_value.h:53
Definition: typed_value.h:101
Definition: errors.h:76
Definition: typed_value.h:36
NotifiedValue< bool > * flag(ValueMap &map, FlagAction a=store_true)
Definition: mapped_value.h:88