00001 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2008 INRIA 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation; 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 * 00018 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> 00019 */ 00020 #include "object-factory.h" 00021 #include <sstream> 00022 00023 namespace ns3 { 00024 00025 ObjectFactory::ObjectFactory () 00026 {} 00027 00028 void 00029 ObjectFactory::SetTypeId (TypeId tid) 00030 { 00031 m_tid = tid; 00032 } 00033 void 00034 ObjectFactory::SetTypeId (std::string tid) 00035 { 00036 m_tid = TypeId::LookupByName (tid); 00037 } 00038 void 00039 ObjectFactory::SetTypeId (const char *tid) 00040 { 00041 m_tid = TypeId::LookupByName (tid); 00042 } 00043 void 00044 ObjectFactory::Set (std::string name, const AttributeValue &value) 00045 { 00046 if (name == "") 00047 { 00048 return; 00049 } 00050 m_parameters.SetWithTid (m_tid, name, value); 00051 } 00052 00053 void 00054 ObjectFactory::Set (const AttributeList &list) 00055 { 00056 m_parameters = list; 00057 } 00058 00059 TypeId 00060 ObjectFactory::GetTypeId (void) const 00061 { 00062 return m_tid; 00063 } 00064 00065 Ptr<Object> 00066 ObjectFactory::Create (void) const 00067 { 00068 Callback<ObjectBase *> cb = m_tid.GetConstructor (); 00069 ObjectBase *base = cb (); 00070 Object *derived = dynamic_cast<Object *> (base); 00071 derived->SetTypeId (m_tid); 00072 derived->Construct (m_parameters); 00073 Ptr<Object> object = Ptr<Object> (derived, false); 00074 return object; 00075 } 00076 00077 std::ostream & operator << (std::ostream &os, const ObjectFactory &factory) 00078 { 00079 os << factory.m_tid.GetName () << "[" << factory.m_parameters.SerializeToString () << "]"; 00080 return os; 00081 } 00082 std::istream & operator >> (std::istream &is, ObjectFactory &factory) 00083 { 00084 std::string v; 00085 is >> v; 00086 std::string::size_type lbracket, rbracket; 00087 lbracket = v.find ("["); 00088 rbracket = v.find ("]"); 00089 NS_ASSERT (lbracket != std::string::npos); 00090 NS_ASSERT (rbracket != std::string::npos); 00091 std::string tid = v.substr (0, lbracket); 00092 std::string parameters = v.substr (lbracket+1,rbracket-(lbracket+1)); 00093 factory.SetTypeId (tid); 00094 factory.m_parameters.DeserializeFromString (parameters); 00095 return is; 00096 } 00097 00098 00099 ATTRIBUTE_HELPER_CPP (ObjectFactory); 00100 00101 } // namespace ns3