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 
33 namespace ns3 {
34 
35 class AttributeValue;
36 
48 {
49 public:
56  ObjectFactory ();
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 
112  TypeId GetTypeId (void) const;
113 
119  Ptr<Object> Create (void) const;
130  template <typename T>
131  Ptr<T> Create (void) const;
132 
133 private:
140  void DoSet (const std::string &name, const AttributeValue &value);
151  friend std::ostream & operator << (std::ostream &os, const ObjectFactory &factory);
162  friend std::istream & operator >> (std::istream &is, ObjectFactory &factory);
163 
171 };
172 
173 std::ostream & operator << (std::ostream &os, const ObjectFactory &factory);
174 std::istream & operator >> (std::istream &is, ObjectFactory &factory);
175 
176 
189 template <typename T, typename... Args>
190 Ptr<T>
191 CreateObjectWithAttributes (Args... args);
192 
193 
195 
196 } // namespace ns3
197 
198 
199 /***************************************************************
200  * Implementation of the templates declared above.
201  ***************************************************************/
202 
203 namespace ns3 {
204 
205 template <typename T>
206 Ptr<T>
208 {
209  Ptr<Object> object = Create ();
210  return object->GetObject<T> ();
211 }
212 
213 template <typename... Args>
214 ObjectFactory::ObjectFactory (const std::string& typeId, Args&&... args)
215 {
216  SetTypeId (typeId);
217  Set (args...);
218 }
219 
220 template <typename... Args>
221 void
222 ObjectFactory::Set (const std::string &name, const AttributeValue & value, Args&&... args)
223 {
224  DoSet (name, value);
225  Set (args...);
226 }
227 
228 template <typename T, typename... Args>
229 Ptr<T>
231 {
232  ObjectFactory factory;
233  factory.SetTypeId (T::GetTypeId ());
234  factory.Set (args...);
235  return factory.Create<T> ();
236 }
237 
238 
239 } // namespace ns3
240 
241 #endif /* OBJECT_FACTORY_H */
std::istream & operator>>(std::istream &is, Angles &a)
initialize a struct Angles from input
Definition: angles.cc:48
AttributeConstructionList m_parameters
The list of attributes and values to be used in constructing objects by this factory.
Hold a value for an Attribute.
Definition: attribute.h:68
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
ObjectFactory()
Default constructor.
ns3::AttributeConstructionList declaration.
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
List of Attribute name, value and checker triples used to construct Objects.
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
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.
ns3::TypeId declaration; inline and template implementations.
bool IsTypeIdSet(void) const
Check if the ObjectFactory has been configured with a TypeId.
TypeId GetTypeId(void) const
Get the TypeId which will be created by this ObjectFactory.
ns3::Object class declaration, which is the root of the Object hierarchy and Aggregation.
friend std::istream & operator>>(std::istream &is, ObjectFactory &factory)
Read a factory configuration from an input stream.
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.
TypeId m_tid
The TypeId this factory will create.
a unique identifier for an interface.
Definition: type-id.h:58
#define ATTRIBUTE_HELPER_HEADER(type)
Declare the attribute value, accessor and checkers for class type
void DoSet(const std::string &name, const AttributeValue &value)
Set an attribute to be set during construction.