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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8#ifndef OBJECT_FACTORY_H
9#define OBJECT_FACTORY_H
10
12#include "object.h"
13#include "type-id.h"
14
15/**
16 * \file
17 * \ingroup object
18 * ns3::ObjectFactory class declaration.
19 */
20
21namespace ns3
22{
23
24class AttributeValue;
25
26/**
27 * \ingroup object
28 *
29 * \brief Instantiate subclasses of ns3::Object.
30 *
31 * This class can also hold a set of attributes to set
32 * automatically during the object construction.
33 *
34 * \see attribute_ObjectFactory
35 */
37{
38 public:
39 /**
40 * Default constructor.
41 *
42 * This factory is not capable of constructing a real Object
43 * until it has at least a TypeId.
44 */
46 /**
47 * Construct a factory for a specific TypeId by name.
48 *
49 * \tparam Args \deduced Template type parameter pack for the sequence of name-value pairs
50 * \param [in] typeId The name of the TypeId this factory should create.
51 * \param [in] args A sequence of name-value pairs of additional attributes to set.
52 *
53 * The args sequence can be made of any number of pairs, each consisting of a
54 * name (of std::string type) followed by a value (of const AttributeValue & type).
55 */
56 template <typename... Args>
57 ObjectFactory(const std::string& typeId, Args&&... args);
58
59 /**@{*/
60 /**
61 * Set the TypeId of the Objects to be created by this factory.
62 *
63 * \param [in] tid The TypeId of the object to instantiate.
64 */
65 void SetTypeId(TypeId tid);
66 void SetTypeId(std::string tid);
67 /**@}*/
68
69 /**
70 * Check if the ObjectFactory has been configured with a TypeId
71 *
72 * \return true if a TypeId has been configured to the ObjectFactory
73 */
74 bool IsTypeIdSet() const;
75
76 /**
77 * Set an attribute to be set during construction.
78 *
79 * \tparam Args \deduced Template type parameter pack for the sequence of name-value pairs
80 * \param [in] name The name of the attribute to set.
81 * \param [in] value The value of the attribute to set.
82 * \param [in] args A sequence of name-value pairs of additional attributes to set.
83 *
84 * The args sequence can be made of any number of pairs, each consisting of a
85 * name (of std::string type) followed by a value (of const AttributeValue & type).
86 */
87 template <typename... Args>
88 void Set(const std::string& name, const AttributeValue& value, Args&&... args);
89
90 /**
91 * Base case to stop the recursion performed by the templated version of this
92 * method.
93 */
94 void Set()
95 {
96 }
97
98 /**
99 * Get the TypeId which will be created by this ObjectFactory.
100 * \returns The currently-selected TypeId.
101 */
102 TypeId GetTypeId() const;
103
104 /**
105 * Create an Object instance of the configured TypeId.
106 *
107 * \returns A new object instance.
108 */
109 Ptr<Object> Create() const;
110 /**
111 * Create an Object instance of the requested type.
112 *
113 * This method performs an extra call to ns3::Object::GetObject before
114 * returning a pointer of the requested type to the user. This method
115 * is really syntactical sugar.
116 *
117 * \tparam T \explicit The requested Object type.
118 * \returns A new object instance.
119 */
120 template <typename T>
121 Ptr<T> Create() const;
122
123 private:
124 /**
125 * Set an attribute to be set during construction.
126 *
127 * \param [in] name The name of the attribute to set.
128 * \param [in] value The value of the attribute to set.
129 */
130 void DoSet(const std::string& name, const AttributeValue& value);
131 /**
132 * Print the factory configuration on an output stream.
133 *
134 * The configuration will be printed as a string with the form
135 * "<TypeId-name>[<attribute-name>=<attribute-value>|...]"
136 *
137 * \param [in,out] os The stream.
138 * \param [in] factory The ObjectFactory.
139 * \returns The stream.
140 */
141 friend std::ostream& operator<<(std::ostream& os, const ObjectFactory& factory);
142 /**
143 * Read a factory configuration from an input stream.
144 *
145 * The configuration should be in the form
146 * "<TypeId-name>[<attribute-name>=<attribute-value>|...]"
147 *
148 * \param [in,out] is The input stream.
149 * \param [out] factory The factory to configure as described by the stream.
150 * \return The stream.
151 */
152 friend std::istream& operator>>(std::istream& is, ObjectFactory& factory);
153
154 /** The TypeId this factory will create. */
156 /**
157 * The list of attributes and values to be used in constructing
158 * objects by this factory.
159 */
161};
162
163std::ostream& operator<<(std::ostream& os, const ObjectFactory& factory);
164std::istream& operator>>(std::istream& is, ObjectFactory& factory);
165
166/**
167 * \ingroup object
168 * Allocate an Object on the heap and initialize with a set of attributes.
169 *
170 * \tparam T \explicit The requested Object type.
171 * \tparam Args \deduced The type of the sequence of name-value pairs.
172 * \param [in] args A sequence of name-value pairs of the attributes to set.
173 * \returns A pointer to a newly allocated object.
174 *
175 * The args sequence can be made of any number of pairs, each consisting of a
176 * name (of std::string type) followed by a value (of const AttributeValue & type).
177 */
178template <typename T, typename... Args>
180
182
183} // namespace ns3
184
185/***************************************************************
186 * Implementation of the templates declared above.
187 ***************************************************************/
188
189namespace ns3
190{
191
192template <typename T>
193Ptr<T>
195{
196 Ptr<Object> object = Create();
197 auto obj = object->GetObject<T>();
198 NS_ASSERT_MSG(obj != nullptr,
199 "ObjectFactory::Create error: incompatible types ("
200 << T::GetTypeId().GetName() << " and " << object->GetInstanceTypeId() << ")");
201 return obj;
202}
203
204template <typename... Args>
205ObjectFactory::ObjectFactory(const std::string& typeId, Args&&... args)
206{
207 SetTypeId(typeId);
208 Set(args...);
209}
210
211template <typename... Args>
212void
213ObjectFactory::Set(const std::string& name, const AttributeValue& value, Args&&... args)
214{
215 DoSet(name, value);
216 Set(args...);
217}
218
219template <typename T, typename... Args>
220Ptr<T>
222{
223 ObjectFactory factory;
224 factory.SetTypeId(T::GetTypeId());
225 factory.Set(args...);
226 return factory.Create<T>();
227}
228
229} // namespace ns3
230
231#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:59
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.
a unique identifier for an interface.
Definition type-id.h:48
#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:75
#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:148
std::istream & operator>>(std::istream &is, Angles &a)
Definition angles.cc:172
ns3::Object class declaration, which is the root of the Object hierarchy and Aggregation.
ns3::TypeId declaration; inline and template implementations.