A Discrete-Event Network Simulator
API
object-factory.h
Go to the documentation of this file.
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
24#include "object.h"
25#include "type-id.h"
26
33namespace ns3 {
34
35class AttributeValue;
36
48{
49public:
67 template <typename... Args>
68 ObjectFactory (const std::string& typeId, Args&&... args);
69
76 void SetTypeId (TypeId tid);
77 void SetTypeId (const char *tid);
78 void SetTypeId (std::string tid);
86 bool IsTypeIdSet (void) const;
87
99 template <typename... Args>
100 void Set (const std::string &name, const AttributeValue &value, Args&&... args);
101
106 void Set (void)
107 { }
108
113 TypeId GetTypeId (void) const;
114
120 Ptr<Object> Create (void) const;
131 template <typename T>
132 Ptr<T> Create (void) const;
133
134private:
141 void DoSet (const std::string &name, const AttributeValue &value);
152 friend std::ostream & operator << (std::ostream &os, const ObjectFactory &factory);
163 friend std::istream & operator >> (std::istream &is, ObjectFactory &factory);
164
172};
173
174std::ostream & operator << (std::ostream &os, const ObjectFactory &factory);
175std::istream & operator >> (std::istream &is, ObjectFactory &factory);
176
177
190template <typename T, typename... Args>
191Ptr<T>
192CreateObjectWithAttributes (Args... args);
193
194
196
197} // namespace ns3
198
199
200/***************************************************************
201 * Implementation of the templates declared above.
202 ***************************************************************/
203
204namespace ns3 {
205
206template <typename T>
207Ptr<T>
209{
210 Ptr<Object> object = Create ();
211 return object->GetObject<T> ();
212}
213
214template <typename... Args>
215ObjectFactory::ObjectFactory (const std::string& typeId, Args&&... args)
216{
217 SetTypeId (typeId);
218 Set (args...);
219}
220
221template <typename... Args>
222void
223ObjectFactory::Set (const std::string &name, const AttributeValue & value, Args&&... args)
224{
225 DoSet (name, value);
226 Set (args...);
227}
228
229template <typename T, typename... Args>
230Ptr<T>
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 */
ns3::AttributeConstructionList declaration.
List of Attribute name, value and checker triples used to construct Objects.
Hold a value for an Attribute.
Definition: attribute.h:69
Instantiate subclasses of ns3::Object.
void Set(void)
Base case to stop the recursion performed by the templated version of this method.
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(void) const
Get the TypeId which will be created by this ObjectFactory.
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
bool IsTypeIdSet(void) const
Check if the ObjectFactory has been configured with a TypeId.
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.
a unique identifier for an interface.
Definition: type-id.h:59
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.
ATTRIBUTE_HELPER_HEADER(ValueClassTest)
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:139
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:162
ns3::Object class declaration, which is the root of the Object hierarchy and Aggregation.
ns3::TypeId declaration; inline and template implementations.