1.1 --- a/src/core/ptr.cc Thu Oct 01 13:17:24 2009 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,354 +0,0 @@
1.4 -/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
1.5 -/*
1.6 - * Copyright (c) 2005,2006 INRIA
1.7 - *
1.8 - * This program is free software; you can redistribute it and/or modify
1.9 - * it under the terms of the GNU General Public License version 2 as
1.10 - * published by the Free Software Foundation;
1.11 - *
1.12 - * This program is distributed in the hope that it will be useful,
1.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 - * GNU General Public License for more details.
1.16 - *
1.17 - * You should have received a copy of the GNU General Public License
1.18 - * along with this program; if not, write to the Free Software
1.19 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1.20 - *
1.21 - * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
1.22 - */
1.23 -#include "ptr.h"
1.24 -
1.25 -#ifdef RUN_SELF_TESTS
1.26 -
1.27 -#include "test.h"
1.28 -
1.29 -namespace ns3 {
1.30 -
1.31 -class NoCount;
1.32 -
1.33 -template <typename T>
1.34 -void Foo (void) {}
1.35 -
1.36 -class PtrTest : Test
1.37 -{
1.38 -public:
1.39 - PtrTest ();
1.40 - virtual ~PtrTest ();
1.41 - virtual bool RunTests (void);
1.42 - void DestroyNotify (void);
1.43 -private:
1.44 - Ptr<NoCount> CallTest (Ptr<NoCount> p);
1.45 - Ptr<NoCount> const CallTestConst (Ptr<NoCount> const p);
1.46 - uint32_t m_nDestroyed;
1.47 -};
1.48 -
1.49 -
1.50 -class Base
1.51 -{
1.52 -public:
1.53 - Base ();
1.54 - virtual ~Base ();
1.55 - void Ref (void) const;
1.56 - void Unref (void) const;
1.57 -private:
1.58 - mutable uint32_t m_count;
1.59 -};
1.60 -
1.61 -class NoCount : public Base
1.62 -{
1.63 -public:
1.64 - NoCount (PtrTest *test);
1.65 - ~NoCount ();
1.66 - void Nothing (void) const;
1.67 -private:
1.68 - PtrTest *m_test;
1.69 -};
1.70 -
1.71 -Base::Base ()
1.72 - : m_count (1)
1.73 -{}
1.74 -Base::~Base ()
1.75 -{}
1.76 -void
1.77 -Base::Ref (void) const
1.78 -{
1.79 - m_count++;
1.80 -}
1.81 -void
1.82 -Base::Unref (void) const
1.83 -{
1.84 - m_count--;
1.85 - if (m_count == 0)
1.86 - {
1.87 - delete this;
1.88 - }
1.89 -}
1.90 -
1.91 -NoCount::NoCount (PtrTest *test)
1.92 - : m_test (test)
1.93 -{}
1.94 -NoCount::~NoCount ()
1.95 -{
1.96 - m_test->DestroyNotify ();
1.97 -}
1.98 -void
1.99 -NoCount::Nothing () const
1.100 -{}
1.101 -
1.102 -PtrTest::PtrTest ()
1.103 - : Test ("Ptr")
1.104 -{}
1.105 -
1.106 -PtrTest::~PtrTest ()
1.107 -{}
1.108 -
1.109 -void
1.110 -PtrTest::DestroyNotify (void)
1.111 -{
1.112 - m_nDestroyed++;
1.113 -}
1.114 -Ptr<NoCount>
1.115 -PtrTest::CallTest (Ptr<NoCount> p)
1.116 -{
1.117 - return p;
1.118 -}
1.119 -
1.120 -Ptr<NoCount> const
1.121 -PtrTest::CallTestConst (Ptr<NoCount> const p)
1.122 -{
1.123 - return p;
1.124 -}
1.125 -
1.126 -bool
1.127 -PtrTest::RunTests (void)
1.128 -{
1.129 - bool ok = true;
1.130 -
1.131 - m_nDestroyed = false;
1.132 - {
1.133 - Ptr<NoCount> p = Create<NoCount> (this);
1.134 - }
1.135 - if (m_nDestroyed != 1)
1.136 - {
1.137 - ok = false;
1.138 - }
1.139 -
1.140 - m_nDestroyed = 0;
1.141 - {
1.142 - Ptr<NoCount> p;
1.143 - p = Create<NoCount> (this);
1.144 - p = p;
1.145 - }
1.146 - if (m_nDestroyed != 1)
1.147 - {
1.148 - ok = false;
1.149 - }
1.150 -
1.151 - m_nDestroyed = 0;
1.152 - {
1.153 - Ptr<NoCount> p1;
1.154 - p1 = Create<NoCount> (this);
1.155 - Ptr<NoCount> p2 = p1;
1.156 - }
1.157 - if (m_nDestroyed != 1)
1.158 - {
1.159 - ok = false;
1.160 - }
1.161 -
1.162 - m_nDestroyed = 0;
1.163 - {
1.164 - Ptr<NoCount> p1;
1.165 - p1 = Create<NoCount> (this);
1.166 - Ptr<NoCount> p2;
1.167 - p2 = p1;
1.168 - }
1.169 - if (m_nDestroyed != 1)
1.170 - {
1.171 - ok = false;
1.172 - }
1.173 -
1.174 - m_nDestroyed = 0;
1.175 - {
1.176 - Ptr<NoCount> p1;
1.177 - p1 = Create<NoCount> (this);
1.178 - Ptr<NoCount> p2 = Create<NoCount> (this);
1.179 - p2 = p1;
1.180 - }
1.181 - if (m_nDestroyed != 2)
1.182 - {
1.183 - ok = false;
1.184 - }
1.185 -
1.186 - m_nDestroyed = 0;
1.187 - {
1.188 - Ptr<NoCount> p1;
1.189 - p1 = Create<NoCount> (this);
1.190 - Ptr<NoCount> p2;
1.191 - p2 = Create<NoCount> (this);
1.192 - p2 = p1;
1.193 - }
1.194 - if (m_nDestroyed != 2)
1.195 - {
1.196 - ok = false;
1.197 - }
1.198 -
1.199 - m_nDestroyed = 0;
1.200 - {
1.201 - Ptr<NoCount> p1;
1.202 - p1 = Create<NoCount> (this);
1.203 - p1 = Create<NoCount> (this);
1.204 - }
1.205 - if (m_nDestroyed != 2)
1.206 - {
1.207 - ok = false;
1.208 - }
1.209 -
1.210 - m_nDestroyed = 0;
1.211 - {
1.212 - Ptr<NoCount> p1;
1.213 - {
1.214 - Ptr<NoCount> p2;
1.215 - p1 = Create<NoCount> (this);
1.216 - p2 = Create<NoCount> (this);
1.217 - p2 = p1;
1.218 - }
1.219 - if (m_nDestroyed != 1)
1.220 - {
1.221 - ok = false;
1.222 - }
1.223 - }
1.224 - if (m_nDestroyed != 2)
1.225 - {
1.226 - ok = false;
1.227 - }
1.228 -
1.229 - m_nDestroyed = 0;
1.230 - {
1.231 - Ptr<NoCount> p1;
1.232 - {
1.233 - Ptr<NoCount> p2;
1.234 - p1 = Create<NoCount> (this);
1.235 - p2 = Create<NoCount> (this);
1.236 - p2 = CallTest (p1);
1.237 - }
1.238 - if (m_nDestroyed != 1)
1.239 - {
1.240 - ok = false;
1.241 - }
1.242 - }
1.243 - if (m_nDestroyed != 2)
1.244 - {
1.245 - ok = false;
1.246 - }
1.247 -
1.248 - {
1.249 - Ptr<NoCount> p1;
1.250 - Ptr<NoCount> const p2 = CallTest (p1);
1.251 - Ptr<NoCount> const p3 = CallTestConst (p1);
1.252 - Ptr<NoCount> p4 = CallTestConst (p1);
1.253 - Ptr<NoCount const> p5 = p4;
1.254 - //p4 = p5; You cannot make a const pointer be a non-const pointer.
1.255 - // but if you use ConstCast, you can.
1.256 - p4 = ConstCast<NoCount> (p5);
1.257 - p5 = p1;
1.258 - Ptr<NoCount> p;
1.259 - if (p == 0)
1.260 - {}
1.261 - if (p != 0)
1.262 - {}
1.263 - if (0 == p)
1.264 - {}
1.265 - if (0 != p)
1.266 - {}
1.267 - if (p)
1.268 - {}
1.269 - if (!p)
1.270 - {}
1.271 - }
1.272 -
1.273 - m_nDestroyed = 0;
1.274 - {
1.275 - NoCount *raw;
1.276 - {
1.277 - Ptr<NoCount> p = Create<NoCount> (this);
1.278 - {
1.279 - Ptr<NoCount const> p1 = p;
1.280 - }
1.281 - raw = GetPointer (p);
1.282 - p = 0;
1.283 - }
1.284 - if (m_nDestroyed != 0)
1.285 - {
1.286 - ok = false;
1.287 - }
1.288 - delete raw;
1.289 - }
1.290 -
1.291 - m_nDestroyed = 0;
1.292 - {
1.293 - Ptr<NoCount> p = Create<NoCount> (this);
1.294 - const NoCount *v1 = PeekPointer (p);
1.295 - NoCount *v2 = PeekPointer (p);
1.296 - v1->Nothing ();
1.297 - v2->Nothing ();
1.298 - }
1.299 - if (m_nDestroyed != 1)
1.300 - {
1.301 - ok = false;
1.302 - }
1.303 -
1.304 - {
1.305 - Ptr<Base> p0 = Create<NoCount> (this);
1.306 - Ptr<NoCount> p1 = Create<NoCount> (this);
1.307 - if (p0 == p1)
1.308 - {
1.309 - ok = false;
1.310 - }
1.311 - if (p0 != p1)
1.312 - {
1.313 - }
1.314 - else
1.315 - {
1.316 - ok = false;
1.317 - }
1.318 - }
1.319 -#if 0
1.320 - {
1.321 - Ptr<NoCount> p = Create<NoCount> (cb);
1.322 - Callback<void> callback = MakeCallback (&NoCount::Nothing, p);
1.323 - callback ();
1.324 - }
1.325 - {
1.326 - Ptr<const NoCount> p = Create<NoCount> (cb);
1.327 - Callback<void> callback = MakeCallback (&NoCount::Nothing, p);
1.328 - callback ();
1.329 - }
1.330 -#endif
1.331 -
1.332 -#if 0
1.333 - // as expected, fails compilation.
1.334 - {
1.335 - Ptr<const Base> p = Create<NoCount> (cb);
1.336 - Callback<void> callback = MakeCallback (&NoCount::Nothing, p);
1.337 - }
1.338 - // local types are not allowed as arguments to a template.
1.339 - {
1.340 - class B
1.341 - {
1.342 - public:
1.343 - B () {}
1.344 - };
1.345 - Foo<B> ();
1.346 - }
1.347 -#endif
1.348 -
1.349 -
1.350 - return ok;
1.351 -}
1.352 -
1.353 -PtrTest g_ptr_test;
1.354 -
1.355 -}; // namespace ns3
1.356 -
1.357 -#endif /* RUN_SELF_TESTS */