GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GRegistry.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GRegistry.hpp - Interface class for registries *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2012-2021 by Juergen Knoedlseder *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 /**
22  * @file GRegistry.hpp
23  * @brief Interface class definition for registries
24  * @author Juergen Knoedlseder
25  */
26 
27 #ifndef GREGISTRY_HPP
28 #define GREGISTRY_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <string>
32 #include <iostream>
33 #include "GLog.hpp"
34 #include "GTypemaps.hpp"
35 
36 
37 /***********************************************************************//**
38  * @class GRegistryPointer
39  *
40  * @brief Smart pointer for registry classes
41  ***************************************************************************/
42 template <class T>
44 
45 public:
46  // Constructors and destructors
47  GRegistryPointer(void) : m_ptr(0) { }
49  virtual ~GRegistryPointer(void) { free_members(); }
50 
51  // Operators
53  if (this != &ptr) {
54  free_members();
55  m_ptr = ptr.m_ptr;
56  }
57  return *this;
58  }
59  T& operator*() const { return *m_ptr; }
60  T* operator->() const { return m_ptr; }
61  T& operator[](const int& index) const { return m_ptr[index]; }
62 
63  // Methods
64  void assign(T* ptr) {
65  free_members();
66  m_ptr = ptr;
67  }
68 
69 private:
70  // Protected methods
71  void free_members(void) {
72  if (m_ptr != NULL) delete [] m_ptr;
73  }
74 
75  // Private members
76  T* m_ptr; //!< Pointer
77 };
78 
79 
80 /***********************************************************************//**
81  * @class GRegistry
82  *
83  * @brief Interface class for registries
84  *
85  * This class defines the interface for registries. A registry is a
86  * container class that contains instance of derived classes. For example,
87  * if three different derived classes exist for a given base class, the
88  * registry will contain one instance for each derived class. Using the
89  * clone mechanism, the registry may thus provide a new instance of a given
90  * derived class, depending on the type of the derived class.
91  *
92  * The interface class requires the implementation of the following methods
93  * for all registry classes:
94  *
95  * size(void) Number of elements in registry class
96  *
97  * name(const int& index) Name of registered class by index
98  *
99  * print() Print content of registry
100  ***************************************************************************/
101 class GRegistry {
102 
103 public:
104  /// @brief Destructor
105  ///
106  /// Destroys class.
107  virtual ~GRegistry(void) {}
108 
109  /// @brief Return class name
110  ///
111  /// @return String containing the class name.
112  ///
113  /// Returns the class name for non-abstract classes in a human readable
114  /// way.
115  virtual std::string classname(void) const = 0;
116 
117  /// @brief Return number of classes in registry
118  ///
119  /// Returns the number of classes that have been registered in the
120  /// registry.
121  virtual int size(void) const = 0;
122 
123  /// @brief Return name of registered class by index
124  ///
125  /// @param[in] index Class index [0,...,size()-1]
126  ///
127  /// Returns the name of a registered class, specified by its index in
128  /// the registry.
129  virtual std::string name(const int& index) const = 0;
130 
131  /// @brief Print content of object
132  ///
133  /// @return String containing the content of the object.
134  ///
135  /// Formats the content in a standard way and puts this content in a
136  /// C++ string that is returned.
137  virtual std::string print(const GChatter& chatter = NORMAL) const = 0;
138 
139  // Implement methods
140  std::string content(void) const;
141 };
142 
143 /* __ Prototypes _________________________________________________________ */
144 std::ostream& operator<<(std::ostream& os, const GRegistry& registry);
145 GLog& operator<<(GLog& log, const GRegistry& registry);
146 
147 #endif /* GREGISTRY_HPP */
Interface class for registries.
Definition: GRegistry.hpp:101
void assign(T *ptr)
Definition: GRegistry.hpp:64
Smart pointer for registry classes.
Definition: GRegistry.hpp:43
virtual ~GRegistryPointer(void)
Definition: GRegistry.hpp:49
std::string content(void) const
Return list of names in registry.
Definition: GRegistry.cpp:54
Information logger interface definition.
Definition: GLog.hpp:62
GRegistryPointer(const GRegistryPointer< T > &ptr)
Definition: GRegistry.hpp:48
GVector log(const GVector &vector)
Computes natural logarithm of vector elements.
Definition: GVector.cpp:1274
std::ostream & operator<<(std::ostream &os, const GBase &base)
Output operator.
Definition: GBase.cpp:57
GChatter
Definition: GTypemaps.hpp:33
T & operator*() const
Definition: GRegistry.hpp:59
virtual ~GRegistry(void)
Destructor.
Definition: GRegistry.hpp:107
T & operator[](const int &index) const
Definition: GRegistry.hpp:61
Information logger class definition.
Definition of GammaLib typemaps.
virtual std::string print(const GChatter &chatter=NORMAL) const =0
Print content of object.
GRegistryPointer< T > & operator=(const GRegistryPointer< T > &ptr)
Definition: GRegistry.hpp:52
void free_members(void)
Definition: GRegistry.hpp:71
virtual std::string classname(void) const =0
Return class name.
virtual int size(void) const =0
Return number of classes in registry.
T * operator->() const
Definition: GRegistry.hpp:60
T * m_ptr
Pointer.
Definition: GRegistry.hpp:76
GRegistryPointer(void)
Definition: GRegistry.hpp:47
virtual std::string name(const int &index) const =0
Return name of registered class by index.