All Classes Namespaces Functions Variables Enumerations Enumerator Pages
SetDescriptor.h
1 /*
2  Copyright (c) 2014 by Elvis Angelaccio
3 
4  Permission is hereby granted, free of charge, to any person obtaining a copy
5  of this software and associated documentation files (the "Software"), to deal
6  in the Software without restriction, including without limitation the rights
7  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  copies of the Software, and to permit persons to whom the Software is
9  furnished to do so, subject to the following conditions:
10 
11  The above copyright notice and this permission notice shall be included in
12  all copies or substantial portions of the Software.
13 
14  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  THE SOFTWARE.
21 */
22 
23 #ifndef SET_DESCRIPTOR_H
24 #define SET_DESCRIPTOR_H
25 
26 #include <string>
27 #include <map>
28 #include <stdexcept>
29 
30 namespace entity
31 {
39  {
40 
41  public:
42 
43  static const unsigned int MAX_FIELDS_NUMBER = 512;
45  SetDescriptor();
46 
47  void setSequenceNumber(int sn);
48  int getSequenceNumber() const;
49 
51  int size() const;
52 
53  const std::map<std::string, std::string>& getStringFields() const;
54  const std::map<std::string, int>& getIntFields() const;
55  const std::map<std::string, float>& getFloatFields() const;
56 
61  bool existKey(const std::string& key);
62 
63  /*
64  * Verify if the SetDescriptor has the given value under the given key.
65  * @param key A key of a field.
66  * @param value The field value to be verified.
67  */
68  bool hasValue(const std::string& key, const std::string& value);
69  bool hasValue(const std::string& key, int value);
70  bool hasValue(const std::string& key, float value);
71 
72  /*
73  * Add the given value to the given key.
74  * @param key A key of a certain SetDesriptor field.
75  * @param value The value to add to the SetDescriptor.
76  * @throw UnknownKeyError if the key is empty or is not in the SetDescriptor
77  * @throw WrongTypeError if the key exists but it's owned by another SetDescriptor map.
78  */
79  void addValue(const std::string& key, const std::string& value);
80  void addValue(const std::string& key, int value);
81  void addValue(const std::string& key, float value);
82 
88  template<typename T> bool addKey(const std::string& key);
89 
90  bool operator==(const SetDescriptor& other);
91  bool operator!=(const SetDescriptor& other);
92 
93  friend std::ostream& operator<<(std::ostream &os, const SetDescriptor& sd);
94 
95  private:
96 
97  static const int INVALID_SEQUENCE_NUMBER = -1;
99  int sequenceNumber;
100  std::map<std::string, std::string> stringFields;
101  std::map<std::string, int> intFields;
102  std::map<std::string, float> floatFields;
103  };
104 
106  class UnknownKeyError : public std::runtime_error
107  {
108  public:
109  UnknownKeyError(const std::string& msg) : std::runtime_error(msg) {}
110  };
111 
113  class WrongTypeError : public std::runtime_error
114  {
115  public:
116  WrongTypeError(const std::string& msg) : std::runtime_error(msg) {}
117  };
118 
119 
120  const std::string DEFAULT_SERIALIZABLE_FLOAT = "0.0";
121 }
122 
123 #endif
Definition: DataItem.h:28
bool addKey(const std::string &key)
Definition: SetDescriptor.h:113
A SetDescriptor (SD) is a set of pairs , where the key is always a string, while the value may be an integer, a float or a string.
Definition: SetDescriptor.h:38
int size() const
Definition: SetDescriptor.cpp:47
SetDescriptor()
Definition: SetDescriptor.cpp:35
bool existKey(const std::string &key)
Definition: SetDescriptor.cpp:68
Definition: SetDescriptor.h:106