libcrn  3.9.5
A document image processing library
•All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNMap.h
Go to the documentation of this file.
1 /* Copyright 2006-2016 Yann LEYDIER, CoReNum, INSA-Lyon, Université Paris Descartes, ENS-Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNMap.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNMAP_H
23 #define CRNMAP_H
24 
25 #include <CRNString.h>
26 #include <CRNIO/CRNPath.h>
27 #include <map>
28 #include <CRNData/CRNMapPtr.h>
29 #include <CRNData/CRNForeach.h>
30 #include <set>
31 
32 namespace crn
33 {
34  /****************************************************************************/
42  class Map: public Object
43  {
44  public:
46  Map();
48  virtual ~Map() override;
49 
50  Map(const Map &other);
51  Map& operator=(const Map &other);
52  Map(Map &&) = default;
53  Map& operator=(Map &&) = default;
54 
56  size_t Size() const noexcept { return data.size(); }
58  bool IsEmpty() const noexcept { return data.empty(); }
59 
63  SObject& operator[](const String &s) { return data[s]; }
65  SObject Get(const String &s);
67  SCObject Get(const String &s) const;
69  void Set(const String &key, SObject value);
70 
72  using iterator = std::map<String, SObject>::iterator;
73 
75  void Remove(const String &key);
77  void Remove(const SObject &obj);
79  void Remove(iterator it);
81  void Remove(iterator first, iterator end_);
83  void Clear() noexcept { data.clear(); }
84 
86  iterator begin() { return data.begin(); }
88  iterator end() { return data.end(); }
90  iterator Find(const String &key) { return data.find(key); }
91 
93  using const_iterator = std::map<String, SObject>::const_iterator;
95  const_iterator begin() const { return data.begin(); }
97  const_iterator end() const { return data.end(); }
99  const_iterator cbegin() const { return data.cbegin(); }
101  const_iterator cend() const { return data.cend(); }
103  const_iterator Find(const String &key) const { return data.find(key); }
104 
106  using reverse_iterator = std::map<String, SObject>::reverse_iterator;
108  reverse_iterator rbegin() { return data.rbegin(); }
110  reverse_iterator rend() { return data.rend(); }
111 
113  using const_reverse_iterator = std::map<String, SObject>::const_reverse_iterator;
115  const_reverse_iterator rbegin() const { return data.rbegin(); }
117  const_reverse_iterator rend() const { return data.rend(); }
118 
120  using pair = std::pair<const crn::String, SObject>;
121 
123  std::set<String> GetKeys() const;
125  String FirstKey() const;
127  String LastKey() const;
128 
130  void Swap(Map &other) noexcept;
131 
132  std::map<String, SObject> Std() && { return std::move(data); }
133 
135  void Deserialize(xml::Element &el);
137  xml::Element Serialize(xml::Element &parent) const;
138 
139  void Load(const Path &fname);
140  void Save(const Path &fname) const;
141 
142  private:
143  std::map<String, SObject> data;
146  public: Map(xml::Element &el) { Deserialize(el); }
147  };
148  template<> struct IsSerializable<Map> : public std::true_type {};
149  template<> struct IsClonable<Map> : public std::true_type {};
150 
151  inline void Swap(Map &m1, Map &m2) noexcept { m1.Swap(m2); }
152 }
155 
156 namespace std
157 {
158  inline void swap(crn::Map &m1, crn::Map &m2) noexcept { m1.Swap(m2); }
159 }
160 #endif
String FirstKey() const
Returns the first (lowest) key.
Definition: CRNMap.cpp:250
void Clear() noexcept
Empties the map.
Definition: CRNMap.h:83
iterator Find(const String &key)
Returns an iterator to a specific key.
Definition: CRNMap.h:90
#define CRN_ADD_RANGED_FOR_TO_CONST_POINTERS(TYPE)
Enables ranged for for smart pointers on a type.
Definition: CRNForeach.h:51
const_iterator Find(const String &key) const
Returns a const_iterator to a specific key.
Definition: CRNMap.h:103
XML element.
Definition: CRNXml.h:135
const_iterator cbegin() const
Returns a const iterator to the first element.
Definition: CRNMap.h:99
void Set(const String &key, SObject value)
Sets a value for a key with constraints check.
Definition: CRNMap.cpp:92
void Deserialize(xml::Element &el)
Reads from an XML node if applicable.
Definition: CRNMap.cpp:187
#define CRN_ADD_RANGED_FOR_TO_POINTERS(TYPE)
Enables ranged for for smart pointers on a type.
Definition: CRNForeach.h:37
void Remove(const String &key)
Removes an element (safe)
Definition: CRNMap.cpp:104
void Load(const Path &fname)
Definition: CRNMap.cpp:282
void Swap(Map &other) noexcept
Swaps contents with another map.
Definition: CRNMap.cpp:277
A UTF32 character string class.
Definition: CRNString.h:61
std::map< String, SObject >::iterator iterator
iterator on the contents of the container
Definition: CRNMap.h:72
void Save(const Path &fname) const
Definition: CRNMap.cpp:293
const_iterator end() const
Returns a const iterator after the last element.
Definition: CRNMap.h:97
Map()
Default constructor.
String LastKey() const
Returns the last (greatest) key.
Definition: CRNMap.cpp:263
const_reverse_iterator rbegin() const
Returns a const reverse iterator to the last element.
Definition: CRNMap.h:115
SObject & operator[](const String &s)
Returns an object from index. No constraint check is performed if the reference is used as a lvalue...
Definition: CRNMap.h:63
A convenience class for file paths.
Definition: CRNPath.h:39
iterator begin()
Returns an iterator to the first element.
Definition: CRNMap.h:86
xml::Element Serialize(xml::Element &parent) const
Dumps to an XML node if applicable.
Definition: CRNMap.cpp:220
reverse_iterator rend()
Returns a reverse iterator before the first element.
Definition: CRNMap.h:110
std::map< String, SObject >::const_iterator const_iterator
const_iterator on the contents of the container
Definition: CRNMap.h:93
const_iterator begin() const
Returns a const iterator to the first element.
Definition: CRNMap.h:95
void Swap(Map &m1, Map &m2) noexcept
Definition: CRNMap.h:151
const_reverse_iterator rend() const
Returns a const reverse iterator before the first element.
Definition: CRNMap.h:117
std::map< String, SObject >::reverse_iterator reverse_iterator
reverse_iterator on the contents of the container
Definition: CRNMap.h:106
const_iterator cend() const
Returns a const iterator after the last element.
Definition: CRNMap.h:101
SObject Get(const String &s)
Returns an object from index or nullptr if inexistent.
Definition: CRNMap.cpp:61
size_t Size() const noexcept
Returns the number of data objects in the map.
Definition: CRNMap.h:56
bool IsEmpty() const noexcept
Tests if the map is empty.
Definition: CRNMap.h:58
std::map< String, SObject >::const_reverse_iterator const_reverse_iterator
const_reverse_iterator on the contents of the container
Definition: CRNMap.h:113
reverse_iterator rbegin()
Returns a reverse iterator to the last element.
Definition: CRNMap.h:108
#define CRN_DECLARE_CLASS_CONSTRUCTOR(classname)
Declares a class constructor.
Definition: CRNObject.h:173
std::pair< const crn::String, SObject > pair
A (key, value) pair.
Definition: CRNMap.h:120
Data map class.
Definition: CRNMap.h:42
iterator end()
Returns an iterator after the last element.
Definition: CRNMap.h:88
std::map< String, SObject > Std()&&
Definition: CRNMap.h:132
Map & operator=(const Map &other)
Definition: CRNMap.cpp:47
virtual ~Map() override
Destructor.
std::set< String > GetKeys() const
Returns all keys.
Definition: CRNMap.cpp:236