src/core/object-factory.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Mon, 02 Jun 2008 10:30:24 -0700
changeset 3190 51fe9001a679
parent 2965 4b28e9740e3b
child 3891 590b21d7fcdd
permissions -rw-r--r--
add some doxygen, remove a couple of XXX
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2586
50d78910a997 add missing copyright/license headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2583
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
50d78910a997 add missing copyright/license headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2583
diff changeset
     2
/*
50d78910a997 add missing copyright/license headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2583
diff changeset
     3
 * Copyright (c) 2008 INRIA
50d78910a997 add missing copyright/license headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2583
diff changeset
     4
 *
50d78910a997 add missing copyright/license headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2583
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
50d78910a997 add missing copyright/license headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2583
diff changeset
     6
 * it under the terms of the GNU General Public License version 2 as
50d78910a997 add missing copyright/license headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2583
diff changeset
     7
 * published by the Free Software Foundation;
50d78910a997 add missing copyright/license headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2583
diff changeset
     8
 *
50d78910a997 add missing copyright/license headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2583
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
50d78910a997 add missing copyright/license headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2583
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
50d78910a997 add missing copyright/license headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2583
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
50d78910a997 add missing copyright/license headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2583
diff changeset
    12
 * GNU General Public License for more details.
50d78910a997 add missing copyright/license headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2583
diff changeset
    13
 *
50d78910a997 add missing copyright/license headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2583
diff changeset
    14
 * You should have received a copy of the GNU General Public License
50d78910a997 add missing copyright/license headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2583
diff changeset
    15
 * along with this program; if not, write to the Free Software
50d78910a997 add missing copyright/license headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2583
diff changeset
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
50d78910a997 add missing copyright/license headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2583
diff changeset
    17
 *
50d78910a997 add missing copyright/license headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2583
diff changeset
    18
 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
50d78910a997 add missing copyright/license headers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2583
diff changeset
    19
 */
