1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3 * Copyright (c) 2008 INRIA
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19 */
20 #ifndef OBJECT_FACTORY_H
21 #define OBJECT_FACTORY_H
22
23 #include "attribute-construction-list.h"
24 #include "object.h"
25 #include "type-id.h"
26
27 /**
28 * \file
29 * \ingroup object
30 * ns3::ObjectFactory class declaration.
31 */
32
33 namespace ns3 {
34
35 class 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 */
47 class ObjectFactory
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 */
56 ObjectFactory ();
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 (const char *tid);
78 void SetTypeId (std::string tid);
79 /**@}*/
80
81 /**
82 * Check if the ObjectFactory has been configured with a TypeId
83 *
84 * \return true if a TypeId has been configured to the ObjectFactory
85 */
86 bool IsTypeIdSet (void) const;
87
88 /**
89 * Set an attribute to be set during construction.
90 *
91 * \tparam Args \deduced Template type parameter pack for the sequence of name-value pairs
92 * \param [in] name The name of the attribute to set.
93 * \param [in] value The value of the attribute to set.
94 * \param [in] args A sequence of name-value pairs of additional attributes to set.
95 *
96 * The args sequence can be made of any number of pairs, each consisting of a
97 * name (of std::string type) followed by a value (of const AttributeValue & type).
98 */
99 template <typename... Args>
100 void Set (const std::string &name, const AttributeValue &value, Args&&... args);
101
102 /**
103 * Base case to stop the recursion performed by the templated version of this
104 * method.
105 */
Set(void)106 void Set (void)
107 { }
108
109 /**
110 * Get the TypeId which will be created by this ObjectFactory.
111 * \returns The currently-selected TypeId.
112 */
113 TypeId GetTypeId (void) const;
114
115 /**
116 * Create an Object instance of the configured TypeId.
117 *
118 * \returns A new object instance.
119 */
120 Ptr<Object> Create (void) 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 (void) 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. */
166 TypeId m_tid;
167 /**
168 * The list of attributes and values to be used in constructing
169 * objects by this factory.
170 */
171 AttributeConstructionList m_parameters;
172 };
173
174 std::ostream & operator << (std::ostream &os, const ObjectFactory &factory);
175 std::istream & operator >> (std::istream &is, ObjectFactory &factory);
176
177
178 /**
179 * \ingroup object
180 * Allocate an Object on the heap and initialize with a set of attributes.
181 *
182 * \tparam T \explicit The requested Object type.
183 * \tparam Args \deduced The type of the sequence of name-value pairs.
184 * \param [in] args A sequence of name-value pairs of the attributes to set.
185 * \returns A pointer to a newly allocated object.
186 *
187 * The args sequence can be made of any number of pairs, each consisting of a
188 * name (of std::string type) followed by a value (of const AttributeValue & type).
189 */
190 template <typename T, typename... Args>
191 Ptr<T>
192 CreateObjectWithAttributes (Args... args);
193
194
195 ATTRIBUTE_HELPER_HEADER (ObjectFactory);
196
197 } // namespace ns3
198
199
200 /***************************************************************
201 * Implementation of the templates declared above.
202 ***************************************************************/
203
204 namespace ns3 {
205
206 template <typename T>
207 Ptr<T>
Create(void)208 ObjectFactory::Create (void) const
209 {
210 Ptr<Object> object = Create ();
211 return object->GetObject<T> ();
212 }
213
214 template <typename... Args>
ObjectFactory(const std::string & typeId,Args &&...args)215 ObjectFactory::ObjectFactory (const std::string& typeId, Args&&... args)
216 {
217 SetTypeId (typeId);
218 Set (args...);
219 }
220
221 template <typename... Args>
222 void
Set(const std::string & name,const AttributeValue & value,Args &&...args)223 ObjectFactory::Set (const std::string &name, const AttributeValue & value, Args&&... args)
224 {
225 DoSet (name, value);
226 Set (args...);
227 }
228
229 template <typename T, typename... Args>
230 Ptr<T>
CreateObjectWithAttributes(Args...args)231 CreateObjectWithAttributes (Args... args)
232 {
233 ObjectFactory factory;
234 factory.SetTypeId (T::GetTypeId ());
235 factory.Set (args...);
236 return factory.Create<T> ();
237 }
238
239
240 } // namespace ns3
241
242 #endif /* OBJECT_FACTORY_H */
243