redis3m  1.0.0
 All Classes Functions Variables Enumerations Pages
Public Member Functions | Static Public Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
redis3m::patterns::model Class Reference

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. More...

#include <model.h>

Public Member Functions

 model ()
 Default constructor, use it to build a new object, still not saved to database. If you need to access data with getters, change _loaded to true. Otherwise is better to leave it as false.
 
 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, call it from subclasses passing parameters as-is. It will set _loaded to true, because data stored on this object is correct, as it's just retrieved from database. More...
 
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. More...
 
const std::string & id () const
 Object unique identifier. More...
 
bool loaded () const
 If true means that object data is valid, as just get from database. More...
 

Static Public Member Functions

static std::vector< std::string > indices ()
 A vector of fields to be indexed, used by Orm. More...
 
static std::vector< std::string > uniques ()
 A vector of fields to be indexed in a unique fashion, used by Orm. More...
 
static std::vector< std::string > tracked ()
 Keys associated with a Model, may be sets or lists usually. They will be saved as <Modelname>:<modelid>:<keyname> More...
 

Static Protected Member Functions

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. Useful on constructor. More...
 
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 from default. More...
 
template<typename IntegerType >
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) More...
 
template<typename IntegerType >
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. More...
 
static bool read_bool_from_map (const std::map< std::string, std::string > &map, const std::string &key)
 Read a boolean from a map. More...
 
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. More...
 

Protected Attributes

std::string _id
 Unique identifier of the object, subclasses can set it to a custom value.
 
bool _loaded
 If this flag is true, data contained by an instance are correct, means that they reflect something stored on database. Can be set to true even for new object, still not saved, if it's necessary (by subclasses).
 

Detailed Description

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.

Constructor & Destructor Documentation

redis3m::patterns::model::model ( const std::string &  id,
const std::map< std::string, std::string > &  map 
)
inline

Contructor called by orm or simple_obj_store pattern. Used to fill object with data from database, call it from subclasses passing parameters as-is. It will set _loaded to true, because data stored on this object is correct, as it's just retrieved from database.

Parameters
ididentifier of object
mapserialized fields

Member Function Documentation

const std::string& redis3m::patterns::model::id ( ) const
inline

Object unique identifier.

Returns
static std::vector< std::string > redis3m::patterns::model::indices ( )
inlinestatic

A vector of fields to be indexed, used by Orm.

Returns
bool redis3m::patterns::model::loaded ( ) const
inline

If true means that object data is valid, as just get from database.

Returns
static bool redis3m::patterns::model::read_bool_from_map ( const std::map< std::string, std::string > &  map,
const std::string &  key 
)
inlinestaticprotected

Read a boolean from a map.

Parameters
map
key
Returns
template<typename IntegerType >
static IntegerType redis3m::patterns::model::read_int_from_map ( const std::map< std::string, std::string > &  map,
const std::string &  key,
const IntegerType  default_value = 0 
)
inlinestaticprotected

Read an integer from map, otherwise use the specified default value (0)

Parameters
map
key
default_value
Returns
static std::string redis3m::patterns::model::read_str_from_map ( const std::map< std::string, std::string > &  map,
const std::string &  key,
const std::string &  default_value = "" 
)
inlinestaticprotected

Convenient method to get a field from a map, or returning a default value if the key is not present. Useful on constructor.

Parameters
map
key
default_value
Returns
std::map<std::string, std::string> redis3m::patterns::model::to_map ( )
inline

Serialize all object data to a string:string map, it will be saved on Redis on a Hash.

Returns
static std::vector<std::string> redis3m::patterns::model::tracked ( )
inlinestatic

Keys associated with a Model, may be sets or lists usually. They will be saved as <Modelname>:<modelid>:<keyname>

Returns
A vector of keys,
static std::vector< std::string > redis3m::patterns::model::uniques ( )
inlinestatic

A vector of fields to be indexed in a unique fashion, used by Orm.

Returns
static void redis3m::patterns::model::write_bool_to_map ( std::map< std::string, std::string > &  map,
const std::string &  key,
const bool  value 
)
inlinestaticprotected

Write a boolean to a map, only if it's true.

Parameters
map
key
value
template<typename IntegerType >
static void redis3m::patterns::model::write_int_to_map ( std::map< std::string, std::string > &  map,
const std::string &  key,
const IntegerType  value,
const IntegerType  default_value = 0 
)
inlinestaticprotected

Write an integer to a map, only if it's different from default value.

Parameters
map
key
value
default_value
static void redis3m::patterns::model::write_str_to_map ( std::map< std::string, std::string > &  map,
const std::string &  key,
const std::string &  value,
const std::string &  default_value = "" 
)
inlinestaticprotected

Write a string to a map, useful on to_map() method. It saves value on the map only if it's different from default.

Parameters
map
key
value
default_value

The documentation for this class was generated from the following file: