7 #include <boost/lexical_cast.hpp>
9 #include <redis3m/utils/exception.h>
17 REDIS3M_EXCEPTION(model_not_loaded)
19 #define REDIS3M_MODEL_RO_ATTRIBUTE(type, name) \
21 inline const type& name() const { if (_loaded) return _##name; else throw redis3m::patterns::model_not_loaded(); }\
54 model(
const std::string&
id,
const std::map<std::string, std::string>& map):
66 std::map<std::string, std::string>
to_map()
68 return std::map<std::string, std::string>();
75 inline const std::string&
id()
const {
if (
_loaded)
return _id;
else throw model_not_loaded(); }
87 static inline std::vector< std::string >
indices()
89 return std::vector<std::string>();
97 static inline std::vector< std::string >
uniques()
99 return std::vector<std::string>();
107 static inline std::vector<std::string>
tracked()
109 return std::vector<std::string>();
122 const std::string& key,
123 const std::string& default_value=
"")
125 if (map.find(key) != map.end())
131 return default_value;
144 const std::string& key,
145 const std::string& value,
146 const std::string& default_value=
"")
148 if (value != default_value)
154 template<
typename IntegerType>
164 const std::string& key,
165 const IntegerType default_value=0)
167 if (map.find(key) != map.end())
169 return boost::lexical_cast<IntegerType>(map.at(key));
173 return default_value;
177 template<
typename IntegerType>
187 const std::string& key,
188 const IntegerType value,
189 const IntegerType default_value=0)
191 if (value != default_value)
193 map[key] = boost::lexical_cast<std::string>(value);
204 const std::string& key)
206 if (map.find(key) != map.end())
223 const std::string& key,
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