Merge branch 'master' into jwi-bcc64xsingletonwarning
[ACE_TAO.git] / ACE / examples / Smart_Pointers / Gadget_Part_Impl.cpp
blobcee3b09e548194ff7f50180682b5b764613e320e
1 /* -*- C++ -*- */
2 //=============================================================================
3 /**
4 * @file Gadget_Part_Impl.cpp
6 * @author Christopher Kohlhoff <chris@kohlhoff.com>
7 */
8 //=============================================================================
10 #include "Gadget_Part_Impl.h"
11 #include "ace/ACE.h"
12 #include "ace/Log_Msg.h"
13 #include "ace/Unbounded_Queue.h"
15 Gadget_Part_Impl::Gadget_Part_Impl (Gadget_ptr owner,
16 const char* name,
17 int size)
18 : owner_ (owner),
19 name_ (ACE::strnew (name)),
20 size_ (size)
22 ACE_DEBUG ((LM_DEBUG, "Gadget_Part_Impl constructor\n"));
25 Gadget_Part_Impl::~Gadget_Part_Impl ()
27 ACE_DEBUG ((LM_DEBUG, "Gadget_Part_Impl destructor\n"));
29 delete [] name_;
32 void Gadget_Part_Impl::print_info ()
34 ACE_DEBUG ((LM_INFO, "Gadget part: name=%s size=%d\n", name_, size_));
37 void Gadget_Part_Impl::remove_from_owner ()
39 // Need to guarantee the existence of the owner for the duration of this call.
40 Gadget_var owner = owner_;
42 // Weak pointers are automatically set to NULL if the object they refer to
43 // is deleted. We can use this fact to check that our owner still exists.
44 if (owner == 0)
45 return;
47 // Take all existing parts from the owner and build up a temporary list. If
48 // we find ourselves then we won't add ourselves to the list.
49 ACE_Unbounded_Queue<Gadget_Part_var> parts;
50 for (;;)
52 Gadget_Part_var part = owner->remove_part ();
53 if (part == 0)
54 break;
55 if (part != this)
56 parts.enqueue_tail (part);
59 // Add the remaining parts back to the gadget.
60 while (!parts.is_empty ())
62 Gadget_Part_var part;
63 parts.dequeue_head (part);
64 owner->add_part (part);