src/core/object-factory.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 2965 4b28e9740e3b
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
#include "object-factory.h"
mathieu@2395
    21
#include <sstream>
mathieu@2395
    22
mathieu@2395
    23
namespace ns3 {
mathieu@2395
    24
mathieu@2395
    25
ObjectFactory::ObjectFactory ()
mathieu@2395
    26
{}
mathieu@2395
    27
mathieu@2395
    28
void 
mathieu@2395
    29
ObjectFactory::SetTypeId (TypeId tid)
mathieu@2395
    30
{
mathieu@2395
    31
  m_tid = tid;
mathieu@2395
    32
}
mathieu@2395
    33
void 
mathieu@2395
    34
ObjectFactory::SetTypeId (std::string tid)
mathieu@2395
    35
{
mathieu@2395
    36
  m_tid = TypeId::LookupByName (tid);
mathieu@2395
    37
}
mathieu@2395
    38
void 
mathieu@2488
    39
ObjectFactory::SetTypeId (const char *tid)
mathieu@2488
    40
{
mathieu@2488
    41
  m_tid = TypeId::LookupByName (tid);
mathieu@2488
    42
}
mathieu@2488
    43
void 
mathieu@2965
    44
ObjectFactory::Set (std::string name, const AttributeValue &value)
mathieu@2395
    45
{
mathieu@2522
    46
  if (name == "")
mathieu@2522
    47
    {
mathieu@2522
    48
      return;
mathieu@2522
    49
    }
mathieu@2583
    50
  m_parameters.SetWithTid (m_tid, name, value);
mathieu@2395
    51
}
mathieu@2395
    52
mathieu@3891
    53
void 
mathieu@3891
    54
ObjectFactory::Set (const AttributeList &list)
mathieu@3891
    55
{
mathieu@3891
    56
  m_parameters = list;
mathieu@3891
    57
}
mathieu@3891
    58
mathieu@2395
    59
TypeId 
mathieu@2395
    60
ObjectFactory::GetTypeId (void) const
mathieu@2395
    61
{
mathieu@2395
    62
  return m_tid;
mathieu@2395
    63
}
mathieu@2395
    64
mathieu@2395
    65
Ptr<Object> 
mathieu@2395
    66
ObjectFactory::Create (void) const
mathieu@2395
    67
{
mathieu@2631
    68
  Callback<ObjectBase *> cb = m_tid.GetConstructor ();
mathieu@2631
    69
  ObjectBase *base = cb ();
mathieu@2631
    70
  Object *derived = dynamic_cast<Object *> (base);
mathieu@2631
    71
  derived->SetTypeId (m_tid);
mathieu@2631
    72
  derived->Construct (m_parameters);
mathieu@2631
    73
  Ptr<Object> object = Ptr<Object> (derived, false);
mathieu@2395
    74
  return object;
mathieu@2395
    75
}
mathieu@2395
    76
mathieu@2555
    77
std::ostream & operator << (std::ostream &os, const ObjectFactory &factory)
mathieu@2555
    78
{
mathieu@2952
    79
  os << factory.m_tid.GetName () << "[" << factory.m_parameters.SerializeToString () << "]";
mathieu@2555
    80
  return os;
mathieu@2555
    81
}
mathieu@2555
    82
std::istream & operator >> (std::istream &is, ObjectFactory &factory)
mathieu@2555
    83
{
mathieu@2956
    84
  std::string v;
mathieu@2956
    85
  is >> v;
mathieu@2956
    86
  std::string::size_type lbracket, rbracket;
mathieu@2956
    87
  lbracket = v.find ("[");
mathieu@2956
    88
  rbracket = v.find ("]");
mathieu@2956
    89
  NS_ASSERT (lbracket != std::string::npos);
mathieu@2956
    90
  NS_ASSERT (rbracket != std::string::npos);
mathieu@2956
    91
  std::string tid = v.substr (0, lbracket);
mathieu@2956
    92
  std::string parameters = v.substr (lbracket+1,rbracket-(lbracket+1));
mathieu@2956
    93
  factory.SetTypeId (tid);
mathieu@2956
    94
  factory.m_parameters.DeserializeFromString (parameters);
mathieu@2555
    95
  return is;
mathieu@2555
    96
}
mathieu@2555
    97
mathieu@2555
    98
mathieu@2582
    99
ATTRIBUTE_HELPER_CPP (ObjectFactory);
mathieu@2555
   100
mathieu@2395
   101
} // namespace ns3