A Discrete-Event Network Simulator
API
object-factory.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
19 #ifndef OBJECT_FACTORY_H
20 #define OBJECT_FACTORY_H
21 
23 #include "object.h"
24 #include "type-id.h"
25 
32 namespace ns3
33 {
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(std::string tid);
85  bool IsTypeIdSet() const;
86 
98  template <typename... Args>
99  void Set(const std::string& name, const AttributeValue& value, Args&&... args);
100 
105  void Set()
106  {
107  }
108 
113  TypeId GetTypeId() const;
114 
120  Ptr<Object> Create() const;
131  template <typename T>
132  Ptr<T> Create() const;
133 
134  private:
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 
174 std::ostream& operator<<(std::ostream& os, const ObjectFactory& factory);
175 std::istream& operator>>(std::istream& is, ObjectFactory& factory);
176 
189 template <typename T, typename... Args>
191 
193 
194 } // namespace ns3
195 
196 /***************************************************************
197  * Implementation of the templates declared above.
198  ***************************************************************/
199 
200 namespace ns3
201 {
202 
203 template <typename T>
204 Ptr<T>
206 {
207  Ptr<Object> object = Create();
208  return object->GetObject<T>();
209 }
210 
211 template <typename... Args>
212 ObjectFactory::ObjectFactory(const std::string& typeId, Args&&... args)
213 {
214  SetTypeId(typeId);
215  Set(args...);
216 }
217 
218 template <typename... Args>
219 void
220 ObjectFactory::Set(const std::string& name, const AttributeValue& value, Args&&... args)
221 {
222  DoSet(name, value);
223  Set(args...);
224 }
225 
226 template <typename T, typename... Args>
227 Ptr<T>
229 {
230  ObjectFactory factory;
231  factory.SetTypeId(T::GetTypeId());
232  factory.Set(args...);
233  return factory.Create<T>();
234 }
235 
236 } // namespace ns3
237 
238 #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:70
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.
friend std::istream & operator>>(std::istream &is, ObjectFactory &factory)
Read a factory configuration from an input stream.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
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.
a unique identifier for an interface.
Definition: type-id.h:60
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::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:153
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:129
value
Definition: second.py:41
ns3::Object class declaration, which is the root of the Object hierarchy and Aggregation.
ns3::TypeId declaration; inline and template implementations.