Add {To,From}{Human,Internal,Network} to ExtensionItem.

Also, deprecate the old SerializeFormat/serialize/unserialise API.
This commit is contained in:
Peter Powell 2019-08-07 13:40:01 +01:00
parent 704f0fca5f
commit 85182d727c
8 changed files with 109 additions and 24 deletions

View File

@ -19,15 +19,12 @@
#pragma once
/** DEPRECATED: use {To,From}{Human,Internal,Network} instead. */
enum SerializeFormat
{
/** Shown to a human (does not need to be unserializable) */
FORMAT_USER,
/** Passed internally to this process (i.e. for /RELOADMODULE) */
FORMAT_INTERNAL,
/** Passed to other servers on the network (i.e. METADATA s2s command) */
FORMAT_NETWORK,
/** Stored on disk (i.e. permchannel database) */
FORMAT_PERSIST
};
@ -61,20 +58,37 @@ class CoreExport ExtensionItem : public ServiceProvider, public usecountbase
/** Destroys an instance of the ExtensionItem class. */
virtual ~ExtensionItem();
/** Serialize this item into a string
*
* @param format The format to serialize to
* @param container The object containing this item
* @param item The item itself
/** Sets an ExtensionItem using a value in the internal format.
* @param container A container the ExtensionItem should be set on.
* @param value A value in the internal format.
*/
virtual std::string serialize(SerializeFormat format, const Extensible* container, void* item) const = 0;
virtual void FromInternal(Extensible* container, const std::string& value);
/** Convert the string form back into an item
* @param format The format to serialize from (not FORMAT_USER)
* @param container The object that this item applies to
* @param value The return from a serialize() call that was run elsewhere with this key
/** Sets an ExtensionItem using a value in the network format.
* @param container A container the ExtensionItem should be set on.
* @param value A value in the network format.
*/
virtual void unserialize(SerializeFormat format, Extensible* container, const std::string& value) = 0;
virtual void FromNetwork(Extensible* container, const std::string& value);
/** Gets an ExtensionItem's value in a human-readable format.
* @param container The container the ExtensionItem is set on.
* @param item The value to convert to a human-readable format.
* @return The value specified in \p item in a human readable format.
*/
virtual std::string ToHuman(const Extensible* container, void* item) const;
/** Gets an ExtensionItem's value in the internal format.
* @param container The container the ExtensionItem is set on.
* @param item The value to convert to the internal format.
* @return The value specified in \p item in the internal format.
*/
virtual std::string ToInternal(const Extensible* container, void* item) const ;
/** Gets an ExtensionItem's value in the network format.
* @param container The container the ExtensionItem is set on.
* @param item The value to convert to the network format.
* @return The value specified in \p item in the network format.
*/
virtual std::string ToNetwork(const Extensible* container, void* item) const;
/** Deallocates the specified ExtensionItem value.
* @param container The container that the ExtensionItem is set on.
@ -85,6 +99,12 @@ class CoreExport ExtensionItem : public ServiceProvider, public usecountbase
/** Registers this object with the ExtensionManager. */
void RegisterService() CXX11_OVERRIDE;
/** DEPRECATED: use To{Human,Internal,Network} instead. */
DEPRECATED_METHOD(virtual std::string serialize(SerializeFormat format, const Extensible* container, void* item) const);
/** DEPRECATED: use From{Internal,Network} instead. */
DEPRECATED_METHOD(virtual void unserialize(SerializeFormat format, Extensible* container, const std::string& value));
protected:
/** Retrieves the value for this ExtensionItem from the internal map.
* @param container The container that the ExtensionItem is set on.

View File

@ -219,6 +219,71 @@ Extensible::~Extensible()
ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "Extensible destructor called without cull @%p", (void*)this);
}
void ExtensionItem::FromInternal(Extensible* container, const std::string& value)
{
FromNetwork(container, value);
}
void ExtensionItem::FromNetwork(Extensible* container, const std::string& value)
{
}
std::string ExtensionItem::ToHuman(const Extensible* container, void* item) const
{
// Try to use the network form by default.
std::string ret = ToNetwork(container, item);
// If there's no network form then fall back to the internal form.
if (ret.empty())
ret = ToInternal(container, item);
return ret;
}
std::string ExtensionItem::ToInternal(const Extensible* container, void* item) const
{
return ToNetwork(container, item);
}
std::string ExtensionItem::ToNetwork(const Extensible* container, void* item) const
{
return std::string();
}
std::string ExtensionItem::serialize(SerializeFormat format, const Extensible* container, void* item) const
{
// Wrap the deprecated API with the new API.
switch (format)
{
case FORMAT_USER:
return ToHuman(container, item);
case FORMAT_INTERNAL:
case FORMAT_PERSIST:
return ToInternal(container, item);
case FORMAT_NETWORK:
return ToNetwork(container, item);
}
return "";
}
void ExtensionItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
{
// Wrap the deprecated API with the new API.
switch (format)
{
case FORMAT_USER:
break;
case FORMAT_INTERNAL:
case FORMAT_PERSIST:
FromInternal(container, value);
break;
case FORMAT_NETWORK:
FromNetwork(container, value);
break;
}
}
LocalExtItem::LocalExtItem(const std::string& Key, ExtensibleType exttype, Module* mod)
: ExtensionItem(Key, exttype, mod)
{

View File

@ -379,7 +379,7 @@ void DataKeeper::SaveExtensions(Extensible* extensible, std::vector<InstanceData
if (it == setexts.end())
continue;
std::string value = item->serialize(FORMAT_INTERNAL, extensible, it->second);
std::string value = item->ToInternal(extensible, it->second);
// If the serialized value is empty the extension won't be saved and restored
if (!value.empty())
extdata.push_back(InstanceData(index, value));
@ -597,7 +597,7 @@ void DataKeeper::RestoreExtensions(const std::vector<InstanceData>& list, Extens
for (std::vector<InstanceData>::const_iterator i = list.begin(); i != list.end(); ++i)
{
const InstanceData& id = *i;
handledexts[id.index].extitem->unserialize(FORMAT_INTERNAL, extensible, id.serialized);
handledexts[id.index].extitem->FromInternal(extensible, id.serialized);
}
}

View File

@ -90,7 +90,7 @@ class CheckContext
for(Extensible::ExtensibleStore::const_iterator i = ext->GetExtList().begin(); i != ext->GetExtList().end(); ++i)
{
ExtensionItem* item = i->first;
std::string value = item->serialize(FORMAT_USER, ext, i->second);
std::string value = item->ToHuman(ext, i->second);
if (!value.empty())
Write("meta:" + item->name, value);
else if (!item->name.empty())

View File

@ -86,7 +86,7 @@ namespace Stats
for (Extensible::ExtensibleStore::const_iterator i = ext->GetExtList().begin(); i != ext->GetExtList().end(); i++)
{
ExtensionItem* item = i->first;
std::string value = item->serialize(FORMAT_USER, ext, i->second);
std::string value = item->ToHuman(ext, i->second);
if (!value.empty())
data << "<meta name=\"" << item->name << "\">" << Sanitize(value) << "</meta>";
else if (!item->name.empty())

View File

@ -486,7 +486,7 @@ void ModuleSpanningTree::OnUserConnect(LocalUser* user)
for(Extensible::ExtensibleStore::const_iterator i = user->GetExtList().begin(); i != user->GetExtList().end(); i++)
{
ExtensionItem* item = i->first;
std::string value = item->serialize(FORMAT_NETWORK, user, i->second);
std::string value = item->ToNetwork(user, i->second);
if (!value.empty())
ServerInstance->PI->SendMetaData(user, item->name, value);
}

View File

@ -50,7 +50,7 @@ CmdResult CommandMetadata::Handle(User* srcuser, Params& params)
ExtensionItem* item = ServerInstance->Extensions.GetItem(params[2]);
if ((item) && (item->type == ExtensionItem::EXT_CHANNEL))
item->unserialize(FORMAT_NETWORK, c, value);
item->FromNetwork(c, value);
FOREACH_MOD(OnDecodeMetaData, (c,params[2],value));
}
else
@ -62,7 +62,7 @@ CmdResult CommandMetadata::Handle(User* srcuser, Params& params)
std::string value = params.size() < 3 ? "" : params[2];
if ((item) && (item->type == ExtensionItem::EXT_USER))
item->unserialize(FORMAT_NETWORK, u, value);
item->FromNetwork(u, value);
FOREACH_MOD(OnDecodeMetaData, (u,params[1],value));
}
}

View File

@ -259,7 +259,7 @@ void TreeSocket::SyncChannel(Channel* chan, BurstState& bs)
for (Extensible::ExtensibleStore::const_iterator i = chan->GetExtList().begin(); i != chan->GetExtList().end(); i++)
{
ExtensionItem* item = i->first;
std::string value = item->serialize(FORMAT_NETWORK, chan, i->second);
std::string value = item->ToNetwork(chan, i->second);
if (!value.empty())
this->WriteLine(CommandMetadata::Builder(chan, item->name, value));
}
@ -295,7 +295,7 @@ void TreeSocket::SendUsers(BurstState& bs)
for (Extensible::ExtensibleStore::const_iterator i = exts.begin(); i != exts.end(); ++i)
{
ExtensionItem* item = i->first;
std::string value = item->serialize(FORMAT_NETWORK, u->second, i->second);
std::string value = item->ToNetwork(u->second, i->second);
if (!value.empty())
this->WriteLine(CommandMetadata::Builder(user, item->name, value));
}