Libmacro  0.2
Libmacro is an extensible macro and hotkey library.
contract.h
Go to the documentation of this file.
1 /* Libmacro - A multi-platform, extendable macro and hotkey C library
2  Copyright (C) 2013 Jonathan Pelletier, New Paradigm Software
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library 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 GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
23 #ifndef __cplusplus
24  #pragma message "C++ support is required for extras module"
25  #include "mcr/err.h"
26 #endif
27 
28 #ifndef MCR_EXTRAS_CONTRACT_H_
29 #define MCR_EXTRAS_CONTRACT_H_
30 
31 #include "mcr/extras/base_cpp.h"
32 #include "mcr/extras/util/string_less_ci.h"
33 
34 #include <map>
35 
36 namespace mcr
37 {
39 template<typename T>
40 class Contract final // And that's final!
41 {
42 public:
43  T notFound;
44  std::map<T, std::string> map;
45  std::map<std::string, T, string_less_ci> rmap;
46 
49  Contract(const T &notFound) : notFound(notFound)
50  {}
51  Contract(const Contract &) = default;
54  ~Contract() = default;
55  Contract &operator =(const Contract &) = default;
56 
57  T value(const std::string &name) const
58  {
59  auto it = rmap.find(name);
60  return it == rmap.end() ? notFound : it->second;
61  }
62  T value(const char *name) const
63  {
64  auto it = rmap.find(name);
65  return it == rmap.end() ? notFound : it->second;
66  }
67  bool empty() const
68  {
69  return map.empty();
70  }
71  T maximum() const
72  {
73  if (empty())
74  return notFound;
75  return map.rbegin()->first;
76  }
77  const char *name(T value) const
78  {
79  auto it = map.find(value);
80  return it == map.end() ? nullptr : it->second.c_str();
81  }
82  void set(T value, const std::string &name)
83  {
84  map[value] = name;
85  rmap[name] = value;
86  }
87  void add(T value, const std::string &addName)
88  {
89  rmap[addName] = value;
90  }
91  void add(T value, const char * const*addNames, size_t count)
92  {
93  for (size_t i = 0; i < count; i++) {
94  add(value, addNames[i]);
95  }
96  }
97  void setMap(T value, const std::string &name, const char * const*addNames,
98  size_t count)
99  {
100  set(value, name);
101  add(value, addNames, count);
102  }
103  void clear()
104  {
105  map.clear();
106  rmap.clear();
107  }
108 };
109 }
110 
111 #endif
Raise a compiler error. Usage: #include "mcr/err.h"
Libmacro, by Jonathan Pelletier, New Paradigm Software. Alpha version.
Definition: classes.h:31
Contract(const T &notFound)
Definition: contract.h:49
~Contract()=default