src/core/ptr.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Thu, 10 May 2007 18:33:52 +0200
changeset 567 6fb98941c36f
parent 544 cbc4158d47c9
child 569 31a7c6fc511e
permissions -rw-r--r--
remove leaks and rework the Ptr class to work with a new refcount mechanism
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
223
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     2
/*
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     3
 * Copyright (c) 2005,2006 INRIA
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     4
 * All rights reserved.
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     5
 *
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     7
 * it under the terms of the GNU General Public License version 2 as
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     8
 * published by the Free Software Foundation;
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     9
 *
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    13
 * GNU General Public License for more details.
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    14
 *
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    16
 * along with this program; if not, write to the Free Software
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    18
 *
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    19
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    20
 */
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    21
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    22
#ifndef PTR_H
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    23
#define PTR_H
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    24
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    25
#include <stdint.h>
286
57e6a2006962 convert use of <cassert> to "ns3/assert.h"
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 227
diff changeset
    26
#include "assert.h"
223
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    27
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    28
namespace ns3 {
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    29
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    30
/**
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    31
 * \brief smart pointer class similar to boost::shared_ptr
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    32
 *
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    33
 * This smart-pointer class is supposed to be used to manage
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    34
 * heap-allocated objects: when it decides it does not need
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    35
 * the object it references, it invokes operator delete on it.
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    36
 * This implementation allows you to manipulate the smart pointer
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    37
 * as if it was a normal pointer: you can compare it with zero,
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    38
 * compare it against other pointers, etc. However, the only 
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    39
 * operation we are careful to avoid is the conversion back to
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    40
 * raw pointers: if you need to convert back, you need to invoke
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    41
 * the Ptr<T>::Remove method which returns a raw pointer and
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    42
 * makes the smart pointer forget about the raw pointer.
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    43
 */
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    44
template <typename T>
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    45
class Ptr 
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    46
{
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    47
private:
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    48
  T *m_ptr;
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    49
  class Tester {
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    50
  private:
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    51
    void operator delete (void *);
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    52
  };
225
dad23ccd9e6c more tests, fix bugs uncovered by tests
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 223
diff changeset
    53
  friend class Ptr<const T>;
567
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
    54
  void Acquire (void) const;
223
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    55
public:
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    56
  /**
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    57
   * Create an empty smart pointer
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    58
   */
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    59
  Ptr ();
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    60
  /**
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    61
   * \param ptr raw pointer to manage
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    62
   *
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    63
   * Create a smart pointer which points to the
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    64
   * input raw pointer. This method takes ownershipt
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    65
   * of the input raw pointer. That is, the smart pointer
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    66
   * becomes responsible for calling delete on the
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    67
   * raw pointer when needed.
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    68
   */
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    69
  Ptr (T *ptr);
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    70
  Ptr (Ptr const&o);
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    71
  // allow conversions from T to T const.
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    72
  template <typename U>
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    73
  Ptr (Ptr<U> const &o);
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    74
  ~Ptr () ;
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    75
  Ptr<T> &operator = (Ptr const& o);
544
cbc4158d47c9 remove ptr::Remove, make ptr::Peek share the same signature as ptr::Get
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 543
diff changeset
    76
cbc4158d47c9 remove ptr::Remove, make ptr::Peek share the same signature as ptr::Get
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 543
diff changeset
    77
  /**
cbc4158d47c9 remove ptr::Remove, make ptr::Peek share the same signature as ptr::Get
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 543
diff changeset
    78
   * \return the pointer managed by this smart pointer.
cbc4158d47c9 remove ptr::Remove, make ptr::Peek share the same signature as ptr::Get
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 543
diff changeset
    79
   *
cbc4158d47c9 remove ptr::Remove, make ptr::Peek share the same signature as ptr::Get
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 543
diff changeset
    80
   * The underlying refcount is not incremented prior
cbc4158d47c9 remove ptr::Remove, make ptr::Peek share the same signature as ptr::Get
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 543
diff changeset
    81
   * to returning to the caller so the caller is not
cbc4158d47c9 remove ptr::Remove, make ptr::Peek share the same signature as ptr::Get
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 543
diff changeset
    82
   * responsible for calling Unref himself.
cbc4158d47c9 remove ptr::Remove, make ptr::Peek share the same signature as ptr::Get
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 543
diff changeset
    83
   */
cbc4158d47c9 remove ptr::Remove, make ptr::Peek share the same signature as ptr::Get
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 543
diff changeset
    84
  T * Peek () const;
cbc4158d47c9 remove ptr::Remove, make ptr::Peek share the same signature as ptr::Get
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 543
diff changeset
    85
cbc4158d47c9 remove ptr::Remove, make ptr::Peek share the same signature as ptr::Get
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 543
diff changeset
    86
  /**
cbc4158d47c9 remove ptr::Remove, make ptr::Peek share the same signature as ptr::Get
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 543
diff changeset
    87
   * \return the pointer managed by this smart pointer.
cbc4158d47c9 remove ptr::Remove, make ptr::Peek share the same signature as ptr::Get
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 543
diff changeset
    88
   *
cbc4158d47c9 remove ptr::Remove, make ptr::Peek share the same signature as ptr::Get
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 543
diff changeset
    89
   * The underlying refcount is incremented prior
cbc4158d47c9 remove ptr::Remove, make ptr::Peek share the same signature as ptr::Get
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 543
diff changeset
    90
   * to returning to the caller so the caller is
cbc4158d47c9 remove ptr::Remove, make ptr::Peek share the same signature as ptr::Get
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 543
diff changeset
    91
   * responsible for calling Unref himself.
cbc4158d47c9 remove ptr::Remove, make ptr::Peek share the same signature as ptr::Get
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 543
diff changeset
    92
   */
543
a730800a31d5 Node* -> Ptr<Node>
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 542
diff changeset
    93
  T * Get () const;
223
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    94
  T *operator -> () const;
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    95
  T *operator -> ();
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    96
  // allow if (!sp)
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    97
  bool operator! ();
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    98
  // allow if (sp)
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    99
  operator Tester * () const;
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   100
  // allow if (sp == 0)
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   101
  template <typename T1, typename T2>
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   102
  inline friend bool operator == (Ptr<T1> const &lhs, T2 const *rhs);
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   103
  // allow if (0 == sp)
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   104
  template <typename T1, typename T2>
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   105
  inline friend bool operator == (T1 const *lhs, Ptr<T2> &rhs);
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   106
  // allow if (sp != 0)
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   107
  template <typename T1, typename T2>
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   108
  inline friend bool operator != (Ptr<T1> const &lhs, T2 const *rhs);
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   109
  // allow if (0 != sp)
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   110
  template <typename T1, typename T2>
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   111
  inline friend bool operator != (T1 const *lhs, Ptr<T2> &rhs);
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   112
567
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   113
  // allow if (sp0 == sp1)
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   114
  template <typename T1, typename T2>
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   115
  inline friend bool operator == (Ptr<T1> const &lhs, Ptr<T2> const &rhs);
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   116
  // allow if (sp0 != sp1)
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   117
  template <typename T1, typename T2>
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   118
  inline friend bool operator != (Ptr<T1> const &lhs, Ptr<T2> const &rhs);
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   119
225
dad23ccd9e6c more tests, fix bugs uncovered by tests
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 223
diff changeset
   120
  template <typename T1, typename T2>
dad23ccd9e6c more tests, fix bugs uncovered by tests
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 223
diff changeset
   121
  inline friend Ptr<T1> const_pointer_cast (Ptr<T2> const&p);
dad23ccd9e6c more tests, fix bugs uncovered by tests
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 223
diff changeset
   122
dad23ccd9e6c more tests, fix bugs uncovered by tests
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 223
diff changeset
   123
223
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   124
};
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   125
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   126
template <typename T>
567
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   127
void 
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   128
Ptr<T>::Acquire (void) const
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   129
{
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   130
  if (m_ptr != 0)
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   131
    {
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   132
      m_ptr->Ref ();
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   133
    }  
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   134
}
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   135
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   136
template <typename T>
223
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   137
Ptr<T>::Ptr ()
542
00722b9a01b3 Ptr changed to use Object's refcounts
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 432
diff changeset
   138
  : m_ptr (0)
223
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   139
{}
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   140
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   141
template <typename T>
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   142
Ptr<T>::Ptr (T *ptr) 
542
00722b9a01b3 Ptr changed to use Object's refcounts
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 432
diff changeset
   143
  : m_ptr (ptr)
567
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   144
{
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   145
  Acquire ();
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   146
}
223
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   147
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   148
template <typename T>
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   149
Ptr<T>::Ptr (Ptr const&o) 
567
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   150
  : m_ptr (o.Peek ())
223
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   151
{
567
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   152
  Acquire ();
223
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   153
}
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   154
template <typename T>
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   155
template <typename U>
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   156
Ptr<T>::Ptr (Ptr<U> const &o)
567
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   157
  : m_ptr (o.Peek ())
223
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   158
{
567
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   159
  Acquire ();
223
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   160
}
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   161
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   162
template <typename T>
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   163
Ptr<T>::~Ptr () 
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   164
{
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   165
  if (m_ptr != 0) 
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   166
    {
542
00722b9a01b3 Ptr changed to use Object's refcounts
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 432
diff changeset
   167
      m_ptr->Unref();
223
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   168
    }
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   169
}
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   170
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   171
template <typename T>
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   172
Ptr<T> &
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   173
Ptr<T>::operator = (Ptr const& o) 
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   174
{
426
7d0bde915fd6 Fix Ptr<T>::m_count memory leak in some places.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 286
diff changeset
   175
  if (&o == this)
567
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   176
    {
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   177
      return *this;
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   178
    }
223
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   179
  if (m_ptr != 0) 
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   180
    {
542
00722b9a01b3 Ptr changed to use Object's refcounts
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 432
diff changeset
   181
      m_ptr->Unref();
223
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   182
    }
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   183
  m_ptr = o.m_ptr;
567
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   184
  Acquire ();
223
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   185
  return *this;
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   186
}
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   187
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   188
template <typename T>
544
cbc4158d47c9 remove ptr::Remove, make ptr::Peek share the same signature as ptr::Get
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 543
diff changeset
   189
T *
543
a730800a31d5 Node* -> Ptr<Node>
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 542
diff changeset
   190
Ptr<T>::Peek () const
227
482a61824ef4 add ns3::Ptr<T>::operator * with a few tests
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 225
diff changeset
   191
{
544
cbc4158d47c9 remove ptr::Remove, make ptr::Peek share the same signature as ptr::Get
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 543
diff changeset
   192
  return m_ptr;
227
482a61824ef4 add ns3::Ptr<T>::operator * with a few tests
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 225
diff changeset
   193
}
482a61824ef4 add ns3::Ptr<T>::operator * with a few tests
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 225
diff changeset
   194
482a61824ef4 add ns3::Ptr<T>::operator * with a few tests
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 225
diff changeset
   195
template <typename T>
543
a730800a31d5 Node* -> Ptr<Node>
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 542
diff changeset
   196
T * 
a730800a31d5 Node* -> Ptr<Node>
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 542
diff changeset
   197
Ptr<T>::Get () const
a730800a31d5 Node* -> Ptr<Node>
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 542
diff changeset
   198
{
567
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   199
  Acquire ();
543
a730800a31d5 Node* -> Ptr<Node>
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 542
diff changeset
   200
  return m_ptr;
a730800a31d5 Node* -> Ptr<Node>
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 542
diff changeset
   201
}
a730800a31d5 Node* -> Ptr<Node>
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 542
diff changeset
   202
a730800a31d5 Node* -> Ptr<Node>
Raj Bhattacharjea <raj.b@gatech.edu>
parents: 542
diff changeset
   203
template <typename T>
223
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   204
T *
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   205
Ptr<T>::operator -> () 
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   206
{
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   207
  return m_ptr;
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   208
}
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   209
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   210
template <typename T>
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   211
T *
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   212
Ptr<T>::operator -> () const
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   213
{
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   214
  return m_ptr;
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   215
}
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   216
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   217
template <typename T>
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   218
bool 
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   219
Ptr<T>::operator! () 
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   220
{
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   221
  return m_ptr == 0;
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   222
}
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   223
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   224
template <typename T>
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   225
Ptr<T>::operator Tester * () const
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   226
{
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   227
  if (m_ptr == 0) 
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   228
    {
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   229
      return 0;
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   230
    }
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   231
  static Tester test;
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   232
  return &test;
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   233
}
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   234
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   235
// non-member friend functions.
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   236
template <typename T1, typename T2>
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   237
bool 
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   238
operator == (Ptr<T1> const &lhs, T2 const *rhs)
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   239
{
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   240
  return lhs.m_ptr == rhs;
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   241
}
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   242
template <typename T1, typename T2>
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   243
bool 
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   244
operator == (T1 const *lhs, Ptr<T2> &rhs)
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   245
{
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   246
  return lhs == rhs.m_ptr;
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   247
}
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   248
template <typename T1, typename T2>
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   249
bool 
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   250
operator != (Ptr<T1> const &lhs, T2 const *rhs)
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   251
{
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   252
  return lhs.m_ptr != rhs;
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   253
}
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   254
template <typename T1, typename T2>
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   255
bool 
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   256
operator != (T1 const *lhs, Ptr<T2> &rhs)
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   257
{
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   258
  return lhs != rhs.m_ptr;
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   259
}
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   260
225
dad23ccd9e6c more tests, fix bugs uncovered by tests
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 223
diff changeset
   261
template <typename T1, typename T2>
567
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   262
bool 
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   263
operator == (Ptr<T1> const &lhs, Ptr<T2> const &rhs)
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   264
{
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   265
  return lhs.Get () == rhs.Get ();
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   266
}
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   267
template <typename T1, typename T2>
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   268
bool 
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   269
operator != (Ptr<T1> const &lhs, Ptr<T2> const &rhs)
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   270
{
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   271
  return lhs.Get () != rhs.Get ();
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   272
}
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   273
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   274
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 544
diff changeset
   275
template <typename T1, typename T2>
225
dad23ccd9e6c more tests, fix bugs uncovered by tests
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 223
diff changeset
   276
Ptr<T1>
dad23ccd9e6c more tests, fix bugs uncovered by tests
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 223
diff changeset
   277
const_pointer_cast (Ptr<T2> const&p)
dad23ccd9e6c more tests, fix bugs uncovered by tests
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 223
diff changeset
   278
{
dad23ccd9e6c more tests, fix bugs uncovered by tests
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 223
diff changeset
   279
  return Ptr<T1> (const_cast<T1 *> (p.m_ptr));
dad23ccd9e6c more tests, fix bugs uncovered by tests
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 223
diff changeset
   280
}
dad23ccd9e6c more tests, fix bugs uncovered by tests
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 223
diff changeset
   281
dad23ccd9e6c more tests, fix bugs uncovered by tests
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 223
diff changeset
   282
223
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   283
}; // namespace ns3
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   284
80f1c6b76999 initial go at smart pointer implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   285
#endif /* PTR_H */