00001 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2008 INRIA 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation; 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 * 00018 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> 00019 */ 00020 #ifndef OBJECT_FACTORY_H 00021 #define OBJECT_FACTORY_H 00022 00023 #include "attribute-list.h" 00024 #include "object.h" 00025 #include "type-id.h" 00026 00027 namespace ns3 { 00028 00029 class AttributeValue; 00030 00031 /** 00032 * \ingroup object 00033 * 00034 * \brief instantiate subclasses of ns3::Object. 00035 * 00036 * This class can also hold a set of attributes to set 00037 * automatically during the object construction. 00038 */ 00039 class ObjectFactory 00040 { 00041 public: 00042 ObjectFactory (); 00043 00044 /** 00045 * \param tid the TypeId of the object to instantiate. 00046 */ 00047 void SetTypeId (TypeId tid); 00048 /** 00049 * \param tid the TypeId of the object to instantiate. 00050 */ 00051 void SetTypeId (const char *tid); 00052 /** 00053 * \param tid the TypeId of the object to instantiate. 00054 */ 00055 void SetTypeId (std::string tid); 00056 /** 00057 * \param name the name of the attribute to set during object construction 00058 * \param value the value of the attribute to set during object construction 00059 */ 00060 void Set (std::string name, const AttributeValue &value); 00061 00062 void Set (const AttributeList &list); 00063 00064 /** 00065 * \returns the currently-selected TypeId to use to create an object 00066 * instance. 00067 */ 00068 TypeId GetTypeId (void) const; 00069 00070 /** 00071 * \returns a new object instance. 00072 */ 00073 Ptr<Object> Create (void) const; 00074 /** 00075 * \returns a new object instance. 00076 * 00077 * This method performs an extra call to ns3::Object::GetObject before 00078 * returning a pointer of the requested type to the user. This method 00079 * is really syntactical sugar. 00080 */ 00081 template <typename T> 00082 Ptr<T> Create (void) const; 00083 00084 private: 00085 friend std::ostream & operator << (std::ostream &os, const ObjectFactory &factory); 00086 friend std::istream & operator >> (std::istream &is, ObjectFactory &factory); 00087 00088 TypeId m_tid; 00089 AttributeList m_parameters; 00090 }; 00091 00092 std::ostream & operator << (std::ostream &os, const ObjectFactory &factory); 00093 std::istream & operator >> (std::istream &is, ObjectFactory &factory); 00094 00095 /** 00096 * \class ns3::ObjectFactoryValue 00097 * \brief hold objects of type ns3::ObjectFactory 00098 */ 00099 00100 ATTRIBUTE_HELPER_HEADER (ObjectFactory); 00101 00102 } // namespace ns3 00103 00104 namespace ns3 { 00105 00106 template <typename T> 00107 Ptr<T> 00108 ObjectFactory::Create (void) const 00109 { 00110 Ptr<Object> object = Create (); 00111 return object->GetObject<T> (); 00112 } 00113 00114 } // namespace ns3 00115 00116 #endif /* OBJECT_FACTORY_H */