serialize.cpp

Go to the documentation of this file.
00001 /*
00002  *
00003  * (C) 2003-2013 Anope Team
00004  * Contact us at team@anope.org
00005  *
00006  * Please read COPYING and README for further details.
00007  *
00008  * Based on the original code of Epona by Lara.
00009  * Based on the original code of Services by Andy Church.
00010  *
00011  */
00012 
00013 
00014 #include "services.h"
00015 #include "anope.h"
00016 #include "serialize.h"
00017 #include "modules.h"
00018 #include "account.h"
00019 #include "bots.h"
00020 #include "regchannel.h"
00021 #include "xline.h"
00022 #include "access.h"
00023 
00024 using namespace Serialize;
00025 
00026 std::vector<Anope::string> Type::TypeOrder;
00027 std::map<Anope::string, Type *> Serialize::Type::Types;
00028 std::list<Serializable *> *Serializable::SerializableItems;
00029 
00030 void Serialize::RegisterTypes()
00031 {
00032         static Type nc("NickCore", NickCore::Unserialize), na("NickAlias", NickAlias::Unserialize), bi("BotInfo", BotInfo::Unserialize),
00033                 ci("ChannelInfo", ChannelInfo::Unserialize), access("ChanAccess", ChanAccess::Unserialize), logsetting("LogSetting", LogSetting::Unserialize),
00034                 modelock("ModeLock", ModeLock::Unserialize), akick("AutoKick", AutoKick::Unserialize), badword("BadWord", BadWord::Unserialize),
00035                 memo("Memo", Memo::Unserialize), xline("XLine", XLine::Unserialize);
00036 }
00037 
00038 void Serialize::CheckTypes()
00039 {
00040         for (std::map<Anope::string, Serialize::Type *>::const_iterator it = Serialize::Type::GetTypes().begin(), it_end = Serialize::Type::GetTypes().end(); it != it_end; ++it)
00041         {
00042                 Serialize::Type *t = it->second;
00043                 t->Check();
00044         }
00045 }
00046 
00047 Serializable::Serializable(const Anope::string &serialize_type) : last_commit(0), last_commit_time(0), id(0)
00048 {
00049         if (SerializableItems == NULL)
00050                 SerializableItems = new std::list<Serializable *>();
00051         SerializableItems->push_back(this);
00052 
00053         this->s_type = Type::Find(serialize_type);
00054 
00055         this->s_iter = SerializableItems->end();
00056         --this->s_iter;
00057 
00058         FOREACH_MOD(I_OnSerializableConstruct, OnSerializableConstruct(this));
00059 }
00060 
00061 Serializable::Serializable(const Serializable &other) : last_commit(0), last_commit_time(0), id(0)
00062 {
00063         SerializableItems->push_back(this);
00064         this->s_iter = SerializableItems->end();
00065         --this->s_iter;
00066 
00067         this->s_type = other.s_type;
00068 
00069         FOREACH_MOD(I_OnSerializableConstruct, OnSerializableConstruct(this));
00070 }
00071 
00072 Serializable::~Serializable()
00073 {
00074         FOREACH_MOD(I_OnSerializableDestruct, OnSerializableDestruct(this));
00075 
00076         SerializableItems->erase(this->s_iter);
00077 }
00078 
00079 Serializable &Serializable::operator=(const Serializable &)
00080 {
00081         return *this;
00082 }
00083 
00084 void Serializable::QueueUpdate()
00085 {
00086         /* Schedule updater */
00087         FOREACH_MOD(I_OnSerializableUpdate, OnSerializableUpdate(this));
00088 
00089         /* Check for modifications now - this can delete this object! */
00090         FOREACH_MOD(I_OnSerializeCheck, OnSerializeCheck(this->GetSerializableType()));
00091 }
00092 
00093 bool Serializable::IsCached(Serialize::Data &data)
00094 {
00095         return this->last_commit == data.Hash();
00096 }
00097 
00098 void Serializable::UpdateCache(Serialize::Data &data)
00099 {
00100         this->last_commit = data.Hash();
00101 }
00102 
00103 bool Serializable::IsTSCached()
00104 {
00105         return this->last_commit_time == Anope::CurTime;
00106 }
00107 
00108 void Serializable::UpdateTS()
00109 {
00110         this->last_commit_time = Anope::CurTime;
00111 }
00112 
00113 const std::list<Serializable *> &Serializable::GetItems()
00114 {
00115         return *SerializableItems;
00116 }
00117 
00118 Type::Type(const Anope::string &n, unserialize_func f, Module *o)  : name(n), unserialize(f), owner(o), timestamp(0)
00119 {
00120         TypeOrder.push_back(this->name);
00121         Types[this->name] = this;
00122 
00123         FOREACH_MOD(I_OnSerializeTypeCreate, OnSerializeTypeCreate(this));
00124 }
00125 
00126 Type::~Type()
00127 {
00128         std::vector<Anope::string>::iterator it = std::find(TypeOrder.begin(), TypeOrder.end(), this->name);
00129         if (it != TypeOrder.end())
00130                 TypeOrder.erase(it);
00131         Types.erase(this->name);
00132 }
00133 
00134 Serializable *Type::Unserialize(Serializable *obj, Serialize::Data &data)
00135 {
00136         return this->unserialize(obj, data);
00137 }
00138 
00139 void Type::Check()
00140 {
00141         FOREACH_MOD(I_OnSerializeCheck, OnSerializeCheck(this));
00142 }
00143 
00144 time_t Type::GetTimestamp() const
00145 {
00146         return this->timestamp;
00147 }
00148 
00149 void Type::UpdateTimestamp()
00150 {
00151         this->timestamp = Anope::CurTime;
00152 }
00153 
00154 Type *Serialize::Type::Find(const Anope::string &name)
00155 {
00156         std::map<Anope::string, Type *>::iterator it = Types.find(name);
00157         if (it != Types.end())
00158                 return it->second;
00159         return NULL;
00160 }
00161 
00162 const std::vector<Anope::string> &Type::GetTypeOrder()
00163 {
00164         return TypeOrder;
00165 }
00166 
00167 const std::map<Anope::string, Serialize::Type *>& Type::GetTypes()
00168 {
00169         return Types;
00170 }
00171