summaryrefslogtreecommitdiff
path: root/src/core/common/RttiBuilder.hpp
blob: 442f3588cbb0c8896c523336a67b2c933d895ea0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
    Ousía
    Copyright (C) 2014, 2015  Benjamin Paaßen, Andreas Stöckel

    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/>.
*/

/**
 * @file RttiBuilder.hpp
 *
 * Defines a more convenient version of the RttiBuilder.
 *
 * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de)
 */

#ifndef _OUSIA_RTTI_BUILDER_HPP_
#define _OUSIA_RTTI_BUILDER_HPP_

#include "Argument.hpp"
#include "Rtti.hpp"
#include "Function.hpp"
#include "Property.hpp"

namespace ousia {

/**
 * The RttiBuilder class is a more convenient version of the RttiBuilderBase
 * class which allows simple definition of new methods and properties.
 *
 * @tparam T is the C++ class for which the type is being built.
 */
template <class T>
class RttiBuilder : public RttiBuilderBase {
public:
	/**
	 * Default constructor, initializes the name of the type described by the
	 * RttiSet with "unknown".
	 */
	RttiBuilder() : RttiBuilderBase(typeid(T)){};

	/**
	 * Default constructor, initializes the name of the type described by the
	 * RttiSet with the given name.
	 *
	 * @param name is the initial name of the type described by the type
	 * builder.
	 */
	RttiBuilder(std::string name) : RttiBuilderBase(typeid(T), name){};

	/**
	 * Sets the human readable name of the type information being built to the
	 * given string.
	 *
	 * @param s is the name to which the name should be set.
	 * @return a reference to the current RttiBuilder to allow method chaining.
	 */
	RttiBuilder<T> &name(const std::string &s)
	{
		RttiBuilderBase::name(s);
		return *this;
	}

	/**
	 * Adds the given type descriptor as "parent" of the type information that
	 * is being built by this RttiBuilder instance.
	 *
	 * @param p is the pointer to the type descriptor that should be added.
	 * @return a reference to the current RttiBuilder to allow method chaining.
	 */
	RttiBuilder<T> &parent(const Rtti *p)
	{
		RttiBuilderBase::parent(p);
		return *this;
	}

	/**
	 * Adds the given type descriptors as "parent" of the type information that
	 * is being built by this RttiBuilder instance.
	 *
	 * @param p is the pointer to the type descriptor that should be added.
	 * @return a reference to the current RttiBuilder to allow method chaining.
	 */
	RttiBuilder<T> &parent(const RttiSet &p)
	{
		RttiBuilderBase::parent(p);
		return *this;
	}

	/**
	 * Marks the current type being built by this RttiBuilder instance as being
	 * a composition of the given other type.
	 *
	 * @param p is the pointer to the type descriptor that should be added as
	 * composition type.
	 * @return a reference to the current RttiBuilder to allow method chaining.
	 */
	RttiBuilder<T> &composedOf(const Rtti *p)
	{
		RttiBuilderBase::composedOf(p);
		return *this;
	}

	/**
	 * Marks the current type being built by this RttiBuilder instance as being
	 * a composition of the given other types.
	 *
	 * @param p is the pointer to the type descriptor that should be added as
	 * composition type.
	 * @return a reference to the current RttiBuilder to allow method chaining.
	 */
	RttiBuilder<T> &composedOf(const RttiSet &p)
	{
		RttiBuilderBase::composedOf(p);
		return *this;
	}

	/**
	 * Registers a generic (no particular C++ type given) method for this RTTI
	 * type descriptor.
	 *
	 * @param name is the name of the method. Names must be unique for one
	 * Rtti instance. If the name is not unique, an exception is thrown.
	 * @param function is the function that should be registered.
	 * @return a reference to the current RttiBuilder to allow method chaining.
	 */
	RttiBuilder<T> &genericMethod(const std::string &name,
	                               std::shared_ptr<Function> function)
	{
		RttiBuilderBase::genericMethod(name, function);
		return *this;
	}

	/**
	 * Registers a generic (no particular C++ type given) property descriptor
	 * for this RTTI type descriptor.
	 *
	 * @param name is the name of the property. Names must be unique for one
	 * Rtti instance. If the property is not unique, an exception is thrown.
	 * @param property is the property that should be registered.
	 * @return a reference to the current RttiBuilder to allow method chaining.
	 */
	RttiBuilder<T> &genericProperty(
	    const std::string &name, std::shared_ptr<PropertyDescriptor> property)
	{
		RttiBuilderBase::genericProperty(name, property);
		return *this;
	}

	/**
	 * Registers a method for this RTTI type descriptor.
	 *
	 * @param name is the name of the method. Names must be unique for one
	 * Rtti instance. If the name is not unique, an exception is thrown.
	 * @param method is the function that should be registered.
	 * @return a reference to the current RttiBuilder to allow method chaining.
	 */
	RttiBuilder<T> &method(const std::string name, const Method<T> &method)
	{
		return genericMethod(name, std::make_shared<Method<T>>(method));
	}

	/**
	 * Registers a method for this RTTI type descriptor.
	 *
	 * @param name is the name of the method. Names must be unique for one
	 * Rtti instance. If the name is not unique, an exception is thrown.
	 * @param method is the function that should be registered.
	 * @return a reference to the current RttiBuilder to allow method chaining.
	 */
	RttiBuilder<T> &method(const std::string name,
	                       const typename Method<T>::Callback &method)
	{
		return genericMethod(name, std::make_shared<Method<T>>(method));
	}

	/**
	 * Registers a property for this RTTI type descriptor.
	 *
	 * @param name is the name of the property. Names must be unique for one
	 * Rtti instance. If the property is not unique, an exception is thrown.
	 * @param property is the property that should be registered.
	 * @return a reference to the current RttiBuilder to allow method chaining.
	 */
	RttiBuilder<T> &property(const std::string name,
	                         const Property<T> &property)
	{
		RttiBuilderBase::genericProperty(
		    name, std::make_shared<Property<T>>(property));
		return *this;
	}
};
}

#endif /* _OUSIA_RTTI_BUILDER_HPP_ */