2395
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    20
#include "object-factory.h"
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    21
#include <sstream>
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    22
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    23
namespace ns3 {
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    24
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    25
ObjectFactory::ObjectFactory ()
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    26
{}
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    27
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    28
void 
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    29
ObjectFactory::SetTypeId (TypeId tid)
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    30
{
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    31
  m_tid = tid;
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    32
}
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    33
void 
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    34
ObjectFactory::SetTypeId (std::string tid)
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    35
{
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    36
  m_tid = TypeId::LookupByName (tid);
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    37
}
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    38
void 
2488
c35c4f282206 add an extra overload of SetTypeId to avoid overload resolution confusions.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2433
diff changeset
    39
ObjectFactory::SetTypeId (const char *tid)
c35c4f282206 add an extra overload of SetTypeId to avoid overload resolution confusions.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2433
diff changeset
    40
{
c35c4f282206 add an extra overload of SetTypeId to avoid overload resolution confusions.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2433
diff changeset
    41
  m_tid = TypeId::LookupByName (tid);
c35c4f282206 add an extra overload of SetTypeId to avoid overload resolution confusions.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2433
diff changeset
    42
}
c35c4f282206 add an extra overload of SetTypeId to avoid overload resolution confusions.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2433
diff changeset
    43
void 
2965
4b28e9740e3b get rid of Attribute class. Use AttributeValue subclasses directly.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2956
diff changeset
    44
ObjectFactory::Set (std::string name, const AttributeValue &value)
2395
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    45
{
2522
7c9d1c314beb report attribute setting errors as early as possible.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2502
diff changeset
    46
  if (name == "")
7c9d1c314beb report attribute setting errors as early as possible.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2502
diff changeset
    47
    {
7c9d1c314beb report attribute setting errors as early as possible.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2502
diff changeset
    48
      return;
7c9d1c314beb report attribute setting errors as early as possible.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2502
diff changeset
    49
    }
2583
c09faa6c90bf doxygen doc
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2582
diff changeset
    50
  m_parameters.SetWithTid (m_tid, name, value);
2395
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    51
}
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    52
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    53
TypeId 
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    54
ObjectFactory::GetTypeId (void) const
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    55
{
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    56
  return m_tid;
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    57
}
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    58
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    59
Ptr<Object> 
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    60
ObjectFactory::Create (void) const
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    61
{
2631
365595f1f9a8 replace TypeId::CreateObject with TypeId::GetConstructor
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2586
diff changeset
    62
  Callback<ObjectBase *> cb = m_tid.GetConstructor ();
365595f1f9a8 replace TypeId::CreateObject with TypeId::GetConstructor
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2586
diff changeset
    63
  ObjectBase *base = cb ();
365595f1f9a8 replace TypeId::CreateObject with TypeId::GetConstructor
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2586
diff changeset
    64
  Object *derived = dynamic_cast<Object *> (base);
365595f1f9a8 replace TypeId::CreateObject with TypeId::GetConstructor
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2586
diff changeset
    65
  derived->SetTypeId (m_tid);
365595f1f9a8 replace TypeId::CreateObject with TypeId::GetConstructor
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2586
diff changeset
    66
  derived->Construct (m_parameters);
365595f1f9a8 replace TypeId::CreateObject with TypeId::GetConstructor
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2586
diff changeset
    67
  Ptr<Object> object = Ptr<Object> (derived, false);
2395
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    68
  return object;
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    69
}
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    70
2555
469bcb4d0010 give Attribute power to ObjectFactory class.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2522
diff changeset
    71
std::ostream & operator << (std::ostream &os, const ObjectFactory &factory)
469bcb4d0010 give Attribute power to ObjectFactory class.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2522
diff changeset
    72
{
2952
d5047d6ce4ce implement operator << for ObjectFactory.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2631
diff changeset
    73
  os << factory.m_tid.GetName () << "[" << factory.m_parameters.SerializeToString () << "]";
2555
469bcb4d0010 give Attribute power to ObjectFactory class.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2522
diff changeset
    74
  return os;
469bcb4d0010 give Attribute power to ObjectFactory class.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2522
diff changeset
    75
}
469bcb4d0010 give Attribute power to ObjectFactory class.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2522
diff changeset
    76
std::istream & operator >> (std::istream &is, ObjectFactory &factory)
469bcb4d0010 give Attribute power to ObjectFactory class.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2522
diff changeset
    77
{
2956
1c1345207c74 implement operator >>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2952
diff changeset
    78
  std::string v;
1c1345207c74 implement operator >>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2952
diff changeset
    79
  is >> v;
1c1345207c74 implement operator >>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2952
diff changeset
    80
  std::string::size_type lbracket, rbracket;
1c1345207c74 implement operator >>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2952
diff changeset
    81
  lbracket = v.find ("[");
1c1345207c74 implement operator >>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2952
diff changeset
    82
  rbracket = v.find ("]");
1c1345207c74 implement operator >>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2952
diff changeset
    83
  NS_ASSERT (lbracket != std::string::npos);
1c1345207c74 implement operator >>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2952
diff changeset
    84
  NS_ASSERT (rbracket != std::string::npos);
1c1345207c74 implement operator >>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2952
diff changeset
    85
  std::string tid = v.substr (0, lbracket);
1c1345207c74 implement operator >>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2952
diff changeset
    86
  std::string parameters = v.substr (lbracket+1,rbracket-(lbracket+1));
1c1345207c74 implement operator >>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2952
diff changeset
    87
  factory.SetTypeId (tid);
1c1345207c74 implement operator >>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2952
diff changeset
    88
  factory.m_parameters.DeserializeFromString (parameters);
2555
469bcb4d0010 give Attribute power to ObjectFactory class.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2522
diff changeset
    89
  return is;
469bcb4d0010 give Attribute power to ObjectFactory class.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2522
diff changeset
    90
}
469bcb4d0010 give Attribute power to ObjectFactory class.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2522
diff changeset
    91
469bcb4d0010 give Attribute power to ObjectFactory class.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2522
diff changeset
    92
2582
3e28107b870f rename old VALUE leftovers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2555
diff changeset
    93
ATTRIBUTE_HELPER_CPP (ObjectFactory);
2555
469bcb4d0010 give Attribute power to ObjectFactory class.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2522
diff changeset
    94
2395
ffd1c96afe4b a pretty simple wrapper around TypeId+Parameters: ObjectFactory
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    95
} // namespace ns3