Registry.h
1 //-*-C++-*-
2 /***************************************************************************
3  *
4  * Copyright (C) 2004 - 2016 by Willem van Straten
5  * Licensed under the Academic Free License version 2.1
6  *
7  ***************************************************************************/
8 
9 // psrchive/Util/units/Registry.h
10 
11 #ifndef __Registry_h
12 #define __Registry_h
13 
14 #include "ReferenceTo.h"
15 
16 #include <vector>
17 #include <iostream>
18 
19 namespace Registry {
20 
21  template<class Parent> class List;
22 
24  template<class Parent>
25  class Entry : public Reference::Able
26  {
27  public:
28 
29  friend class List<Parent>;
30 
32  Parent* get () const { return instance; }
33 
35  Entry () { instance = 0; }
36 
38  virtual ~Entry () { if (instance) delete instance; }
39 
40  protected:
41 
43  void register_child (Parent* _instance)
44  {
46 
48  std::cerr << "Registry::Entry<Parent>::register_child"
49  " instance=" << _instance << " registry=" << &registry <<
50  " size=" << registry.size() + 1 << std::endl;
51 
52  instance = _instance;
53  registry.add (this);
54  }
55 
57  virtual Parent* create () const = 0;
58 
59  private:
60  Parent* instance;
61  };
62 
64  template<class Parent>
65  class List : public Reference::Able
66  {
67 
68  friend class Entry<Parent>;
69 
70  public:
71 
73  template<class Child> class Instances;
74 
76  template<class Child> class Enter;
77 
79  template<class Child, class Argument> class Unary;
80 
82  unsigned size () const { return entries.size(); }
83 
85  Parent* operator[] (unsigned i) const { return entries[i]->get(); }
86 
88  void erase (unsigned i) { entries.erase( entries.begin() + i ); }
89 
91  Parent* create (unsigned i) const { return entries[i]->create(); }
92 
94  static bool verbose;
95 
97  static List& get_registry()
98  { if (!registry) { auto_delete = registry = new List; } return *registry; }
99 
100  protected:
101 
103  void add (Entry<Parent>* entry) { entries.push_back (entry); }
104 
106  std::vector< Reference::To< Entry<Parent> > > entries;
107 
109  static List* registry;
110 
113  };
114 
115  template<class Parent>
116  template<class Child>
117  class List<Parent>::Instances
118  {
119  public:
120  static unsigned get_instances () { return instances; }
121  static void add_instance () { instances++; }
122  private:
123  static unsigned instances;
124  };
125 
127  template<class Parent>
128  template<class Child>
129  class List<Parent>::Enter : public Entry<Parent>
130  {
131  public:
133  Enter ()
134  {
135  if (Instances<Child>::get_instances() == 0)
136  this->register_child (new Child);
137  Instances<Child>::add_instance();
138  }
139 
140  protected:
142  Parent* create () const { return new Child; }
143  };
144 
146  template<class Parent>
147  template<class Child, class Argument>
148  class List<Parent>::Unary : public Entry<Parent>
149  {
150  public:
151 
153  Unary (const Argument& arg)
154  {
155  argument = arg;
157  this->register_child( new Child(arg) );
159  }
160  protected:
162  Parent* create () const { return new Child(argument); }
164  Argument argument;
165  };
166 
167 
168 }
169 
170 template<class Parent>
171 template<class Child>
172 unsigned Registry::List<Parent>::Instances<Child>::instances = 0;
173 
174 template<class Parent>
176 
177 template<class Parent>
179 
180 template<class Parent>
182 
183 #endif
static List * registry
The single registry instance for the Parent class.
Definition: Registry.h:114
Entry()
Null constructor.
Definition: Registry.h:40
Parent * get() const
Return the pointer to the instance.
Definition: Registry.h:37
void add(Entry< Parent > *entry)
Add an entry to the registry.
Definition: Registry.h:108
virtual Parent * create() const =0
Return a pointer to a new instance of a Parent (or its children)
static bool verbose
verbosity flag used for debugging
Definition: Registry.h:99
std::vector< Reference::To< Entry< Parent > > > entries
The vector of registry entries.
Definition: Registry.h:111
Parent * create(unsigned i) const
Return pointer to a null-constructed instance of Parent class.
Definition: Registry.h:96
Template class manages Reference::Able objects.
Definition: Reference.h:74
static List & get_registry()
provide access to the single registry instance
Definition: Registry.h:102
unsigned size() const
Return the size of the list.
Definition: Registry.h:87
Manages Reference::To references to the instance.
Definition: ReferenceAble.h:40
void erase(unsigned i)
Remove the specified entry.
Definition: Registry.h:93
Pure virtual template base class of Registry::List<Parent>::Enter<Child>
Definition: Registry.h:30
void register_child(Parent *_instance)
Adds this instance to the Parent::Registry::List<Parent>
Definition: Registry.h:48
List of Registry::Entry.
Definition: Registry.h:26
Counts the number of instances of Child entries.
Definition: Registry.h:78
static Reference::To< List > auto_delete
Clean up the registry when the program exits.
Definition: Registry.h:117
Enter Child with unary constructor into Registry::List<Parent>
Definition: Registry.h:84
virtual ~Entry()
Destructor deletes instance of Parent.
Definition: Registry.h:43
Parent * operator[](unsigned i) const
Return the specified entry.
Definition: Registry.h:90

Generated using doxygen 1.8.17