Anope IRC Services  Version 2.0
serialize.cpp
Go to the documentation of this file.
1 /*
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  * Based on the original code of Epona by Lara.
9  * Based on the original code of Services by Andy Church.
10  *
11  */
12 
13 
14 #include "services.h"
15 #include "anope.h"
16 #include "serialize.h"
17 #include "modules.h"
18 #include "account.h"
19 #include "bots.h"
20 #include "regchannel.h"
21 #include "xline.h"
22 #include "access.h"
23 
24 using namespace Serialize;
25 
26 std::vector<Anope::string> Type::TypeOrder;
27 std::map<Anope::string, Type *> Serialize::Type::Types;
28 std::list<Serializable *> *Serializable::SerializableItems;
29 
31 {
32  static Type nc("NickCore", NickCore::Unserialize), na("NickAlias", NickAlias::Unserialize), bi("BotInfo", BotInfo::Unserialize),
33  ci("ChannelInfo", ChannelInfo::Unserialize), access("ChanAccess", ChanAccess::Unserialize),
34  akick("AutoKick", AutoKick::Unserialize), memo("Memo", Memo::Unserialize), xline("XLine", XLine::Unserialize);
35 }
36 
38 {
39  for (std::map<Anope::string, Serialize::Type *>::const_iterator it = Serialize::Type::GetTypes().begin(), it_end = Serialize::Type::GetTypes().end(); it != it_end; ++it)
40  {
41  Serialize::Type *t = it->second;
42  t->Check();
43  }
44 }
45 
46 Serializable::Serializable(const Anope::string &serialize_type) : last_commit(0), last_commit_time(0), id(0), redis_ignore(0)
47 {
48  if (SerializableItems == NULL)
49  SerializableItems = new std::list<Serializable *>();
50  SerializableItems->push_back(this);
51 
52  this->s_type = Type::Find(serialize_type);
53 
54  this->s_iter = SerializableItems->end();
55  --this->s_iter;
56 
57  FOREACH_MOD(OnSerializableConstruct, (this));
58 }
59 
60 Serializable::Serializable(const Serializable &other) : last_commit(0), last_commit_time(0), id(0), redis_ignore(0)
61 {
62  SerializableItems->push_back(this);
63  this->s_iter = SerializableItems->end();
64  --this->s_iter;
65 
66  this->s_type = other.s_type;
67 
68  FOREACH_MOD(OnSerializableConstruct, (this));
69 }
70 
72 {
73  FOREACH_MOD(OnSerializableDestruct, (this));
74 
75  SerializableItems->erase(this->s_iter);
76 }
77 
79 {
80  return *this;
81 }
82 
84 {
85  /* Schedule updater */
86  FOREACH_MOD(OnSerializableUpdate, (this));
87 
88  /* Check for modifications now - this can delete this object! */
89  FOREACH_MOD(OnSerializeCheck, (this->GetSerializableType()));
90 }
91 
93 {
94  return this->last_commit == data.Hash();
95 }
96 
98 {
99  this->last_commit = data.Hash();
100 }
101 
103 {
104  return this->last_commit_time == Anope::CurTime;
105 }
106 
108 {
110 }
111 
112 const std::list<Serializable *> &Serializable::GetItems()
113 {
114  return *SerializableItems;
115 }
116 
117 Type::Type(const Anope::string &n, unserialize_func f, Module *o) : name(n), unserialize(f), owner(o), timestamp(0)
118 {
119  TypeOrder.push_back(this->name);
120  Types[this->name] = this;
121 
122  FOREACH_MOD(OnSerializeTypeCreate, (this));
123 }
124 
125 Type::~Type()
126 {
127  std::vector<Anope::string>::iterator it = std::find(TypeOrder.begin(), TypeOrder.end(), this->name);
128  if (it != TypeOrder.end())
129  TypeOrder.erase(it);
130  Types.erase(this->name);
131 }
132 
133 Serializable *Type::Unserialize(Serializable *obj, Serialize::Data &data)
134 {
135  return this->unserialize(obj, data);
136 }
137 
138 void Type::Check()
139 {
140  FOREACH_MOD(OnSerializeCheck, (this));
141 }
142 
143 time_t Type::GetTimestamp() const
144 {
145  return this->timestamp;
146 }
147 
148 void Type::UpdateTimestamp()
149 {
150  this->timestamp = Anope::CurTime;
151 }
152 
153 Type *Serialize::Type::Find(const Anope::string &name)
154 {
155  std::map<Anope::string, Type *>::iterator it = Types.find(name);
156  if (it != Types.end())
157  return it->second;
158  return NULL;
159 }
160 
161 const std::vector<Anope::string> &Type::GetTypeOrder()
162 {
163  return TypeOrder;
164 }
165 
166 const std::map<Anope::string, Serialize::Type *>& Type::GetTypes()
167 {
168  return Types;
169 }
170 
static Serializable * Unserialize(Serializable *obj, Serialize::Data &)
Definition: access.cpp:220
Definition: hashcomp.h:84
static Serializable * Unserialize(Serializable *obj, Serialize::Data &)
Definition: regchannel.cpp:238
std::list< Serializable * >::iterator s_iter
Definition: serialize.h:67
void push_back(char c)
Definition: anope.h:142
void UpdateCache(Serialize::Data &)
Definition: serialize.cpp:97
void CheckTypes()
Definition: serialize.cpp:37
void QueueUpdate()
Definition: serialize.cpp:83
CoreExport time_t CurTime
Definition: main.cpp:41
#define FOREACH_MOD(ename, args)
Definition: modules.h:62
void RegisterTypes()
Definition: serialize.cpp:30
void UpdateTS()
Definition: serialize.cpp:107
virtual size_t Hash() const
Definition: serialize.h:36
Serialize::Type * s_type
Definition: serialize.h:64
static Serializable * Unserialize(Serializable *obj, Serialize::Data &)
Definition: bots.cpp:87
static Serializable * Unserialize(Serializable *obj, Serialize::Data &)
Definition: memos.cpp:49
static const std::list< Serializable * > & GetItems()
Definition: serialize.cpp:112
static Serializable * Unserialize(Serializable *obj, Serialize::Data &)
Definition: nickcore.cpp:79
virtual ~Serializable()
Definition: serialize.cpp:71
Serializable(const Anope::string &serialize_type)
Definition: serialize.cpp:46
Serializable & operator=(const Serializable &)
Definition: serialize.cpp:78
size_t last_commit
Definition: serialize.h:69
static Serializable * Unserialize(Serializable *obj, Serialize::Data &)
Definition: nickalias.cpp:155
static Serializable * Unserialize(Serializable *obj, Serialize::Data &data)
Definition: xline.cpp:164
time_t last_commit_time
Definition: serialize.h:71
Anope::string name
Definition: access.cpp:22
bool IsCached(Serialize::Data &)
Definition: serialize.cpp:92
static std::list< Serializable * > * SerializableItems
Definition: serialize.h:62
bool IsTSCached()
Definition: serialize.cpp:102
static Serializable * Unserialize(Serializable *obj, Serialize::Data &)
Definition: regchannel.cpp:56
Serialize::Type * GetSerializableType() const
Definition: serialize.h:101
Type(const Anope::string &n, unserialize_func f, Module *owner=NULL)