Files
unison/samples/main-ptr.cc

77 lines
1.2 KiB
C++
Raw Normal View History

2006-12-22 09:03:09 +01:00
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
#include "ns3/ptr.h"
2007-05-08 11:44:04 -04:00
#include "ns3/object.h"
2006-12-22 09:03:09 +01:00
#include <iostream>
using namespace ns3;
2007-05-08 11:44:04 -04:00
class A : public Object
2006-12-22 09:03:09 +01:00
{
public:
A ();
~A ();
void Method (void);
};
A::A ()
{
std::cout << "A constructor" << std::endl;
}
A::~A()
{
std::cout << "A destructor" << std::endl;
}
void
A::Method (void)
{
std::cout << "A method" << std::endl;
}
static Ptr<A> g_a = 0;
static Ptr<A>
StoreA (Ptr<A> a)
{
Ptr<A> prev = g_a;
g_a = a;
return prev;
}
static void
ClearA (void)
{
g_a = 0;
}
int main (int argc, char *argv[])
{
{
// Create a new object of type A, store it in global
// variable g_a
2007-06-04 16:32:37 +02:00
Ptr<A> a = Create<A> ();
2006-12-22 09:03:09 +01:00
a->Method ();
Ptr<A> prev = StoreA (a);
NS_ASSERT (prev == 0);
2006-12-22 09:03:09 +01:00
}
{
// Create a new object of type A, store it in global
// variable g_a, get a hold on the previous A object.
2007-06-04 16:32:37 +02:00
Ptr<A> a = Create<A> ();
2006-12-22 09:03:09 +01:00
Ptr<A> prev = StoreA (a);
// call method on object
prev->Method ();
// Clear the currently-stored object
ClearA ();
// get the raw pointer and release it.
A *raw = GetPointer (prev);
prev = 0;
2006-12-22 09:03:09 +01:00
raw->Method ();
raw->Unref ();
2006-12-22 09:03:09 +01:00
}
return 0;
}