Files
unison/src/core/examples/main-ptr.cc

77 lines
1.4 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;
2013-11-18 12:39:40 -08:00
class PtrExample : public Object
2006-12-22 09:03:09 +01:00
{
public:
2013-11-18 12:39:40 -08:00
PtrExample ();
~PtrExample ();
2006-12-22 09:03:09 +01:00
void Method (void);
};
2013-11-18 12:39:40 -08:00
PtrExample::PtrExample ()
2006-12-22 09:03:09 +01:00
{
2013-11-18 12:39:40 -08:00
std::cout << "PtrExample constructor" << std::endl;
2006-12-22 09:03:09 +01:00
}
2013-11-18 12:39:40 -08:00
PtrExample::~PtrExample()
2006-12-22 09:03:09 +01:00
{
2013-11-18 12:39:40 -08:00
std::cout << "PtrExample destructor" << std::endl;
2006-12-22 09:03:09 +01:00
}
void
2013-11-18 12:39:40 -08:00
PtrExample::Method (void)
2006-12-22 09:03:09 +01:00
{
2013-11-18 12:39:40 -08:00
std::cout << "PtrExample method" << std::endl;
2006-12-22 09:03:09 +01:00
}
2013-11-18 12:39:40 -08:00
static Ptr<PtrExample> g_ptr = 0;
2006-12-22 09:03:09 +01:00
2013-11-18 12:39:40 -08:00
static Ptr<PtrExample>
StorePtr (Ptr<PtrExample> p)
2006-12-22 09:03:09 +01:00
{
2013-11-18 12:39:40 -08:00
Ptr<PtrExample> prev = g_ptr;
g_ptr = p;
2006-12-22 09:03:09 +01:00
return prev;
}
static void
2013-11-18 12:39:40 -08:00
ClearPtr (void)
2006-12-22 09:03:09 +01:00
{
2013-11-18 12:39:40 -08:00
g_ptr = 0;
2006-12-22 09:03:09 +01:00
}
int main (int argc, char *argv[])
{
{
2013-11-18 12:39:40 -08:00
// Create a new object of type PtrExample, store it in global
// variable g_ptr
Ptr<PtrExample> p = CreateObject<PtrExample> ();
p->Method ();
Ptr<PtrExample> prev = StorePtr (p);
NS_ASSERT (prev == 0);
2006-12-22 09:03:09 +01:00
}
{
2013-11-18 12:39:40 -08:00
// Create a new object of type PtrExample, store it in global
// variable g_ptr, get a hold on the previous PtrExample object.
Ptr<PtrExample> p = CreateObject<PtrExample> ();
Ptr<PtrExample> prev = StorePtr (p);
2006-12-22 09:03:09 +01:00
// call method on object
prev->Method ();
// Clear the currently-stored object
2013-11-18 12:39:40 -08:00
ClearPtr ();
// get the raw pointer and release it.
2013-11-18 12:39:40 -08:00
PtrExample *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;
}