A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
object-factory.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19#ifndef OBJECT_FACTORY_H
20#define OBJECT_FACTORY_H
21
23#include "object.h"
24#include "type-id.h"
25
26/**
27 * \file
28 * \ingroup object
29 * ns3::ObjectFactory class declaration.
30 */
31
32namespace ns3
33{
34
35class AttributeValue;
36
37/**
38 * \ingroup object
39 *
40 * \brief Instantiate subclasses of ns3::Object.
41 *
42 * This class can also hold a set of attributes to set
43 * automatically during the object construction.
44 *
45 * \see attribute_ObjectFactory
46 */
48{
49 public:
50 /**
51 * Default constructor.
52 *
53 * This factory is not capable of constructing a real Object
54 * until it has at least a TypeId.
55 */
57 /**
58 * Construct a factory for a specific TypeId by name.
59 *
60 * \tparam Args \deduced Template type parameter pack for the sequence of name-value pairs
61 * \param [in] typeId The name of the TypeId this factory should create.
62 * \param [in] args A sequence of name-value pairs of additional attributes to set.
63 *
64 * The args sequence can be made of any number of pairs, each consisting of a
65 * name (of std::string type) followed by a value (of const AttributeValue & type).
66 */
67 template <typename... Args>
68 ObjectFactory(const std::string& typeId, Args&&... args);
69
70 /**@{*/
71 /**
72 * Set the TypeId of the Objects to be created by this factory.
73 *
74 * \param [in] tid The TypeId of the object to instantiate.
75 */
76 void SetTypeId(TypeId tid);
77 void SetTypeId(std::string tid);
78 /**@}*/
79
80 /**
81 * Check if the ObjectFactory has been configured with a TypeId
82 *
83 * \return true if a TypeId has been configured to the ObjectFactory
84 */
85 bool IsTypeIdSet() const;
86
87 /**
88 * Set an attribute to be set during construction.
89 *
90 * \tparam Args \deduced Template type parameter pack for the sequence of name-value pairs
91 * \param [in] name The name of the attribute to set.
92 * \param [in] value The value of the attribute to set.
93 * \param [in] args A sequence of name-value pairs of additional attributes to set.
94 *
95 * The args sequence can be made of any number of pairs, each consisting of a
96 * name (of std::string type) followed by a value (of const AttributeValue & type).
97 */
98 template <typename... Args>
99 void Set(const std::string& name, const AttributeValue& value, Args&&... args);
100
101 /**
102 * Base case to stop the recursion performed by the templated version of this
103 * method.
104 */
105 void Set()
106 {
107 }
108
109 /**
110 * Get the TypeId which will be created by this ObjectFactory.
111 * \returns The currently-selected TypeId.
112 */
113 TypeId GetTypeId() const;
114
115 /**
116 * Create an Object instance of the configured TypeId.
117 *
118 * \returns A new object instance.
119 */
120 Ptr<Object> Create() const;
121 /**
122 * Create an Object instance of the requested type.
123 *
124 * This method performs an extra call to ns3::Object::GetObject before
125 * returning a pointer of the requested type to the user. This method
126 * is really syntactical sugar.
127 *
128 * \tparam T \explicit The requested Object type.
129 * \returns A new object instance.
130 */
131 template <typename T>
132 Ptr<T> Create() const;
133
134 private:
135 /**
136 * Set an attribute to be set during construction.
137 *
138 * \param [in] name The name of the attribute to set.
139 * \param [in] value The value of the attribute to set.
140 */
141 void DoSet(const std::string& name, const AttributeValue& value);
142 /**
143 * Print the factory configuration on an output stream.
144 *
145 * The configuration will be printed as a string with the form
146 * "<TypeId-name>[<attribute-name>=<attribute-value>|...]"
147 *
148 * \param [in,out] os The stream.
149 * \param [in] factory The ObjectFactory.
150 * \returns The stream.
151 */
152 friend std::ostream& operator<<(std::ostream& os, const ObjectFactory& factory);
153 /**
154 * Read a factory configuration from an input stream.
155 *
156 * The configuration should be in the form
157 * "<TypeId-name>[<attribute-name>=<attribute-value>|...]"
158 *
159 * \param [in,out] is The input stream.
160 * \param [out] factory The factory to configure as described by the stream.
161 * \return The stream.
162 */
163 friend std::istream& operator>>(std::istream& is, ObjectFactory& factory);
164
165 /** The TypeId this factory will create. */
167 /**
168 * The list of attributes and values to be used in constructing
169 * objects by this factory.
170 */
172};
173
174std::ostream& operator<<(std::ostream& os, const ObjectFactory& factory);
175std::istream& operator>>(std::istream& is, ObjectFactory& factory);
176
177/**
178 * \ingroup object
179 * Allocate an Object on the heap and initialize with a set of attributes.
180 *
181 * \tparam T \explicit The requested Object type.
182 * \tparam Args \deduced The type of the sequence of name-value pairs.
183 * \param [in] args A sequence of name-value pairs of the attributes to set.
184 * \returns A pointer to a newly allocated object.
185 *
186 * The args sequence can be made of any number of pairs, each consisting of a
187 * name (of std::string type) followed by a value (of const AttributeValue & type).
188 */
189template <typename T, typename... Args>
191
193
194} // namespace ns3
195
196/***************************************************************
197 * Implementation of the templates declared above.
198 ***************************************************************/
199
200namespace ns3
201{
202
203template <typename T>
204Ptr<T>
206{
207 Ptr<Object> object = Create();
208 auto obj = object->GetObject<T>();
209 NS_ASSERT_MSG(obj != nullptr,
210 "ObjectFactory::Create error: incompatible types ("
211 << T::GetTypeId().GetName() << " and " << object->GetInstanceTypeId() << ")");
212 return obj;
213}
214
215template <typename... Args>
216ObjectFactory::ObjectFactory(const std::string& typeId, Args&&... args)
217{
218 SetTypeId(typeId);
219 Set(args...);
220}
221
222template <typename... Args>
223void
224ObjectFactory::Set(const std::string& name, const AttributeValue& value, Args&&... args)
225{
226 DoSet(name, value);
227 Set(args...);
228}
229
230template <typename T, typename... Args>
231Ptr<T>
233{
234 ObjectFactory factory;
235 factory.SetTypeId(T::GetTypeId());
236 factory.Set(args...);
237 return factory.Create<T>();
238}
239
240} // namespace ns3
241
242#endif /* OBJECT_FACTORY_H */
ns3::AttributeConstructionList declaration.
List of Attribute name, value and checker triples used to construct Objects.
Hold a value for an Attribute.
Definition: attribute.h:70
Instantiate subclasses of ns3::Object.
void Set()
Base case to stop the recursion performed by the templated version of this method.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
friend std::ostream & operator<<(std::ostream &os, const ObjectFactory &factory)
Print the factory configuration on an output stream.
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
TypeId GetTypeId() const
Get the TypeId which will be created by this ObjectFactory.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
friend std::istream & operator>>(std::istream &is, ObjectFactory &factory)
Read a factory configuration from an input stream.
TypeId m_tid
The TypeId this factory will create.
ObjectFactory()
Default constructor.
void DoSet(const std::string &name, const AttributeValue &value)
Set an attribute to be set during construction.
AttributeConstructionList m_parameters
The list of attributes and values to be used in constructing objects by this factory.
bool IsTypeIdSet() const
Check if the ObjectFactory has been configured with a TypeId.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
a unique identifier for an interface.
Definition: type-id.h:59
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
#define ATTRIBUTE_HELPER_HEADER(type)
Declare the attribute value, accessor and checkers for class type
Ptr< T > CreateObjectWithAttributes(Args... args)
Allocate an Object on the heap and initialize with a set of attributes.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:183
ns3::Object class declaration, which is the root of the Object hierarchy and Aggregation.
ns3::TypeId declaration; inline and template implementations.