summaryrefslogtreecommitdiff
path: root/examples/Smart_Pointers/Gadget_Part_Impl.cpp
blob: aeda00711bb15557366fe40ece6ab24f91377b92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* -*- C++ -*- */
//=============================================================================
/**
 *  @file    Gadget_Part_Impl.cpp
 *
 *  $Id$
 *
 *  @author Christopher Kohlhoff <chris@kohlhoff.com>
 */
//=============================================================================

#include "Gadget_Part_Impl.h"
#include "ace/ACE.h"
#include "ace/Log_Msg.h"
#include "ace/Unbounded_Queue.h"

Gadget_Part_Impl::Gadget_Part_Impl (Gadget_ptr owner,
                                    const char* name,
                                    int size)
  : owner_ (owner),
    name_ (ACE::strnew (name)),
    size_ (size)
{
  ACE_DEBUG ((LM_DEBUG, "Gadget_Part_Impl constructor\n"));
}
  
Gadget_Part_Impl::~Gadget_Part_Impl (void)
{
  ACE_DEBUG ((LM_DEBUG, "Gadget_Part_Impl destructor\n"));

  delete [] name_;
}

void Gadget_Part_Impl::print_info (void)
{
  ACE_DEBUG ((LM_INFO, "Gadget part: name=%s size=%d\n", name_, size_));
}

void Gadget_Part_Impl::remove_from_owner (void)
{
  // Need to guarantee the existence of the owner for the duration of this call.
  Gadget_var owner = owner_;

  // Weak pointers are automatically set to NULL if the object they refer to
  // is deleted. We can use this fact to check that our owner still exists.
  if (owner == 0)
    return;

  // Take all existing parts from the owner and build up a temporary list. If
  // we find ourselves then we won't add ourselves to the list.
  ACE_Unbounded_Queue<Gadget_Part_var> parts;
  for (;;)
    {
      Gadget_Part_var part = owner->remove_part ();
      if (part == 0)
        break;
      if (part != this)
        parts.enqueue_tail (part);
    }

  // Add the remaining parts back to the gadget.
  while (!parts.is_empty ())
    {
      Gadget_Part_var part;
      parts.dequeue_head (part);
      owner->add_part (part);
    }
}