A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
object-ptr-container.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 INRIA, Mathieu Lacage
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Mathieu Lacage <mathieu.lacage@gmail.com>
7 */
9
10#include "log.h"
11
12/**
13 * \file
14 * \ingroup attribute_ObjectPtrContainer
15 * ns3::ObjectPtrContainerValue attribute value implementations.
16 */
17
18namespace ns3
19{
20
21NS_LOG_COMPONENT_DEFINE("ObjectPtrContainer");
22
27
30{
31 NS_LOG_FUNCTION(this);
32 return m_objects.begin();
33}
34
37{
38 NS_LOG_FUNCTION(this);
39 return m_objects.end();
40}
41
42std::size_t
44{
45 NS_LOG_FUNCTION(this);
46 return m_objects.size();
47}
48
50ObjectPtrContainerValue::Get(std::size_t i) const
51{
52 NS_LOG_FUNCTION(this << i);
53 auto it = m_objects.find(i);
54 Ptr<Object> value = nullptr;
55 if (it != m_objects.end())
56 {
57 value = m_objects.find(i)->second;
58 }
59 return value;
60}
61
68
69std::string
71{
72 NS_LOG_FUNCTION(this << checker);
73 std::ostringstream oss;
74 Iterator it;
75 for (it = Begin(); it != End(); ++it)
76 {
77 oss << (*it).second;
78 if (it != End())
79 {
80 oss << " ";
81 }
82 }
83 return oss.str();
84}
85
86bool
89{
90 NS_LOG_FUNCTION(this << value << checker);
91 NS_FATAL_ERROR("cannot deserialize a set of object pointers.");
92 return true;
93}
94
95bool
97{
98 // not allowed.
99 NS_LOG_FUNCTION(this << object << &value);
100 return false;
101}
102
103bool
105{
106 NS_LOG_FUNCTION(this << object << &value);
107 auto v = dynamic_cast<ObjectPtrContainerValue*>(&value);
108 if (v == nullptr)
109 {
110 return false;
111 }
112 v->m_objects.clear();
113 std::size_t n;
114 bool ok = DoGetN(object, &n);
115 if (!ok)
116 {
117 return false;
118 }
119 for (std::size_t i = 0; i < n; i++)
120 {
121 std::size_t index;
122 Ptr<Object> o = DoGet(object, i, &index);
123 v->m_objects[index] = o;
124 }
125 return true;
126}
127
128bool
130{
131 NS_LOG_FUNCTION(this);
132 return true;
133}
134
135bool
137{
138 NS_LOG_FUNCTION(this);
139 return false;
140}
141
142} // namespace ns3
Hold a value for an Attribute.
Definition attribute.h:59
Anchor the ns-3 type and attribute system.
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.
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
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.
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.
ObjectPtrContainerValue()
Default constructor.
Smart pointer class similar to boost::intrusive_ptr.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::ObjectPtrContainerValue attribute value declarations and template implementations.