binutils-gdb/gdb/registry.h
Tom Tromey 08b8a139c9 Rewrite registry.h
This rewrites registry.h, removing all the macros and replacing it
with relatively ordinary template classes.  The result is less code
than the previous setup.  It replaces large macros with a relatively
straightforward C++ class, and now manages its own cleanup.

The existing type-safe "key" class is replaced with the equivalent
template class.  This approach ended up requiring relatively few
changes to the users of the registry code in gdb -- code using the key
system just required a small change to the key's declaration.

All existing users of the old C-like API are now converted to use the
type-safe API.  This mostly involved changing explicit deletion
functions to be an operator() in a deleter class.

The old "save/free" two-phase process is removed, and replaced with a
single "free" phase.  No existing code used both phases.

The old "free" callbacks took a parameter for the enclosing container
object.  However, this wasn't truly needed and is removed here as
well.
2022-07-28 14:16:50 -06:00

236 lines
6.9 KiB
C++

/* Macros for general registry objects.
Copyright (C) 2011-2022 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef REGISTRY_H
#define REGISTRY_H
#include <type_traits>
template<typename T> class registry;
/* An accessor class that is used by registry_key.
Normally, a container class has a registry<> field named
"registry_fields". In this case, the default accessor is used, as
it simply returns the object.
However, a container may sometimes need to store the registry
elsewhere. In this case, registry_accessor can be specialized to
perform the needed indirection. */
template<typename T>
struct registry_accessor
{
/* Given a container of type T, return its registry. */
static registry<T> *get (T *obj)
{
return &obj->registry_fields;
}
};
/* In gdb, sometimes there is a need for one module (e.g., the Python
Type code) to attach some data to another object (e.g., an
objfile); but it's also desirable that this be done such that the
base object (the objfile in this example) not need to know anything
about the attaching module (the Python code).
This is handled using the registry system.
A class needing to allow this sort registration can add a registry
field. For example, you would write:
class some_container { registry<some_container> registry_fields; };
The name of the field matters by default, see registry_accessor.
A module wanting to attach data to instances of some_container uses
the "key" class to register a key. This key can then be passed to
the "get" and "set" methods to handle this module's data. */
template<typename T>
class registry
{
public:
registry ()
: m_fields (get_registrations ().size ())
{
}
~registry ()
{
clear_registry ();
}
DISABLE_COPY_AND_ASSIGN (registry);
/* A type-safe registry key.
The registry itself holds just a "void *". This is not always
convenient to manage, so this template class can be used instead,
to provide a type-safe interface, that also helps manage the
lifetime of the stored objects.
When the container is destroyed, this key arranges to destroy the
underlying data using Deleter. This defaults to
std::default_delete. */
template<typename DATA, typename Deleter = std::default_delete<DATA>>
class key
{
public:
key ()
: m_key (registry<T>::new_key (cleanup))
{
}
DISABLE_COPY_AND_ASSIGN (key);
/* Fetch the data attached to OBJ that is associated with this key.
If no such data has been attached, nullptr is returned. */
DATA *get (T *obj) const
{
registry<T> *reg_obj = registry_accessor<T>::get (obj);
return (DATA *) reg_obj->get (m_key);
}
/* Attach DATA to OBJ, associated with this key. Note that any
previous data is simply dropped -- if destruction is needed,
'clear' should be called. */
void set (T *obj, DATA *data) const
{
registry<T> *reg_obj = registry_accessor<T>::get (obj);
reg_obj->set (m_key, data);
}
/* If this key uses the default deleter, then this method is
available. It emplaces a new instance of the associated data
type and attaches it to OBJ using this key. The arguments, if
any, are forwarded to the constructor. */
template<typename Dummy = DATA *, typename... Args>
typename std::enable_if<std::is_same<Deleter,
std::default_delete<DATA>>::value,
Dummy>::type
emplace (T *obj, Args &&...args) const
{
DATA *result = new DATA (std::forward<Args> (args)...);
set (obj, result);
return result;
}
/* Clear the data attached to OBJ that is associated with this KEY.
Any existing data is destroyed using the deleter, and the data is
reset to nullptr. */
void clear (T *obj) const
{
DATA *datum = get (obj);
if (datum != nullptr)
{
cleanup (datum);
set (obj, nullptr);
}
}
private:
/* A helper function that is called by the registry to delete the
contained object. */
static void cleanup (void *arg)
{
DATA *datum = (DATA *) arg;
Deleter d;
d (datum);
}
/* The underlying key. */
const typename registry<T>::registry_data *m_key;
};
/* Clear all the data associated with this container. This is
dangerous and should not normally be done. */
void clear_registry ()
{
/* Call all the free functions. */
for (const auto &datum : get_registrations ())
{
void *elt = m_fields[datum->index];
if (elt != nullptr)
{
datum->free (elt);
m_fields[datum->index] = nullptr;
}
}
}
private:
/* Registry callbacks have this type. */
typedef void (*registry_data_callback) (void *);
/* The type of a key. */
struct registry_data
{
unsigned index;
registry_data_callback free;
};
/* Get a new key for this particular registry. FREE is a callback.
When the container object is destroyed, all FREE functions are
called. The data associated with the container object is passed
to the callback. */
static const registry_data *new_key (registry_data_callback free)
{
std::unique_ptr<registry_data> result (new registry_data);
std::vector<std::unique_ptr<registry_data>> &registrations
= get_registrations ();
result->index = registrations.size ();
result->free = free;
registrations.emplace_back (std::move (result));
return registrations.back ().get ();
}
/* Set the datum associated with KEY in this container. */
void set (const registry_data *key, void *datum)
{
m_fields[key->index] = datum;
}
/* Fetch the datum associated with KEY in this container. If 'set'
has not been called for this key, nullptr is returned. */
void *get (const registry_data *key)
{
return m_fields[key->index];
}
/* The data stored in this instance. */
std::vector<void *> m_fields;
/* Return a reference to the vector of all the registrations that
have been made. We do separate allocations here so that the
addresses are stable and can be used as keys. */
static std::vector<std::unique_ptr<registry_data>> &get_registrations ()
{
static std::vector<std::unique_ptr<registry_data>> registrations;
return registrations;
}
};
#endif /* REGISTRY_H */