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