redis3m  1.0.0
 All Classes Functions Variables Enumerations Pages
model.h
1 // Copyright (c) 2014 Luca Marturana. All rights reserved.
2 // Licensed under Apache 2.0, see LICENSE for details
3 
4 #pragma once
5 
6 #include <string>
7 #include <boost/lexical_cast.hpp>
8 #include <map>
9 #include <redis3m/utils/exception.h>
10 #include <vector>
11 
12 namespace redis3m
13 {
14 namespace patterns
15 {
16 
17 REDIS3M_EXCEPTION(model_not_loaded)
18 
19 #define REDIS3M_MODEL_RO_ATTRIBUTE(type, name) \
20 public:\
21  inline const type& name() const { if (_loaded) return _##name; else throw redis3m::patterns::model_not_loaded(); }\
22 private:\
23  type _##name;
24 
32 class model
33 {
34 public:
40  model():
41  _loaded(false)
42  {
43 
44  }
45 
54  model(const std::string& id, const std::map<std::string, std::string>& map):
55  _id(id),
56  _loaded(true)
57  {
58 
59  }
60 
66  std::map<std::string, std::string> to_map()
67  {
68  return std::map<std::string, std::string>();
69  }
70 
75  inline const std::string& id() const { if (_loaded) return _id; else throw model_not_loaded(); }
76 
81  inline bool loaded() const { return _loaded; }
82 
87  static inline std::vector< std::string > indices()
88  {
89  return std::vector<std::string>();
90  }
91 
97  static inline std::vector< std::string > uniques()
98  {
99  return std::vector<std::string>();
100  }
101 
107  static inline std::vector<std::string> tracked()
108  {
109  return std::vector<std::string>();
110  }
111 
112 protected:
121  inline static std::string read_str_from_map(const std::map<std::string, std::string>& map,
122  const std::string& key,
123  const std::string& default_value="")
124  {
125  if (map.find(key) != map.end())
126  {
127  return map.at(key);
128  }
129  else
130  {
131  return default_value;
132  }
133  }
134 
143  inline static void write_str_to_map(std::map<std::string, std::string>& map,
144  const std::string& key,
145  const std::string& value,
146  const std::string& default_value="")
147  {
148  if (value != default_value)
149  {
150  map[key] = value;
151  }
152  }
153 
154  template<typename IntegerType>
163  inline static IntegerType read_int_from_map(const std::map<std::string, std::string>& map,
164  const std::string& key,
165  const IntegerType default_value=0)
166  {
167  if (map.find(key) != map.end())
168  {
169  return boost::lexical_cast<IntegerType>(map.at(key));
170  }
171  else
172  {
173  return default_value;
174  }
175  }
176 
177  template<typename IntegerType>
186  inline static void write_int_to_map(std::map<std::string, std::string>& map,
187  const std::string& key,
188  const IntegerType value,
189  const IntegerType default_value=0)
190  {
191  if (value != default_value)
192  {
193  map[key] = boost::lexical_cast<std::string>(value);
194  }
195  }
196 
203  inline static bool read_bool_from_map(const std::map<std::string, std::string>& map,
204  const std::string& key)
205  {
206  if (map.find(key) != map.end())
207  {
208  return true;
209  }
210  else
211  {
212  return false;
213  }
214  }
215 
222  inline static void write_bool_to_map(std::map<std::string, std::string>& map,
223  const std::string& key,
224  const bool value)
225  {
226  if (value)
227  {
228  map[key] = "true";
229  }
230  }
231 
236  std::string _id;
237 
243  bool _loaded;
244 };
245 
246 
247 }
248 }
static IntegerType read_int_from_map(const std::map< std::string, std::string > &map, const std::string &key, const IntegerType default_value=0)
Read an integer from map, otherwise use the specified default value (0)
Definition: model.h:163
bool _loaded
If this flag is true, data contained by an instance are correct, means that they reflect something st...
Definition: model.h:243
std::string _id
Unique identifier of the object, subclasses can set it to a custom value.
Definition: model.h:236
static void write_int_to_map(std::map< std::string, std::string > &map, const std::string &key, const IntegerType value, const IntegerType default_value=0)
Write an integer to a map, only if it's different from default value.
Definition: model.h:186
static bool read_bool_from_map(const std::map< std::string, std::string > &map, const std::string &key)
Read a boolean from a map.
Definition: model.h:203
static void write_str_to_map(std::map< std::string, std::string > &map, const std::string &key, const std::string &value, const std::string &default_value="")
Write a string to a map, useful on to_map() method. It saves value on the map only if it's different ...
Definition: model.h:143
static std::vector< std::string > uniques()
A vector of fields to be indexed in a unique fashion, used by Orm.
Definition: model.h:97
bool loaded() const
If true means that object data is valid, as just get from database.
Definition: model.h:81
static void write_bool_to_map(std::map< std::string, std::string > &map, const std::string &key, const bool value)
Write a boolean to a map, only if it's true.
Definition: model.h:222
Class useful to define models used by orm and simple_obj_store. REDIS3M_MODEL_RO_ATTRIBUTE(type, name) macro defines automatically an attribute with a public getter. All fields need to be serialized to a std::map<std::string, std::string>. Model class contains various helpers to to that easily.
Definition: model.h:32
model(const std::string &id, const std::map< std::string, std::string > &map)
Contructor called by orm or simple_obj_store pattern. Used to fill object with data from database...
Definition: model.h:54
static std::string read_str_from_map(const std::map< std::string, std::string > &map, const std::string &key, const std::string &default_value="")
Convenient method to get a field from a map, or returning a default value if the key is not present...
Definition: model.h:121
std::map< std::string, std::string > to_map()
Serialize all object data to a string:string map, it will be saved on Redis on a Hash.
Definition: model.h:66
model()
Default constructor, use it to build a new object, still not saved to database. If you need to access...
Definition: model.h:40
static std::vector< std::string > tracked()
Keys associated with a Model, may be sets or lists usually. They will be saved as <Modelname>:<modeli...
Definition: model.h:107
static std::vector< std::string > indices()
A vector of fields to be indexed, used by Orm.
Definition: model.h:87
const std::string & id() const
Object unique identifier.
Definition: model.h:75