A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
simulator.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005 INRIA
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 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19
20#ifndef SIMULATOR_H
21#define SIMULATOR_H
22
23#include "event-id.h"
24#include "event-impl.h"
25#include "make-event.h"
26#include "nstime.h"
27#include "object-factory.h"
28
29#include <stdint.h>
30#include <string>
31
38namespace ns3
39{
40
41class SimulatorImpl;
42class Scheduler;
43
68{
69 public:
70 // Delete default constructor and destructor to avoid misuse
71 Simulator() = delete;
72 ~Simulator() = delete;
73
86 static void SetImplementation(Ptr<SimulatorImpl> impl);
87
106
115 static void SetScheduler(ObjectFactory schedulerFactory);
116
126 static void Destroy();
127
137 static bool IsFinished();
138
149 static void Run();
150
159 static void Stop();
160
170 static void Stop(const Time& delay);
171
188 static uint32_t GetContext();
189
197 enum : uint32_t
198 {
202 NO_CONTEXT = 0xffffffff
203 };
204
209 static uint64_t GetEventCount();
210
231 template <typename FUNC,
232 std::enable_if_t<!std::is_convertible_v<FUNC, Ptr<EventImpl>>, int> = 0,
233 std::enable_if_t<!std::is_function_v<std::remove_pointer_t<FUNC>>, int> = 0,
234 typename... Ts>
235 static EventId Schedule(const Time& delay, FUNC f, Ts&&... args);
236
252 template <typename... Us, typename... Ts>
253 static EventId Schedule(const Time& delay, void (*f)(Us...), Ts&&... args); // Schedule events (in the same context) to run at a future time.
255
277 template <typename FUNC,
278 std::enable_if_t<!std::is_convertible_v<FUNC, Ptr<EventImpl>>, int> = 0,
279 std::enable_if_t<!std::is_function_v<std::remove_pointer_t<FUNC>>, int> = 0,
280 typename... Ts>
281 static void ScheduleWithContext(uint32_t context, const Time& delay, FUNC f, Ts&&... args);
282
295 template <typename... Us, typename... Ts>
296 static void ScheduleWithContext(uint32_t context,
297 const Time& delay,
298 void (*f)(Us...),
299 Ts&&... args); // Schedule events (in a different context) to run now or at a future time.
301
320 template <typename FUNC,
321 std::enable_if_t<!std::is_convertible_v<FUNC, Ptr<EventImpl>>, int> = 0,
322 std::enable_if_t<!std::is_function_v<std::remove_pointer_t<FUNC>>, int> = 0,
323 typename... Ts>
324 static EventId ScheduleNow(FUNC f, Ts&&... args);
325
337 template <typename... Us, typename... Ts>
338 static EventId ScheduleNow(void (*f)(Us...), Ts&&... args);
339 // Schedule events (in the same context) to run now.
341
362 template <typename FUNC,
363 std::enable_if_t<!std::is_convertible_v<FUNC, Ptr<EventImpl>>, int> = 0,
364 std::enable_if_t<!std::is_function_v<std::remove_pointer_t<FUNC>>, int> = 0,
365 typename... Ts>
366 static EventId ScheduleDestroy(FUNC f, Ts&&... args);
367
380 template <typename... Us, typename... Ts>
381 static EventId ScheduleDestroy(void (*f)(Us...), Ts&&... args);
382 // Schedule events to run when Simulator:Destroy() is called.
384
397 static void Remove(const EventId& id);
398
412 static void Cancel(const EventId& id);
413
428 static bool IsExpired(const EventId& id);
429
435 static Time Now();
436
445 static Time GetDelayLeft(const EventId& id);
446
456
464 static EventId Schedule(const Time& delay, const Ptr<EventImpl>& event);
465
474 static void ScheduleWithContext(uint32_t context, const Time& delay, EventImpl* event);
475
483 static EventId ScheduleDestroy(const Ptr<EventImpl>& event);
484
491 static EventId ScheduleNow(const Ptr<EventImpl>& event);
492
500 static uint32_t GetSystemId();
501
502 private:
509 static EventId DoSchedule(const Time& delay, EventImpl* event);
515 static EventId DoScheduleNow(EventImpl* event);
521 static EventId DoScheduleDestroy(EventImpl* event);
522
523}; // class Simulator
524
538Time Now();
539
540} // namespace ns3
541
542/********************************************************************
543 * Implementation of the templates declared above.
544 ********************************************************************/
545
546namespace ns3
547{
548
549// Doxygen has trouble with static template functions in a class:
550// it treats the in-class declaration as different from the
551// out of class definition, so makes two entries in the member list. Ugh
552
553template <typename FUNC,
554 std::enable_if_t<!std::is_convertible_v<FUNC, Ptr<EventImpl>>, int>,
555 std::enable_if_t<!std::is_function_v<std::remove_pointer_t<FUNC>>, int>,
556 typename... Ts>
557EventId
558Simulator::Schedule(const Time& delay, FUNC f, Ts&&... args)
559{
560 return DoSchedule(delay, MakeEvent(f, std::forward<Ts>(args)...));
561}
562
563template <typename... Us, typename... Ts>
565Simulator::Schedule(const Time& delay, void (*f)(Us...), Ts&&... args)
566{
567 return DoSchedule(delay, MakeEvent(f, std::forward<Ts>(args)...));
568}
569
570template <typename FUNC,
571 std::enable_if_t<!std::is_convertible_v<FUNC, Ptr<EventImpl>>, int>,
572 std::enable_if_t<!std::is_function_v<std::remove_pointer_t<FUNC>>, int>,
573 typename... Ts>
574void
575Simulator::ScheduleWithContext(uint32_t context, const Time& delay, FUNC f, Ts&&... args)
576{
577 return ScheduleWithContext(context, delay, MakeEvent(f, std::forward<Ts>(args)...));
578}
579
580template <typename... Us, typename... Ts>
581void
582Simulator::ScheduleWithContext(uint32_t context, const Time& delay, void (*f)(Us...), Ts&&... args)
583{
584 return ScheduleWithContext(context, delay, MakeEvent(f, std::forward<Ts>(args)...));
585}
586
587template <typename FUNC,
588 std::enable_if_t<!std::is_convertible_v<FUNC, Ptr<EventImpl>>, int>,
589 std::enable_if_t<!std::is_function_v<std::remove_pointer_t<FUNC>>, int>,
590 typename... Ts>
592Simulator::ScheduleNow(FUNC f, Ts&&... args)
593{
594 return DoScheduleNow(MakeEvent(f, std::forward<Ts>(args)...));
595}
596
597template <typename... Us, typename... Ts>
599Simulator::ScheduleNow(void (*f)(Us...), Ts&&... args)
600{
601 return DoScheduleNow(MakeEvent(f, std::forward<Ts>(args)...));
602}
603
604template <typename FUNC,
605 std::enable_if_t<!std::is_convertible_v<FUNC, Ptr<EventImpl>>, int>,
606 std::enable_if_t<!std::is_function_v<std::remove_pointer_t<FUNC>>, int>,
607 typename... Ts>
609Simulator::ScheduleDestroy(FUNC f, Ts&&... args)
610{
611 return DoScheduleDestroy(MakeEvent(f, std::forward<Ts>(args)...));
612}
613
614template <typename... Us, typename... Ts>
616Simulator::ScheduleDestroy(void (*f)(Us...), Ts&&... args)
617{
618 return DoScheduleDestroy(MakeEvent(f, std::forward<Ts>(args)...));
619}
620
621} // namespace ns3
622
623#endif /* SIMULATOR_H */
double f(double x, void *params)
Definition: 80211b.c:70
An identifier for simulation events.
Definition: event-id.h:55
A simulation event.
Definition: event-impl.h:46
Instantiate subclasses of ns3::Object.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
Control the scheduling of simulation events.
Definition: simulator.h:68
static EventId DoScheduleDestroy(EventImpl *event)
Implementation of the various ScheduleDestroy methods.
Definition: simulator.cc:260
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:558
static Ptr< SimulatorImpl > GetImplementation()
Get the SimulatorImpl singleton.
Definition: simulator.cc:364
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
Definition: simulator.cc:276
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:140
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:575
static bool IsFinished()
Check if the simulation should finish.
Definition: simulator.cc:169
static uint32_t GetSystemId()
Get the system id of this simulator.
Definition: simulator.cc:321
@ NO_CONTEXT
Flag for events not associated with any particular context.
Definition: simulator.h:202
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:199
static void Run()
Run the simulation.
Definition: simulator.cc:176
static bool IsExpired(const EventId &id)
Check if an event has already run or been cancelled.
Definition: simulator.cc:286
Simulator()=delete
static EventId ScheduleDestroy(FUNC f, Ts &&... args)
Schedule an event to run at the end of the simulation, when Simulator::Destroy() is called.
Definition: simulator.h:609
static void SetScheduler(ObjectFactory schedulerFactory)
Set the scheduler type with an ObjectFactory.
Definition: simulator.cc:162
static EventId DoScheduleNow(EventImpl *event)
Implementation of the various ScheduleNow methods.
Definition: simulator.cc:251
static uint64_t GetEventCount()
Get the number of events executed.
Definition: simulator.cc:315
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:592
~Simulator()=delete
static EventId DoSchedule(const Time &delay, EventImpl *event)
Implementation of the various Schedule methods.
Definition: simulator.cc:242
static Time GetMaximumSimulationTime()
Get the maximum representable simulation time.
Definition: simulator.cc:302
static void Remove(const EventId &id)
Remove an event from the event list.
Definition: simulator.cc:266
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:184
static void SetImplementation(Ptr< SimulatorImpl > impl)
Definition: simulator.cc:336
static uint32_t GetContext()
Get the current simulation context.
Definition: simulator.cc:309
static Time GetDelayLeft(const EventId &id)
Get the remaining time until this event will execute.
Definition: simulator.cc:208
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
ns3::EventId declarations.
ns3::EventImpl declarations.
EventImpl * MakeEvent(void(*f)())
Make an EventImpl from a function pointer taking varying numbers of arguments.
Definition: make-event.cc:36
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:296
ns3::MakeEvent function declarations and template implementation.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Declaration of classes ns3::Time and ns3::TimeWithUnit, and the TimeValue implementation classes.
ns3::ObjectFactory class declaration.