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
mathieu@2586
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
mathieu@2586
     2
/*
mathieu@2586
     3
 * Copyright (c) 2008 INRIA
mathieu@2586
     4
 *
mathieu@2586
     5
 * This program is free software; you can redistribute it and/or modify
mathieu@2586
     6
 * it under the terms of the GNU General Public License version 2 as
mathieu@2586
     7
 * published by the Free Software Foundation;
mathieu@2586
     8
 *
mathieu@2586
     9
 * This program is distributed in the hope that it will be useful,
mathieu@2586
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mathieu@2586
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
mathieu@2586
    12
 * GNU General Public License for more details.
mathieu@2586
    13
 *
mathieu@2586
    14
 * You should have received a copy of the GNU General Public License
mathieu@2586
    15
 * along with this program; if not, write to the Free Software
mathieu@2586
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
mathieu@2586
    17
 *
mathieu@2586
    18
 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
mathieu@2586
    19
 */
mathieu@2395
    20
#ifndef OBJECT_FACTORY_H
mathieu@2395
    21
#define OBJECT_FACTORY_H
mathieu@2395
    22
mathieu@2965
    23
#include "attribute-list.h"
mathieu@2395
    24
#include "object.h"
mathieu@2965
    25
#include "type-id.h"
mathieu@2395
    26
mathieu@2395
    27
namespace ns3 {
mathieu@2395
    28
mathieu@2965
    29
class AttributeValue;
mathieu@2965
    30
mathieu@2583
    31
/**
tomh@3182
    32
 * \ingroup object
tomh@3182
    33
 *
mathieu@2583
    34
 * \brief instantiate subclasses of ns3::Object.
mathieu@2583
    35
 *
mathieu@2583
    36
 * This class can also hold a set of attributes to set
mathieu@2583
    37
 * automatically during the object construction.
mathieu@2583
    38
 */
mathieu@2395
    39
class ObjectFactory
mathieu@2395
    40
{
mathieu@2395
    41
public:
mathieu@2395
    42
  ObjectFactory ();
mathieu@2395
    43
mathieu@2583
    44
  /**
mathieu@2583
    45
   * \param tid the TypeId of the object to instantiate.
mathieu@2583
    46
   */
mathieu@2395
    47
  void SetTypeId (TypeId tid);
mathieu@2583
    48
  /**
mathieu@2583
    49
   * \param tid the TypeId of the object to instantiate.
mathieu@2583
    50
   */  
mathieu@2583
    51
  void SetTypeId (const char *tid);
mathieu@2583
    52
  /**
mathieu@2583
    53
   * \param tid the TypeId of the object to instantiate.
mathieu@2583
    54
   */  
mathieu@2395
    55
  void SetTypeId (std::string tid);
mathieu@2583
    56
  /**
mathieu@2583
    57
   * \param name the name of the attribute to set during object construction
mathieu@2583
    58
   * \param value the value of the attribute to set during object construction
mathieu@2583
    59
   */
mathieu@2965
    60
  void Set (std::string name, const AttributeValue &value);
mathieu@2395
    61
mathieu@3891
    62
  void Set (const AttributeList &list);
mathieu@3891
    63
mathieu@2583
    64
  /**
mathieu@2583
    65
   * \returns the currently-selected TypeId to use to create an object
mathieu@2583
    66
   *          instance.
mathieu@2583
    67
   */
mathieu@2395
    68
  TypeId GetTypeId (void) const;
mathieu@2395
    69
mathieu@2583
    70
  /**
mathieu@2583
    71
   * \returns a new object instance.
mathieu@2583
    72
   */
mathieu@2395
    73
  Ptr<Object> Create (void) const;
mathieu@2583
    74
  /**
mathieu@2583
    75
   * \returns a new object instance.
mathieu@2583
    76
   *
mathieu@2583
    77
   * This method performs an extra call to ns3::Object::GetObject before
mathieu@2583
    78
   * returning a pointer of the requested type to the user. This method
mathieu@2583
    79
   * is really syntactical sugar.
mathieu@2583
    80
   */
mathieu@2483
    81
  template <typename T>
mathieu@2483
    82
  Ptr<T> Create (void) const;
mathieu@2395
    83
mathieu@2395
    84
private:
mathieu@2952
    85
  friend std::ostream & operator << (std::ostream &os, const ObjectFactory &factory);
mathieu@2952
    86
  friend std::istream & operator >> (std::istream &is, ObjectFactory &factory);
mathieu@2952
    87
mathieu@2395
    88
  TypeId m_tid;
mathieu@2459
    89
  AttributeList m_parameters;
mathieu@2395
    90
};
mathieu@2395
    91
mathieu@2555
    92
std::ostream & operator << (std::ostream &os, const ObjectFactory &factory);
mathieu@2555
    93
std::istream & operator >> (std::istream &is, ObjectFactory &factory);
mathieu@2555
    94
mathieu@2969
    95
/**
mathieu@2969
    96
 * \class ns3::ObjectFactoryValue
mathieu@2969
    97
 * \brief hold objects of type ns3::ObjectFactory
mathieu@2969
    98
 */
mathieu@2969
    99
mathieu@3094
   100
ATTRIBUTE_HELPER_HEADER (ObjectFactory);
mathieu@2555
   101
mathieu@2395
   102
} // namespace ns3
mathieu@2395
   103
mathieu@2483
   104
namespace ns3 {
mathieu@2483
   105
mathieu@2483
   106
template <typename T>
mathieu@2483
   107
Ptr<T> 
mathieu@2483
   108
ObjectFactory::Create (void) const
mathieu@2483
   109
{
Mathieu@2507
   110
  Ptr<Object> object = Create ();
Mathieu@2507
   111
  return object->GetObject<T> ();
mathieu@2483
   112
}
mathieu@2483
   113
mathieu@2483
   114
} // namespace ns3
mathieu@2483
   115
mathieu@2395
   116
#endif /* OBJECT_FACTORY_H */