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 ();
62  ObjectFactory (std::string typeId);
63 
70  void SetTypeId (TypeId tid);
71  void SetTypeId (const char *tid);
72  void SetTypeId (std::string tid);
81  void Set (std::string name, const AttributeValue &value);
82 
87  TypeId GetTypeId (void) const;
88 
94  Ptr<Object> Create (void) const;
105  template <typename T>
106  Ptr<T> Create (void) const;
107 
108 private:
119  friend std::ostream & operator << (std::ostream &os, const ObjectFactory &factory);
130  friend std::istream & operator >> (std::istream &is, ObjectFactory &factory);
131 
139 };
140 
141 std::ostream & operator << (std::ostream &os, const ObjectFactory &factory);
142 std::istream & operator >> (std::istream &is, ObjectFactory &factory);
143 
144 
170 template <typename T>
171 Ptr<T>
173  (std::string n1 = "", const AttributeValue & v1 = EmptyAttributeValue (),
174  std::string n2 = "", const AttributeValue & v2 = EmptyAttributeValue (),
175  std::string n3 = "", const AttributeValue & v3 = EmptyAttributeValue (),
176  std::string n4 = "", const AttributeValue & v4 = EmptyAttributeValue (),
177  std::string n5 = "", const AttributeValue & v5 = EmptyAttributeValue (),
178  std::string n6 = "", const AttributeValue & v6 = EmptyAttributeValue (),
179  std::string n7 = "", const AttributeValue & v7 = EmptyAttributeValue (),
180  std::string n8 = "", const AttributeValue & v8 = EmptyAttributeValue (),
181  std::string n9 = "", const AttributeValue & v9 = EmptyAttributeValue ()
182  );
183 
184 
186 
187 } // namespace ns3
188 
189 
190 /***************************************************************
191  * Implementation of the templates declared above.
192  ***************************************************************/
193 
194 namespace ns3 {
195 
196 template <typename T>
197 Ptr<T>
199 {
200  Ptr<Object> object = Create ();
201  return object->GetObject<T> ();
202 }
203 
204 template <typename T>
205 Ptr<T>
206 CreateObjectWithAttributes (std::string n1, const AttributeValue & v1,
207  std::string n2, const AttributeValue & v2,
208  std::string n3, const AttributeValue & v3,
209  std::string n4, const AttributeValue & v4,
210  std::string n5, const AttributeValue & v5,
211  std::string n6, const AttributeValue & v6,
212  std::string n7, const AttributeValue & v7,
213  std::string n8, const AttributeValue & v8,
214  std::string n9, const AttributeValue & v9)
215 {
216  ObjectFactory factory;
217  factory.SetTypeId (T::GetTypeId ());
218  factory.Set(n1, v1);
219  factory.Set(n2, v2);
220  factory.Set(n3, v3);
221  factory.Set(n4, v4);
222  factory.Set(n5, v5);
223  factory.Set(n6, v6);
224  factory.Set(n7, v7);
225  factory.Set(n8, v8);
226  factory.Set(n9, v9);
227  return factory.Create<T> ();
228 }
229 
230 
231 } // namespace ns3
232 
233 #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 SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
ObjectFactory()
Default constructor.
ns3::AttributeConstructionList declaration.
Ptr< T > CreateObjectWithAttributes(std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue())
Allocate an Object on the heap and initialize with a set of attributes.
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
A class for an empty attribute value.
Definition: attribute.h:232
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
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::TypeId declaration; inline and template implementations.
void Set(std::string name, const AttributeValue &value)
Set an attribute to be set during construction.
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.
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.