Offer_Iterators.cpp

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

Generated on Thu Nov 9 13:59:57 2006 for TAO_CosTrader by doxygen 1.3.6