1 /* -*- Mode:NS3; -*- */ |
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
2 #include "ns3/callback.h" |
2 #include "ns3/callback.h" |
3 #include <cassert> |
3 #include <cassert> |
4 #include <iostream> |
4 #include <iostream> |
5 |
5 |
6 using namespace ns3; |
6 using namespace ns3; |
7 |
7 |
8 static double |
8 static double |
9 CbOne (double a, double b) |
9 CbOne (double a, double b) |
10 { |
10 { |
11 std::cout << "invoke cbOne a=" << a << ", b=" << b << std::endl; |
11 std::cout << "invoke cbOne a=" << a << ", b=" << b << std::endl; |
12 return a; |
12 return a; |
13 } |
13 } |
14 |
14 |
15 class MyCb { |
15 class MyCb { |
16 public: |
16 public: |
17 int CbTwo (double a) { |
17 int CbTwo (double a) { |
18 std::cout << "invoke cbTwo a=" << a << std::endl; |
18 std::cout << "invoke cbTwo a=" << a << std::endl; |
19 return -5; |
19 return -5; |
20 } |
20 } |
21 }; |
21 }; |
22 |
22 |
23 |
23 |
24 int main (int argc, char *argv[]) |
24 int main (int argc, char *argv[]) |
25 { |
25 { |
26 // return type: double |
26 // return type: double |
27 // first arg type: double |
27 // first arg type: double |
28 // second arg type: double |
28 // second arg type: double |
29 Callback<double, double, double> one; |
29 Callback<double, double, double> one; |
30 // build callback instance which points to cbOne function |
30 // build callback instance which points to cbOne function |
31 one = MakeCallback (&CbOne); |
31 one = MakeCallback (&CbOne); |
32 // this is not a null callback |
32 // this is not a null callback |
33 assert (!one.IsNull ()); |
33 assert (!one.IsNull ()); |
34 // invoke cbOne function through callback instance |
34 // invoke cbOne function through callback instance |
35 double retOne; |
35 double retOne; |
36 retOne = one (10.0, 20.0); |
36 retOne = one (10.0, 20.0); |
37 |
37 |
38 // return type: int |
38 // return type: int |
39 // first arg type: double |
39 // first arg type: double |
40 Callback<int, double> two; |
40 Callback<int, double> two; |
41 MyCb cb; |
41 MyCb cb; |
42 // build callback instance which points to MyCb::cbTwo |
42 // build callback instance which points to MyCb::cbTwo |
43 two = MakeCallback (&MyCb::CbTwo, &cb); |
43 two = MakeCallback (&MyCb::CbTwo, &cb); |
44 // this is not a null callback |
44 // this is not a null callback |
45 assert (!two.IsNull ()); |
45 assert (!two.IsNull ()); |
46 // invoke MyCb::cbTwo through callback instance |
46 // invoke MyCb::cbTwo through callback instance |
47 int retTwo; |
47 int retTwo; |
48 retTwo = two (10.0); |
48 retTwo = two (10.0); |
49 |
49 |
50 two = MakeNullCallback<int, double> (); |
50 two = MakeNullCallback<int, double> (); |
51 // invoking a null callback is just like |
51 // invoking a null callback is just like |
52 // invoking a null function pointer: |
52 // invoking a null function pointer: |
53 // it will crash. |
53 // it will crash. |
54 //int retTwoNull = two (20.0); |
54 //int retTwoNull = two (20.0); |
55 assert (two.IsNull ()); |
55 assert (two.IsNull ()); |
56 |
56 |
57 return 0; |
57 return 0; |
58 } |
58 } |