anope

- supernets anope source code & configuration
git clone git://git.acid.vegas/anope.git
Log | Files | Refs | Archive | README

serialize.cpp (4268B)

      1 /*
      2  *
      3  * (C) 2003-2022 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 #include "services.h"
     13 #include "anope.h"
     14 #include "serialize.h"
     15 #include "modules.h"
     16 #include "account.h"
     17 #include "bots.h"
     18 #include "regchannel.h"
     19 #include "xline.h"
     20 #include "access.h"
     21 
     22 using namespace Serialize;
     23 
     24 std::vector<Anope::string> Type::TypeOrder;
     25 std::map<Anope::string, Type *> Serialize::Type::Types;
     26 std::list<Serializable *> *Serializable::SerializableItems;
     27 
     28 void Serialize::RegisterTypes()
     29 {
     30 	static Type nc("NickCore", NickCore::Unserialize), na("NickAlias", NickAlias::Unserialize), bi("BotInfo", BotInfo::Unserialize),
     31 		ci("ChannelInfo", ChannelInfo::Unserialize), access("ChanAccess", ChanAccess::Unserialize),
     32 		akick("AutoKick", AutoKick::Unserialize), memo("Memo", Memo::Unserialize), xline("XLine", XLine::Unserialize);
     33 }
     34 
     35 void Serialize::CheckTypes()
     36 {
     37 	for (std::map<Anope::string, Serialize::Type *>::const_iterator it = Serialize::Type::GetTypes().begin(), it_end = Serialize::Type::GetTypes().end(); it != it_end; ++it)
     38 	{
     39 		Serialize::Type *t = it->second;
     40 		t->Check();
     41 	}
     42 }
     43 
     44 Serializable::Serializable(const Anope::string &serialize_type) : last_commit(0), last_commit_time(0), id(0), redis_ignore(0)
     45 {
     46 	if (SerializableItems == NULL)
     47 		SerializableItems = new std::list<Serializable *>();
     48 	SerializableItems->push_back(this);
     49 
     50 	this->s_type = Type::Find(serialize_type);
     51 
     52 	this->s_iter = SerializableItems->end();
     53 	--this->s_iter;
     54 
     55 	FOREACH_MOD(OnSerializableConstruct, (this));
     56 }
     57 
     58 Serializable::Serializable(const Serializable &other) : last_commit(0), last_commit_time(0), id(0), redis_ignore(0)
     59 {
     60 	SerializableItems->push_back(this);
     61 	this->s_iter = SerializableItems->end();
     62 	--this->s_iter;
     63 
     64 	this->s_type = other.s_type;
     65 
     66 	FOREACH_MOD(OnSerializableConstruct, (this));
     67 }
     68 
     69 Serializable::~Serializable()
     70 {
     71 	FOREACH_MOD(OnSerializableDestruct, (this));
     72 
     73 	SerializableItems->erase(this->s_iter);
     74 }
     75 
     76 Serializable &Serializable::operator=(const Serializable &)
     77 {
     78 	return *this;
     79 }
     80 
     81 void Serializable::QueueUpdate()
     82 {
     83 	/* Schedule updater */
     84 	FOREACH_MOD(OnSerializableUpdate, (this));
     85 
     86 	/* Check for modifications now - this can delete this object! */
     87 	FOREACH_MOD(OnSerializeCheck, (this->GetSerializableType()));
     88 }
     89 
     90 bool Serializable::IsCached(Serialize::Data &data)
     91 {
     92 	return this->last_commit == data.Hash();
     93 }
     94 
     95 void Serializable::UpdateCache(Serialize::Data &data)
     96 {
     97 	this->last_commit = data.Hash();
     98 }
     99 
    100 bool Serializable::IsTSCached()
    101 {
    102 	return this->last_commit_time == Anope::CurTime;
    103 }
    104 
    105 void Serializable::UpdateTS()
    106 {
    107 	this->last_commit_time = Anope::CurTime;
    108 }
    109 
    110 const std::list<Serializable *> &Serializable::GetItems()
    111 {
    112 	return *SerializableItems;
    113 }
    114 
    115 Type::Type(const Anope::string &n, unserialize_func f, Module *o)  : name(n), unserialize(f), owner(o), timestamp(0)
    116 {
    117 	TypeOrder.push_back(this->name);
    118 	Types[this->name] = this;
    119 
    120 	FOREACH_MOD(OnSerializeTypeCreate, (this));
    121 }
    122 
    123 Type::~Type()
    124 {
    125 	/* null the type of existing serializable objects of this type */
    126 	if (Serializable::SerializableItems != NULL)
    127 		for (std::list<Serializable *>::iterator it = Serializable::SerializableItems->begin(); it != Serializable::SerializableItems->end(); ++it)
    128 		{
    129 			Serializable *s = *it;
    130 
    131 			if (s->s_type == this)
    132 				s->s_type = NULL;
    133 		}
    134 
    135 	std::vector<Anope::string>::iterator it = std::find(TypeOrder.begin(), TypeOrder.end(), this->name);
    136 	if (it != TypeOrder.end())
    137 		TypeOrder.erase(it);
    138 	Types.erase(this->name);
    139 }
    140 
    141 Serializable *Type::Unserialize(Serializable *obj, Serialize::Data &data)
    142 {
    143 	return this->unserialize(obj, data);
    144 }
    145 
    146 void Type::Check()
    147 {
    148 	FOREACH_MOD(OnSerializeCheck, (this));
    149 }
    150 
    151 time_t Type::GetTimestamp() const
    152 {
    153 	return this->timestamp;
    154 }
    155 
    156 void Type::UpdateTimestamp()
    157 {
    158 	this->timestamp = Anope::CurTime;
    159 }
    160 
    161 Type *Serialize::Type::Find(const Anope::string &name)
    162 {
    163 	std::map<Anope::string, Type *>::iterator it = Types.find(name);
    164 	if (it != Types.end())
    165 		return it->second;
    166 	return NULL;
    167 }
    168 
    169 const std::vector<Anope::string> &Type::GetTypeOrder()
    170 {
    171 	return TypeOrder;
    172 }
    173 
    174 const std::map<Anope::string, Serialize::Type *>& Type::GetTypes()
    175 {
    176 	return Types;
    177 }