A Discrete-Event Network Simulator
API
fourth.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  */
16 
17 #include "ns3/object.h"
18 #include "ns3/uinteger.h"
19 #include "ns3/traced-value.h"
20 #include "ns3/trace-source-accessor.h"
21 #include "ns3/simulator.h"
22 
23 #include <iostream>
24 
25 using namespace ns3;
26 
27 class MyObject : public Object
28 {
29 public:
34  static TypeId GetTypeId (void)
35  {
36  static TypeId tid = TypeId ("MyObject")
37  .SetParent<Object> ()
38  .SetGroupName ("Tutorial")
39  .AddConstructor<MyObject> ()
40  .AddTraceSource ("MyInteger",
41  "An integer value to trace.",
43  "ns3::TracedValueCallback::Int32")
44  ;
45  return tid;
46  }
47 
48  MyObject () {}
50 };
51 
52 void
53 IntTrace (int32_t oldValue, int32_t newValue)
54 {
55  std::cout << "Traced " << oldValue << " to " << newValue << std::endl;
56 }
57 
58 int
59 main (int argc, char *argv[])
60 {
61  Ptr<MyObject> myObject = CreateObject<MyObject> ();
62  myObject->TraceConnectWithoutContext ("MyInteger", MakeCallback (&IntTrace));
63 
64  myObject->m_myInt = 1234;
65 }
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
MyObject::MyObject
MyObject()
Definition: fourth.cc:48
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
ns3::MakeTraceSourceAccessor
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Definition: trace-source-accessor.h:202
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
ns3::Object
A base class which provides memory management and object aggregation.
Definition: object.h:88
MyObject::m_myInt
TracedValue< int32_t > m_myInt
Definition: fourth.cc:49
IntTrace
void IntTrace(int32_t oldValue, int32_t newValue)
Definition: fourth.cc:53
MyObject::GetTypeId
static TypeId GetTypeId(void)
Register this type.
Definition: fourth.cc:34
ns3::MakeCallback
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1642
MyObject
Definition: fourth.cc:28
ns3::TracedValue< int32_t >
ns3::ObjectBase::TraceConnectWithoutContext
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:293