mathieu@2586: /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ mathieu@2586: /* mathieu@2586: * Copyright (c) 2008 INRIA mathieu@2586: * mathieu@2586: * This program is free software; you can redistribute it and/or modify mathieu@2586: * it under the terms of the GNU General Public License version 2 as mathieu@2586: * published by the Free Software Foundation; mathieu@2586: * mathieu@2586: * This program is distributed in the hope that it will be useful, mathieu@2586: * but WITHOUT ANY WARRANTY; without even the implied warranty of mathieu@2586: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the mathieu@2586: * GNU General Public License for more details. mathieu@2586: * mathieu@2586: * You should have received a copy of the GNU General Public License mathieu@2586: * along with this program; if not, write to the Free Software mathieu@2586: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA mathieu@2586: * mathieu@2586: * Authors: Mathieu Lacage mathieu@2586: */ mathieu@2395: #include "object-factory.h" mathieu@2395: #include mathieu@2395: mathieu@2395: namespace ns3 { mathieu@2395: mathieu@2395: ObjectFactory::ObjectFactory () mathieu@2395: {} mathieu@2395: mathieu@2395: void mathieu@2395: ObjectFactory::SetTypeId (TypeId tid) mathieu@2395: { mathieu@2395: m_tid = tid; mathieu@2395: } mathieu@2395: void mathieu@2395: ObjectFactory::SetTypeId (std::string tid) mathieu@2395: { mathieu@2395: m_tid = TypeId::LookupByName (tid); mathieu@2395: } mathieu@2395: void mathieu@2488: ObjectFactory::SetTypeId (const char *tid) mathieu@2488: { mathieu@2488: m_tid = TypeId::LookupByName (tid); mathieu@2488: } mathieu@2488: void mathieu@2965: ObjectFactory::Set (std::string name, const AttributeValue &value) mathieu@2395: { mathieu@2522: if (name == "") mathieu@2522: { mathieu@2522: return; mathieu@2522: } mathieu@2583: m_parameters.SetWithTid (m_tid, name, value); mathieu@2395: } mathieu@2395: mathieu@3891: void mathieu@3891: ObjectFactory::Set (const AttributeList &list) mathieu@3891: { mathieu@3891: m_parameters = list; mathieu@3891: } mathieu@3891: mathieu@2395: TypeId mathieu@2395: ObjectFactory::GetTypeId (void) const mathieu@2395: { mathieu@2395: return m_tid; mathieu@2395: } mathieu@2395: mathieu@2395: Ptr mathieu@2395: ObjectFactory::Create (void) const mathieu@2395: { mathieu@2631: Callback cb = m_tid.GetConstructor (); mathieu@2631: ObjectBase *base = cb (); mathieu@2631: Object *derived = dynamic_cast (base); mathieu@2631: derived->SetTypeId (m_tid); mathieu@2631: derived->Construct (m_parameters); mathieu@2631: Ptr object = Ptr (derived, false); mathieu@2395: return object; mathieu@2395: } mathieu@2395: mathieu@2555: std::ostream & operator << (std::ostream &os, const ObjectFactory &factory) mathieu@2555: { mathieu@2952: os << factory.m_tid.GetName () << "[" << factory.m_parameters.SerializeToString () << "]"; mathieu@2555: return os; mathieu@2555: } mathieu@2555: std::istream & operator >> (std::istream &is, ObjectFactory &factory) mathieu@2555: { mathieu@2956: std::string v; mathieu@2956: is >> v; mathieu@2956: std::string::size_type lbracket, rbracket; mathieu@2956: lbracket = v.find ("["); mathieu@2956: rbracket = v.find ("]"); mathieu@2956: NS_ASSERT (lbracket != std::string::npos); mathieu@2956: NS_ASSERT (rbracket != std::string::npos); mathieu@2956: std::string tid = v.substr (0, lbracket); mathieu@2956: std::string parameters = v.substr (lbracket+1,rbracket-(lbracket+1)); mathieu@2956: factory.SetTypeId (tid); mathieu@2956: factory.m_parameters.DeserializeFromString (parameters); mathieu@2555: return is; mathieu@2555: } mathieu@2555: mathieu@2555: mathieu@2582: ATTRIBUTE_HELPER_CPP (ObjectFactory); mathieu@2555: mathieu@2395: } // namespace ns3