summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Trader/Offer_Id_Iterator.cpp
blob: 7ca466e05ccf3ecae6c4f812cd00cd2a56803d8d (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// ============================================================================
// $Id$
// 
// = LIBRARY 
//    orbsvcs
//  
// = FILENAME 
//    Offer_Id_terator.cpp
//
// = AUTHOR 
//    Seth Widoff <sbw1@cs.wustl.edu>
//  
// ============================================================================ 

#include "Offer_Id_Iterator.h"

TAO_Offer_Id_Iterator::TAO_Offer_Id_Iterator(void)
{
}

TAO_Offer_Id_Iterator::~TAO_Offer_Id_Iterator (void)
{
  int items_left = this->ids_.size ();

  for (int i = 0; i < items_left; i++)
    {
      CORBA::string_free (this->ids_.front ());
      this->ids_.pop ();
    }
}

CORBA::ULong
TAO_Offer_Id_Iterator::max_left(CORBA::Environment& env)
  TAO_THROW_SPEC ((CORBA::SystemException,
		  CosTrading::UnknownMaxLeft))
{
  return this->ids_.size ();
}

void
TAO_Offer_Id_Iterator::destroy(CORBA::Environment& _env)
  TAO_THROW_SPEC ((CORBA::SystemException))
{
  // Remove self from POA
  //
  // Note that there is no real error checking here as we can't do
  // much about errors here anyway
  //
  
  TAO_TRY
    {
      PortableServer::POA_var poa = this->_default_POA (TAO_TRY_ENV);
      TAO_CHECK_ENV;
      PortableServer::ObjectId_var id = poa->servant_to_id (this, TAO_TRY_ENV);
      TAO_CHECK_ENV;
      
      poa->deactivate_object (id.in (), TAO_TRY_ENV);
    }
  TAO_CATCHANY
    {      
    }
  TAO_ENDTRY;
  
  delete this;  
}

CORBA::Boolean
TAO_Offer_Id_Iterator::next_n(CORBA::ULong n,
			      CosTrading::OfferIdSeq_out _ids,
			      CORBA::Environment& _env)
  TAO_THROW_SPEC ((CORBA::SystemException))
{
  // Calculate the number of Ids to be returned in this . 
  int items_left = this->ids_.size(),
    difference = items_left - n,
    returnable_items = (difference >= 0) ? n : items_left;

  if (returnable_items == 0)
    _ids = new CosTrading::OfferIdSeq;
  else
    {
      // Allocate space for the returned OfferIds.
      CosTrading::OfferId* id_buf =
	CosTrading::OfferIdSeq::allocbuf (returnable_items);
      
      // Copy in those ids!
      for (int i = 0; i < returnable_items; i++)
	{
	  char* offer_id = this->ids_.front ();
	  id_buf[i] = offer_id;
	  this->ids_.pop ();
	}
      
      // Place them into an OfferIdSeq.
      _ids = new CosTrading::OfferIdSeq (returnable_items,
					 returnable_items,
					 id_buf,
					 1);
    }

  // Return true only if there are items left to be returned in
  // subsequent calls.
  return (CORBA::Boolean)(difference > 0);
}

void
TAO_Offer_Id_Iterator::insert_id(const char* new_id)
{
  this->ids_.push (CORBA::string_dup (new_id));
}