A Discrete-Event Network Simulator
API
object-ptr-container.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007 INRIA, Mathieu Lacage
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Authors: Mathieu Lacage <mathieu.lacage@gmail.com>
18  */
19 #include "object-ptr-container.h"
20 
21 #include "log.h"
22 
29 namespace ns3
30 {
31 
32 NS_LOG_COMPONENT_DEFINE("ObjectPtrContainer");
33 
35 {
36  NS_LOG_FUNCTION(this);
37 }
38 
41 {
42  NS_LOG_FUNCTION(this);
43  return m_objects.begin();
44 }
45 
48 {
49  NS_LOG_FUNCTION(this);
50  return m_objects.end();
51 }
52 
53 std::size_t
55 {
56  NS_LOG_FUNCTION(this);
57  return m_objects.size();
58 }
59 
61 ObjectPtrContainerValue::Get(std::size_t i) const
62 {
63  NS_LOG_FUNCTION(this << i);
64  Iterator it = m_objects.find(i);
65  Ptr<Object> value = nullptr;
66  if (it != m_objects.end())
67  {
68  value = m_objects.find(i)->second;
69  }
70  return value;
71 }
72 
75 {
76  NS_LOG_FUNCTION(this);
77  return ns3::Create<ObjectPtrContainerValue>(*this);
78 }
79 
80 std::string
82 {
83  NS_LOG_FUNCTION(this << checker);
84  std::ostringstream oss;
85  Iterator it;
86  for (it = Begin(); it != End(); ++it)
87  {
88  oss << (*it).second;
89  if (it != End())
90  {
91  oss << " ";
92  }
93  }
94  return oss.str();
95 }
96 
97 bool
100 {
101  NS_LOG_FUNCTION(this << value << checker);
102  NS_FATAL_ERROR("cannot deserialize a set of object pointers.");
103  return true;
104 }
105 
106 bool
108 {
109  // not allowed.
110  NS_LOG_FUNCTION(this << object << &value);
111  return false;
112 }
113 
114 bool
116 {
117  NS_LOG_FUNCTION(this << object << &value);
119  if (v == nullptr)
120  {
121  return false;
122  }
123  v->m_objects.clear();
124  std::size_t n;
125  bool ok = DoGetN(object, &n);
126  if (!ok)
127  {
128  return false;
129  }
130  for (std::size_t i = 0; i < n; i++)
131  {
132  std::size_t index;
133  Ptr<Object> o = DoGet(object, i, &index);
134  v->m_objects[index] = o;
135  }
136  return true;
137 }
138 
139 bool
141 {
142  NS_LOG_FUNCTION(this);
143  return true;
144 }
145 
146 bool
148 {
149  NS_LOG_FUNCTION(this);
150  return false;
151 }
152 
153 } // namespace ns3
Hold a value for an Attribute.
Definition: attribute.h:70
Anchor the ns-3 type and attribute system.
Definition: object-base.h:173
virtual bool DoGetN(const ObjectBase *object, std::size_t *n) const =0
Get the number of instances in the container.
bool Set(ObjectBase *object, const AttributeValue &value) const override
bool Get(const ObjectBase *object, AttributeValue &value) const override
virtual Ptr< Object > DoGet(const ObjectBase *object, std::size_t i, std::size_t *index) const =0
Get an instance from the container, identified by index.
Container for a set of ns3::Object pointers.
std::map< std::size_t, Ptr< Object > > m_objects
The container implementation.
std::size_t GetN() const
Get the number of Objects.
Iterator End() const
Get an iterator to the past-the-end Object.
ObjectPtrContainerValue()
Default constructor.
Iterator Begin() const
Get an iterator to the first Object.
bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker) override
Deserialize from a string.
std::map< std::size_t, Ptr< Object > >::const_iterator Iterator
Iterator type for traversing this container.
Ptr< Object > Get(std::size_t i) const
Get a specific Object.
std::string SerializeToString(Ptr< const AttributeChecker > checker) const override
Serialize each of the Object pointers to a string.
Ptr< AttributeValue > Copy() const override
Get a copy of this container.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
value
Definition: second.py:41
ns3::ObjectPtrContainerValue attribute value declarations and template implementations.