Skip to content
Snippets Groups Projects
Commit e45f4bdb authored by Wuttke, Joachim's avatar Wuttke, Joachim
Browse files

generic registry interface IRegistry in registry.hpp

parent d69e74a1
No related branches found
No related tags found
No related merge requests found
......@@ -72,6 +72,7 @@ opr.hpp
plot.hpp
ptr.hpp
reg.hpp
registry.hpp
rssm.hpp
singleton.hpp
slice.hpp
......
......@@ -5,7 +5,10 @@
//**************************************************************************************************
//! \file defs.hpp
//! \brief a few basics (include, define, using; .h file only)
//! \brief a few basic includes, a few macros, a few 'using' directives.
#ifndef DEFS_H
#define DEFS_H
#include <iostream>
#include <map>
......@@ -22,3 +25,5 @@ using std::string;
using std::vector;
using std::cout;
using std::cerr;
#endif // DEFS_H
......@@ -17,35 +17,6 @@
#include "coord.hpp"
//! Destructor needed to keep valgrind quiet.
SFuncRegistry::~SFuncRegistry()
{
for ( const CFunc* F: FList )
delete F;
}
//! Returns a pointer to the CFunc of given name, or nullptr.
const CFunc* SFuncRegistry::find( string tag ) const
{
auto pos = FMap.find(tag);
return pos == FMap.end() ? nullptr : pos->second;
}
//! Returns a pointer to the CFunc of given name, or throws.
const CFunc* SFuncRegistry::find_or_fail( string tag ) const
{
const CFunc* ret = find( tag );
if( !ret )
throw "Cannot find function '" + tag + "'";
return ret;
}
//! Prints a list of registered operators, for online help.
void SFuncRegistry::display_operators() const
......
......@@ -7,7 +7,7 @@
//! \file fregistry.hpp
//! \brief Registration of CFunc's.
#include <singleton.hpp>
#include "registry.hpp"
// Low-level function types.
typedef void (*func_0_i) (int);
......@@ -47,20 +47,13 @@ typedef void (*func_e_eee) (double&,double&, double,double, double,double, dou
//! A singleton class, holding a collection of CFunc's.
class SFuncRegistry : public ISingleton<SFuncRegistry> {
class SFuncRegistry : public IRegistry<CFunc>, public ISingleton<SFuncRegistry> {
private:
map<string,class CFunc*> FMap; //! unsorted hash, for expression evaluation
vector<const class CFunc*> FList; //! sorted array, for help text
virtual string type_name() const { return "operator and function"; }
void register_fct_template(
const CFuncMetadata& _m, const char* _outtype, const char* _intypes, funcPtr _f );
public:
~SFuncRegistry();
const class CFunc* find( string nam ) const;
const class CFunc* find_or_fail( string nam ) const;
void display_functions() const;
void display_operators() const;
......
//**************************************************************************************************
//* FRIDA: fast reliable interactive data analysis
//* (C) Joachim Wuttke 2001-
//* http://apps.jcns.fz-juelich.de/frida
//**************************************************************************************************
//! \file registry.hpp
//! \brief declares a generic registry mixin interface.
#ifndef IREGISTRY_H
#define IREGISTRY_H
#include "defs.hpp"
#include "singleton.hpp"
//! Mixin interface for registries holding objects of type T
template<class T>
class IRegistry {
protected:
virtual string type_name() const = 0;
map<string,T*> FMap; //! unsorted hash, for expression evaluation
vector<const T*> FList; //! sorted array, for help text
public:
virtual ~IRegistry() { for ( const T* F: FList ) delete F; }
const T* find( string key ) const {
auto pos = FMap.find(key);
return pos == FMap.end() ? nullptr : pos->second; }
const T* find_or_fail( string key ) const {
const T* ret = find( key );
if( !ret )
throw "Cannot find '" + key + "' in " + type_name() + " registry";
return ret; }
};
#endif // IREGISTRY_H
......@@ -11,6 +11,7 @@
#define ISINGLETON_H
//! Mixin interface for singleton classes.
template<class T>
class ISingleton {
private:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment