Offer_Iterators.cpp

Go to the documentation of this file.
00001 // $Id: Offer_Iterators.cpp 77001 2007-02-12 07:54:49Z johnnyw $
00002 
00003 #include "orbsvcs/Trader/Offer_Iterators.h"
00004 
00005 ACE_RCSID(Trader, Offer_Iterators, "$Id: Offer_Iterators.cpp 77001 2007-02-12 07:54:49Z johnnyw $")
00006 
00007 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00008 
00009 TAO_Offer_Iterator::TAO_Offer_Iterator (const TAO_Property_Filter& pfilter)
00010   : pfilter_ (pfilter)
00011 {
00012 }
00013 
00014 TAO_Offer_Iterator::~TAO_Offer_Iterator (void)
00015 {
00016 }
00017 
00018 void
00019 TAO_Offer_Iterator::destroy (void)
00020 {
00021   // Remove self from POA
00022 
00023   PortableServer::POA_var poa =
00024     this->_default_POA ();
00025 
00026   PortableServer::ObjectId_var id =
00027     poa->servant_to_id (this);
00028 
00029   poa->deactivate_object (id.in ());
00030 }
00031 
00032 TAO_Query_Only_Offer_Iterator::
00033 TAO_Query_Only_Offer_Iterator(const TAO_Property_Filter& pfilter)
00034   : TAO_Offer_Iterator (pfilter)
00035 {
00036 }
00037 
00038 TAO_Query_Only_Offer_Iterator::~TAO_Query_Only_Offer_Iterator(void)
00039 {
00040 }
00041 
00042 void
00043 TAO_Query_Only_Offer_Iterator::add_offer (CosTrading::OfferId offer_id,
00044                                           const CosTrading::Offer* offer)
00045 {
00046   this->offers_.enqueue_tail ((CosTrading::Offer*) offer);
00047   CORBA::string_free (offer_id);
00048 }
00049 
00050 CORBA::ULong
00051 TAO_Query_Only_Offer_Iterator::max_left (void)
00052 {
00053   return static_cast<CORBA::ULong> (this->offers_.size ());
00054 }
00055 
00056 CORBA::Boolean
00057 TAO_Query_Only_Offer_Iterator::next_n (CORBA::ULong n,
00058                                        CosTrading::OfferSeq_out offers)
00059 {
00060   offers = new CosTrading::OfferSeq;
00061 
00062   CORBA::ULong sequence_size = static_cast<CORBA::ULong> (this->offers_.size ());
00063   CORBA::ULong offers_in_sequence = (n < sequence_size) ? n : sequence_size;
00064   offers->length (offers_in_sequence);
00065 
00066   // populate the sequence.
00067   for (CORBA::ULong i = 0; i < offers_in_sequence; i++)
00068     {
00069 
00070       CosTrading::Offer *source = 0;
00071       this->offers_.dequeue_head (source);
00072       this->pfilter_.filter_offer (source, offers[i]);
00073     }
00074 
00075   return offers_in_sequence != 0;
00076 }
00077 
00078 TAO_Offer_Iterator_Collection::TAO_Offer_Iterator_Collection (void)
00079 {
00080 }
00081 
00082 TAO_Offer_Iterator_Collection::~TAO_Offer_Iterator_Collection (void)
00083 {
00084   while (! this->iters_.is_empty ())
00085     {
00086       CosTrading::OfferIterator* offer_iter = 0;
00087       this->iters_.dequeue_head (offer_iter);
00088 
00089       try
00090         {
00091           offer_iter->destroy ();
00092 
00093           CORBA::release (offer_iter);
00094         }
00095       catch (const CORBA::Exception&)
00096         {
00097           // Don't let the exceptions propagate since we're in a
00098           // destructor!
00099         }
00100     }
00101 }
00102 
00103 void
00104 TAO_Offer_Iterator_Collection::
00105 add_offer_iterator (CosTrading::OfferIterator_ptr offer_iter)
00106 {
00107   if (! CORBA::is_nil (offer_iter))
00108     this->iters_.enqueue_tail (offer_iter);
00109 }
00110 
00111 CORBA::Boolean
00112 TAO_Offer_Iterator_Collection::next_n (CORBA::ULong n,
00113                                        CosTrading::OfferSeq_out offers)
00114 {
00115   CORBA::ULong offers_left = n;
00116   CORBA::Boolean return_value = 1;
00117   CosTrading::OfferSeq_var out_offers;
00118 
00119   ACE_NEW_THROW_EX (offers,
00120                     CosTrading::OfferSeq,
00121                     CORBA::NO_MEMORY ());
00122 
00123   while (offers_left > 0 && ! this->iters_.is_empty ())
00124     {
00125       CORBA::ULong offset = 0;
00126       CORBA::Boolean any_left = 0;
00127       CosTrading::OfferIterator* iter =  0;
00128       this->iters_.dequeue_head (iter);
00129 
00130       // Determine how many offers we should retrieve from this
00131       // iterator.
00132 
00133       // Retrieve the set of offers.
00134       any_left =
00135         iter->next_n (offers_left,
00136                       CosTrading::OfferSeq_out (out_offers.out ()));
00137 
00138       // If we've exhausted this iterator, destroy it.
00139       if (any_left == 0)
00140         {
00141           iter->destroy ();
00142           CORBA::release (iter);
00143         }
00144       else
00145         this->iters_.enqueue_head (iter);
00146 
00147       // Merge it with the passed set.
00148       offset = offers->length ();
00149       offers->length (out_offers->length () + offset);
00150       for (CORBA::ULong j = out_offers->length (); j > 0; j--)
00151         offers[j + offset - 1] = out_offers[j - 1];
00152 
00153       offers_left -= out_offers->length ();
00154     }
00155 
00156   // Determine if we have anything left to offer.
00157   if (this->iters_.is_empty ())
00158     return_value = 0;
00159 
00160   return return_value;
00161 }
00162 
00163 void
00164 TAO_Offer_Iterator_Collection::destroy (void)
00165 {
00166   // Destroy all iterators in the collection.
00167   for (Offer_Iters::ITERATOR iters_iter (this->iters_);
00168        ! iters_iter.done ();
00169        iters_iter.advance ())
00170     {
00171       CosTrading::OfferIterator** iter = 0;
00172 
00173       iters_iter.next (iter);
00174       (*iter)->destroy ();
00175     }
00176 
00177   // Remove self from POA
00178 
00179   PortableServer::POA_var poa =
00180     this->_default_POA ();
00181 
00182   PortableServer::ObjectId_var id =
00183     poa->servant_to_id (this);
00184 
00185   poa->deactivate_object (id.in ());
00186 }
00187 
00188 CORBA::ULong
00189 TAO_Offer_Iterator_Collection::max_left (void)
00190 {
00191   throw CosTrading::UnknownMaxLeft();
00192 }
00193 
00194 TAO_Offer_Id_Iterator::TAO_Offer_Id_Iterator (void)
00195 {
00196 }
00197 
00198 TAO_Offer_Id_Iterator::~TAO_Offer_Id_Iterator (void)
00199 {
00200   int return_value = 0;
00201 
00202   do
00203     {
00204       CosTrading::OfferId offer_id = 0;
00205 
00206       return_value = this->ids_.dequeue_head (offer_id);
00207       if (return_value == 0)
00208         CORBA::string_free (offer_id);
00209     }
00210   while (return_value == 0);
00211 }
00212 
00213 CORBA::ULong
00214 TAO_Offer_Id_Iterator::max_left (void)
00215 {
00216   return static_cast<CORBA::ULong> (this->ids_.size ());
00217 }
00218 
00219 void
00220 TAO_Offer_Id_Iterator::destroy (void)
00221 {
00222   // Remove self from POA
00223 
00224   PortableServer::POA_var poa =
00225     this->_default_POA ();
00226 
00227   PortableServer::ObjectId_var id =
00228     poa->servant_to_id (this);
00229 
00230   poa->deactivate_object (id.in ());
00231 }
00232 
00233 CORBA::Boolean
00234 TAO_Offer_Id_Iterator::next_n (CORBA::ULong n,
00235                                CosTrading::OfferIdSeq_out _ids)
00236 {
00237   // Calculate the number of Ids to be returned in this.
00238   CORBA::ULong items_left = static_cast<CORBA::ULong> (this->ids_.size());
00239   int difference = items_left - n;
00240   CORBA::ULong returnable_items = (difference >= 0) ? n : items_left;
00241   CORBA::Boolean return_value = (CORBA::Boolean) (difference > 0);
00242 
00243   if (returnable_items == 0)
00244     ACE_NEW_RETURN (_ids,
00245                     CosTrading::OfferIdSeq,
00246                     return_value);
00247   else
00248     {
00249       // Allocate space for the returned OfferIds.
00250       CosTrading::OfferId* id_buf =
00251         CosTrading::OfferIdSeq::allocbuf (returnable_items);
00252 
00253       if (id_buf != 0)
00254         {
00255           // Copy in those ids!
00256           for (CORBA::ULong i = 0; i < returnable_items; i++)
00257             {
00258               CosTrading::OfferId offer_id = 0;
00259 
00260               this->ids_.dequeue_head (offer_id);
00261               id_buf[i] = offer_id;
00262             }
00263 
00264           // Place them into an OfferIdSeq.
00265           ACE_NEW_RETURN (_ids,
00266                           CosTrading::OfferIdSeq (returnable_items,
00267                                                   returnable_items,
00268                                                   id_buf,
00269                                                   1),
00270                           return_value);
00271         }
00272       else
00273         ACE_NEW_RETURN (_ids,
00274                         CosTrading::OfferIdSeq,
00275                         return_value);
00276     }
00277 
00278   // Return true only if there are items left to be returned in
00279   // subsequent calls.
00280   return return_value;
00281 }
00282 
00283 void
00284 TAO_Offer_Id_Iterator::insert_id (CosTrading::OfferId new_id)
00285 {
00286   this->ids_.enqueue_tail (new_id);
00287 }
00288 
00289 TAO_END_VERSIONED_NAMESPACE_DECL

Generated on Tue Feb 2 17:49:26 2010 for TAO_CosTrader by  doxygen 1.4.7