src/core/object-factory.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 3182 61fe7fe81ebd
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
     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 
    23 #include "attribute-list.h"
    24 #include "object.h"
    25 #include "type-id.h"
    26 
    27 namespace ns3 {
    28 
    29 class AttributeValue;
    30 
    31 /**
    32  * \ingroup object
    33  *
    34  * \brief instantiate subclasses of ns3::Object.
    35  *
    36  * This class can also hold a set of attributes to set
    37  * automatically during the object construction.
    38  */
    39 class ObjectFactory
    40 {
    41 public:
    42   ObjectFactory ();
    43 
    44   /**
    45    * \param tid the TypeId of the object to instantiate.
    46    */
    47   void SetTypeId (TypeId tid);
    48   /**
    49    * \param tid the TypeId of the object to instantiate.
    50    */  
    51   void SetTypeId (const char *tid);
    52   /**
    53    * \param tid the TypeId of the object to instantiate.
    54    */  
    55   void SetTypeId (std::string tid);
    56   /**
    57    * \param name the name of the attribute to set during object construction
    58    * \param value the value of the attribute to set during object construction
    59    */
    60   void Set (std::string name, const AttributeValue &value);
    61 
    62   void Set (const AttributeList &list);
    63 
    64   /**
    65    * \returns the currently-selected TypeId to use to create an object
    66    *          instance.
    67    */
    68   TypeId GetTypeId (void) const;
    69 
    70   /**
    71    * \returns a new object instance.
    72    */
    73   Ptr<Object> Create (void) const;
    74   /**
    75    * \returns a new object instance.
    76    *
    77    * This method performs an extra call to ns3::Object::GetObject before
    78    * returning a pointer of the requested type to the user. This method
    79    * is really syntactical sugar.
    80    */
    81   template <typename T>
    82   Ptr<T> Create (void) const;
    83 
    84 private:
    85   friend std::ostream & operator << (std::ostream &os, const ObjectFactory &factory);
    86   friend std::istream & operator >> (std::istream &is, ObjectFactory &factory);
    87 
    88   TypeId m_tid;
    89   AttributeList m_parameters;
    90 };
    91 
    92 std::ostream & operator << (std::ostream &os, const ObjectFactory &factory);
    93 std::istream & operator >> (std::istream &is, ObjectFactory &factory);
    94 
    95 /**
    96  * \class ns3::ObjectFactoryValue
    97  * \brief hold objects of type ns3::ObjectFactory
    98  */
    99 
   100 ATTRIBUTE_HELPER_HEADER (ObjectFactory);
   101 
   102 } // namespace ns3
   103 
   104 namespace ns3 {
   105 
   106 template <typename T>
   107 Ptr<T> 
   108 ObjectFactory::Create (void) const
   109 {
   110   Ptr<Object> object = Create ();
   111   return object->GetObject<T> ();
   112 }
   113 
   114 } // namespace ns3
   115 
   116 #endif /* OBJECT_FACTORY_H */