A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
channel-list.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 
19 #include "ns3/simulator.h"
20 #include "ns3/object-vector.h"
21 #include "ns3/config.h"
22 #include "ns3/log.h"
23 #include "ns3/assert.h"
24 #include "channel-list.h"
25 #include "channel.h"
26 
27 namespace ns3 {
28 
29 NS_LOG_COMPONENT_DEFINE ("ChannelList");
30 
34 class ChannelListPriv : public Object
35 {
36 public:
37  static TypeId GetTypeId (void);
38  ChannelListPriv ();
39  ~ChannelListPriv ();
40 
41  uint32_t Add (Ptr<Channel> channel);
42 
43  ChannelList::Iterator Begin (void) const;
44  ChannelList::Iterator End (void) const;
45 
46  Ptr<Channel> GetChannel (uint32_t n);
47  uint32_t GetNChannels (void);
48 
49  static Ptr<ChannelListPriv> Get (void);
50 
51 private:
52  static Ptr<ChannelListPriv> *DoGet (void);
53  static void Delete (void);
54  virtual void DoDispose (void);
55  std::vector<Ptr<Channel> > m_channels;
56 };
57 
58 NS_OBJECT_ENSURE_REGISTERED (ChannelListPriv);
59 
60 TypeId
61 ChannelListPriv::GetTypeId (void)
62 {
63  static TypeId tid = TypeId ("ns3::ChannelListPriv")
64  .SetParent<Object> ()
65  .AddAttribute ("ChannelList", "The list of all channels created during the simulation.",
67  MakeObjectVectorAccessor (&ChannelListPriv::m_channels),
68  MakeObjectVectorChecker<Channel> ())
69  ;
70  return tid;
71 }
72 
73 Ptr<ChannelListPriv>
74 ChannelListPriv::Get (void)
75 {
77  return *DoGet ();
78 }
79 
80 Ptr<ChannelListPriv> *
81 ChannelListPriv::DoGet (void)
82 {
84  static Ptr<ChannelListPriv> ptr = 0;
85  if (ptr == 0)
86  {
87  ptr = CreateObject<ChannelListPriv> ();
89  Simulator::ScheduleDestroy (&ChannelListPriv::Delete);
90  }
91  return &ptr;
92 }
93 
94 void
95 ChannelListPriv::Delete (void)
96 {
99  (*DoGet ()) = 0;
100 }
101 
102 ChannelListPriv::ChannelListPriv ()
103 {
104  NS_LOG_FUNCTION (this);
105 }
106 
107 ChannelListPriv::~ChannelListPriv ()
108 {
109  NS_LOG_FUNCTION (this);
110 }
111 void
113 {
114  NS_LOG_FUNCTION (this);
115  for (std::vector<Ptr<Channel> >::iterator i = m_channels.begin ();
116  i != m_channels.end (); i++)
117  {
118  Ptr<Channel> channel = *i;
119  channel->Dispose ();
120  *i = 0;
121  }
122  m_channels.erase (m_channels.begin (), m_channels.end ());
124 }
125 
126 uint32_t
127 ChannelListPriv::Add (Ptr<Channel> channel)
128 {
129  NS_LOG_FUNCTION (this << channel);
130  uint32_t index = m_channels.size ();
131  m_channels.push_back (channel);
132  return index;
133 
134 }
135 
136 ChannelList::Iterator
137 ChannelListPriv::Begin (void) const
138 {
139  NS_LOG_FUNCTION (this);
140  return m_channels.begin ();
141 }
142 
143 ChannelList::Iterator
144 ChannelListPriv::End (void) const
145 {
146  NS_LOG_FUNCTION (this);
147  return m_channels.end ();
148 }
149 
150 uint32_t
151 ChannelListPriv::GetNChannels (void)
152 {
153  NS_LOG_FUNCTION (this);
154  return m_channels.size ();
155 }
156 
157 Ptr<Channel>
158 ChannelListPriv::GetChannel (uint32_t n)
159 {
160  NS_LOG_FUNCTION (this << n);
161  NS_ASSERT_MSG (n < m_channels.size (), "Channel index " << n <<
162  " is out of range (only have " << m_channels.size () << " channels).");
163  return m_channels[n];
164 }
165 
166 uint32_t
168 {
170  return ChannelListPriv::Get ()->Add (channel);
171 }
172 
173 ChannelList::Iterator
175 {
177  return ChannelListPriv::Get ()->Begin ();
178 }
179 
180 ChannelList::Iterator
182 {
184  return ChannelListPriv::Get ()->End ();
185 }
186 
189 {
190  NS_LOG_FUNCTION (n);
191  return ChannelListPriv::Get ()->GetChannel (n);
192 }
193 
194 uint32_t
196 {
198  return ChannelListPriv::Get ()->GetNChannels ();
199 }
200 
201 } // namespace ns3
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
static uint32_t GetNChannels(void)
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
virtual void DoDispose(void)
virtual void DoDispose(void)
Definition: object.cc:335
void UnregisterRootNamespaceObject(Ptr< Object > obj)
Definition: config.cc:751
#define NS_LOG_FUNCTION_NOARGS()
Definition: log.h:275
static Iterator Begin(void)
private implementation detail of the ChannelList API.
Definition: channel-list.cc:34
void RegisterRootNamespaceObject(Ptr< Object > obj)
Definition: config.cc:745
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
static uint32_t Add(Ptr< Channel > channel)
static EventId ScheduleDestroy(MEM mem_ptr, OBJ obj)
Definition: simulator.h:1072
static Iterator End(void)
a base class which provides memory management and object aggregation
Definition: object.h:63
contain a set of ns3::Object pointers.
static Ptr< Channel > GetChannel(uint32_t n)
a unique identifier for an interface.
Definition: type-id.h:44
TypeId SetParent(TypeId tid)
Definition: type-id.cc:471