BLPAPI C++  3.24.11
blpapi_event.h
Go to the documentation of this file.
1 /* Copyright 2012. Bloomberg Finance L.P.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions: The above
9  * copyright notice and this permission notice shall be included in all copies
10  * or substantial portions of the Software.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18  * IN THE SOFTWARE.
19  */
20 
28 #ifndef INCLUDED_BLPAPI_EVENT
29 #define INCLUDED_BLPAPI_EVENT
30 
113 #include <blpapi_call.h>
114 #include <blpapi_defs.h>
115 #include <blpapi_message.h>
116 
117 #ifdef __cplusplus
118 extern "C" {
119 #endif
120 
122 int blpapi_Event_eventType(const blpapi_Event_t *event);
123 
125 int blpapi_Event_addRef(const blpapi_Event_t *event);
126 
128 int blpapi_Event_release(const blpapi_Event_t *event);
129 
132 
135 
138  blpapi_EventQueue_t *eventQueue, int timeout);
139 
142 
145  blpapi_EventQueue_t *eventQueue, blpapi_Event_t **eventPointer);
146 
149  const blpapi_Event_t *event);
150 
153 
156  blpapi_MessageIterator_t *iterator, blpapi_Message_t **result);
157 
160 
161 #ifdef __cplusplus
162 }
163 
164 #include <cassert>
165 #include <utility>
166 
174 namespace BloombergLP {
175 namespace blpapi {
176 
196 class Event {
197 
198  blpapi_Event_t *d_impl_p;
199 
200  public:
202  enum EventType {
231  UNKNOWN = -1
232  };
233 
243  class iterator {
244  // DATA
245  blpapi_MessageIterator_t *d_impl_p;
246  Message d_currentMessage;
247 
248  public:
249  // TYPES
250  using difference_type = std::ptrdiff_t;
252  using pointer = Message *;
253  using reference = Message&;
254  using iterator_category = std::input_iterator_tag;
255 
256  // CREATORS
257  iterator();
260  explicit iterator(const Event& event);
266  iterator(const iterator& original);
273  ~iterator();
274 
275  // MANIPULATORS
276  iterator& operator=(const iterator& original);
283  iterator& operator++();
290  iterator operator++(int);
299  void swap(iterator& other);
304  // ACCESSORS
305  const Message& operator*() const noexcept;
306  const Message *operator->() const noexcept;
307 
308  // FRIENDS
309  friend bool operator==(const iterator& lhs, const iterator& rhs);
310  };
311 
312  Event();
319  Event(blpapi_Event_t *handle);
320 
321  Event(const Event& original);
328  ~Event();
335  Event& operator=(const Event& rhs);
342  EventType eventType() const;
347  bool isValid() const;
352  blpapi_Event_t *impl() const;
353 
354  iterator begin() const;
355  iterator end() const;
356 };
357 
358 // FREE OPERATORS
359 bool operator==(const Event::iterator& lhs, const Event::iterator& rhs);
365 bool operator!=(const Event::iterator& lhs, const Event::iterator& rhs);
386 class EventQueue {
387 
388  blpapi_EventQueue_t *d_handle_p;
389 
390  public:
391  EventQueue();
396  EventQueue(const EventQueue&) = delete;
397  EventQueue& operator=(const EventQueue&) = delete;
398 
399  virtual ~EventQueue();
405  virtual Event nextEvent(int timeout = 0);
414  virtual int tryNextEvent(Event *event);
422  virtual void purge();
430  blpapi_EventQueue_t *handle() const;
431 };
432 
449 
450  blpapi_MessageIterator_t *d_impl_p;
451  blpapi_Message_t *d_current_p;
452 
453  public:
454  MessageIterator(const MessageIterator&) = delete;
455  MessageIterator& operator=(const MessageIterator&) = delete;
456 
457  explicit MessageIterator(const Event& event);
465  ~MessageIterator();
470  bool next();
479  bool isValid() const;
485  Message message(bool createClonable = false) const;
495 };
496 
497 void swap(Event::iterator& lhs, Event::iterator& rhs);
499 
503 //=============================================================================
504 // INLINE FUNCTION DEFINITIONS
505 //=============================================================================
506 
507 // -----------
508 // class Event
509 // -----------
510 
511 inline Event::Event()
512  : d_impl_p(0)
513 {
514 }
515 
517  : d_impl_p(handle)
518 {
519 }
520 
521 inline Event::Event(const Event& original)
522  : d_impl_p(original.d_impl_p)
523 {
524  if (d_impl_p) {
525  blpapi_Event_addRef(d_impl_p);
526  }
527 }
528 
530 {
531  if (d_impl_p) {
532  blpapi_Event_release(d_impl_p);
533  }
534 }
535 
536 inline Event& Event::operator=(const Event& rhs)
537 {
538  using std::swap;
539 
540  Event tmp(rhs);
541  swap(tmp.d_impl_p, d_impl_p);
542 
543  return *this;
544 }
545 
547 {
548  return (EventType)blpapi_Event_eventType(d_impl_p);
549 }
550 
551 inline bool Event::isValid() const { return d_impl_p ? true : false; }
552 
553 inline blpapi_Event_t *Event::impl() const { return d_impl_p; }
554 
555 inline Event::iterator Event::begin() const { return iterator(*this); }
556 
557 inline Event::iterator Event::end() const { return iterator(); }
558 
559 // ----------------
560 // class EventQueue
561 // ----------------
562 
564 
566 
567 inline Event EventQueue::nextEvent(int timeout)
568 {
569  return Event(blpapi_EventQueue_nextEvent(d_handle_p, timeout));
570 }
571 
572 inline int EventQueue::tryNextEvent(Event *event)
573 {
574  assert(event);
575 
576  blpapi_Event_t *impl = 0;
577  int ret = blpapi_EventQueue_tryNextEvent(d_handle_p, &impl);
578  if (0 == ret) {
579  *event = Event(impl);
580  }
581  return ret;
582 }
583 
584 inline void EventQueue::purge() { blpapi_EventQueue_purge(d_handle_p); }
585 
586 inline blpapi_EventQueue_t *EventQueue::handle() const { return d_handle_p; }
587 
588 // ---------------------
589 // class MessageIterator
590 // ---------------------
591 
593  : d_impl_p(nullptr)
594  , d_current_p(nullptr)
595 {
596  d_impl_p = blpapi_MessageIterator_create(event.impl());
597 }
598 
600 {
602 }
603 
605 {
606  return !blpapi_MessageIterator_next(d_impl_p, &d_current_p);
607 }
608 
609 inline bool MessageIterator::isValid() const
610 {
611  return d_current_p ? true : false;
612 }
613 
614 inline Message MessageIterator::message(bool createClonable) const
615 {
616  if (createClonable) {
617  BLPAPI_CALL_MESSAGE_ADDREF(d_current_p);
618  }
619  return Message(d_current_p, createClonable);
620 }
621 
622 // --------------------------
623 // class Event::iterator
624 // --------------------------
625 
627  : d_impl_p(nullptr)
628  , d_currentMessage(nullptr)
629 {
630 }
631 
632 inline Event::iterator::iterator(const Event& event)
633  : iterator()
634 {
635  if (event.impl()) {
636  d_impl_p = blpapi_MessageIterator_create(event.impl());
637  ++(*this);
638  }
639 }
640 
641 inline Event::iterator::iterator(const iterator& original)
642  : d_impl_p(original.d_impl_p)
643  , d_currentMessage(original.d_currentMessage)
644 {
645  if (d_impl_p) {
647  }
648 }
649 
651 {
653 }
654 
656 {
657  auto copy(original);
658  copy.swap(*this);
659  return *this;
660 }
661 
662 inline const Message& Event::iterator::operator*() const noexcept
663 {
664  return d_currentMessage;
665 }
666 
667 inline const Message *Event::iterator::operator->() const noexcept
668 {
669  return &d_currentMessage;
670 }
671 
673 {
674  d_currentMessage = Message(nullptr);
675 
676  blpapi_Message_t *nextMessage = nullptr;
677  blpapi_MessageIterator_next(d_impl_p, &nextMessage);
678  d_currentMessage = Message(nextMessage);
679 
680  return *this;
681 }
682 
684 {
685  auto copy = *this;
686  ++(*this);
687  return copy;
688 }
689 
690 inline void Event::iterator::swap(iterator& other)
691 {
692  using std::swap;
693  swap(d_impl_p, other.d_impl_p);
694  swap(d_currentMessage, other.d_currentMessage);
695 }
696 
697 } // close namespace blpapi
698 
699 inline bool blpapi::operator==(
700  Event::iterator const& lhs, Event::iterator const& rhs)
701 {
702  if (&lhs == &rhs) {
703  return true;
704  }
705 
706  if (lhs.d_impl_p == rhs.d_impl_p
707  && lhs.d_currentMessage.impl() == rhs.d_currentMessage.impl()) {
708  return true;
709  }
710 
711  const bool lend = !lhs.d_impl_p || !lhs.d_currentMessage.impl();
712  const bool rend = !rhs.d_impl_p || !rhs.d_currentMessage.impl();
713 
714  return lend && rend;
715 }
716 
717 inline bool blpapi::operator!=(
718  Event::iterator const& lhs, Event::iterator const& rhs)
719 {
720  return !(lhs == rhs);
721 }
722 
724 {
725  lhs.swap(rhs);
726 }
727 
728 } // close namespace BloombergLP
729 
730 #endif // #ifdef __cplusplus
731 #endif // #ifndef INCLUDED_BLPAPI_EVENT
#define BLPAPI_EVENTTYPE_TOKEN_STATUS
Definition: blpapi_defs.h:93
Status updates for a request.
Definition: blpapi_event.h:209
struct blpapi_Message blpapi_Message_t
Definition: blpapi_dispatchtbl.h:78
Definition: blpapi_message.h:161
#define BLPAPI_EVENTTYPE_RESOLUTION_STATUS
Definition: blpapi_defs.h:91
iterator end() const
Definition: blpapi_event.h:557
Status updates for user authorization.
Definition: blpapi_event.h:221
bool operator!=(const CorrelationId &lhs, const CorrelationId &rhs)
Definition: blpapi_correlationid.h:582
const Message & operator *() const noexcept
Definition: blpapi_event.h:662
Status updates for a subscription.
Definition: blpapi_event.h:207
int blpapi_Event_eventType(const blpapi_Event_t *event)
int blpapi_EventQueue_purge(blpapi_EventQueue_t *eventQueue)
#define BLPAPI_EVENTTYPE_RESPONSE
Definition: blpapi_defs.h:85
Common definitions used by the library.
bool operator==(const CorrelationId &lhs, const CorrelationId &rhs)
Definition: blpapi_correlationid.h:562
void swap(Event::iterator &lhs, Event::iterator &rhs)
Swap the contents of the lhs and rhs iterators.
Definition: blpapi_event.h:723
struct blpapi_Event blpapi_Event_t
Definition: blpapi_types.h:139
blpapi_EventQueue_t * handle() const
Definition: blpapi_event.h:586
blpapi_EventQueue_t * blpapi_EventQueue_create(void)
void blpapi_MessageIterator_destroy(blpapi_MessageIterator_t *iterator)
Status updates about topics for service providers.
Definition: blpapi_event.h:225
#define BLPAPI_EVENTTYPE_SESSION_STATUS
Definition: blpapi_defs.h:82
Definition: blpapi_abstractsession.h:187
std::ptrdiff_t difference_type
Definition: blpapi_event.h:250
#define BLPAPI_EVENTTYPE_TIMEOUT
Definition: blpapi_defs.h:89
const Message * operator->() const noexcept
Definition: blpapi_event.h:667
#define BLPAPI_CALL(FUNCNAME)
Definition: blpapi_call.h:353
Event & operator=(const Event &rhs)
Definition: blpapi_event.h:536
iterator & operator=(const iterator &original)
Definition: blpapi_event.h:655
Data updates resulting from a subscription.
Definition: blpapi_event.h:215
#define BLPAPI_EVENTTYPE_ADMIN
Definition: blpapi_defs.h:81
The final (possibly only) response to a request.
Definition: blpapi_event.h:211
virtual Event nextEvent(int timeout=0)
Definition: blpapi_event.h:567
Definition: blpapi_event.h:243
bool isValid() const
Definition: blpapi_event.h:609
#define BLPAPI_EVENTTYPE_AUTHORIZATION_STATUS
Definition: blpapi_defs.h:90
Definition: blpapi_event.h:386
Status updates for a generate token request.
Definition: blpapi_event.h:227
Status updates for a service.
Definition: blpapi_event.h:217
virtual int tryNextEvent(Event *event)
Definition: blpapi_event.h:572
struct blpapi_EventQueue blpapi_EventQueue_t
Definition: blpapi_types.h:148
EventType
The possible types of event.
Definition: blpapi_event.h:202
EventQueue()
Definition: blpapi_event.h:563
#define BLPAPI_EVENTTYPE_SUBSCRIPTION_DATA
Definition: blpapi_defs.h:87
Status updates for a resolution operation.
Definition: blpapi_event.h:223
bool next()
Definition: blpapi_event.h:604
int blpapi_MessageIterator_addRef(const blpapi_MessageIterator_t *iterator)
iterator & operator++()
Definition: blpapi_event.h:672
EventType eventType() const
Definition: blpapi_event.h:546
#define BLPAPI_EVENTTYPE_TOPIC_STATUS
Definition: blpapi_defs.h:92
~MessageIterator()
Definition: blpapi_event.h:599
struct blpapi_MessageIterator blpapi_MessageIterator_t
Definition: blpapi_types.h:151
~Event()
Definition: blpapi_event.h:529
int blpapi_MessageIterator_next(blpapi_MessageIterator_t *iterator, blpapi_Message_t **result)
Admin event.
Definition: blpapi_event.h:203
Defines a message containing elements.
#define BLPAPI_EVENTTYPE_PARTIAL_RESPONSE
Definition: blpapi_defs.h:86
Event()
Definition: blpapi_event.h:511
iterator()
Definition: blpapi_event.h:626
void swap(iterator &other)
Definition: blpapi_event.h:690
iterator begin() const
Definition: blpapi_event.h:555
#define BLPAPI_EVENTTYPE_REQUEST_STATUS
Definition: blpapi_defs.h:84
virtual void purge()
Definition: blpapi_event.h:584
A partial response to a request.
Definition: blpapi_event.h:213
Request event. Provided to ProviderSession only.
Definition: blpapi_event.h:229
bool isValid() const
Definition: blpapi_event.h:551
Definition: blpapi_event.h:196
#define BLPAPI_EVENTTYPE_SERVICE_STATUS
Definition: blpapi_defs.h:88
blpapi_MessageIterator_t * blpapi_MessageIterator_create(const blpapi_Event_t *event)
Definition: blpapi_event.h:448
#define BLPAPI_EXPORT
Definition: blpapi_defs.h:171
std::input_iterator_tag iterator_category
Definition: blpapi_event.h:254
Provide functions for dispatchtbl.
blpapi_Event_t * blpapi_EventQueue_nextEvent(blpapi_EventQueue_t *eventQueue, int timeout)
int blpapi_EventQueue_destroy(blpapi_EventQueue_t *eventQueue)
#define BLPAPI_CALL_MESSAGE_ADDREF(a1)
Definition: blpapi_call.h:360
blpapi_Event_t * impl() const
Definition: blpapi_event.h:553
~iterator()
Definition: blpapi_event.h:650
int blpapi_EventQueue_tryNextEvent(blpapi_EventQueue_t *eventQueue, blpapi_Event_t **eventPointer)
#define BLPAPI_EVENTTYPE_SUBSCRIPTION_STATUS
Definition: blpapi_defs.h:83
An Event returned from Session::nextEvent() if it timed out.
Definition: blpapi_event.h:219
#define BLPAPI_EVENTTYPE_REQUEST
Definition: blpapi_defs.h:94
int blpapi_Event_addRef(const blpapi_Event_t *event)
Message message(bool createClonable=false) const
Definition: blpapi_event.h:614
MessageIterator(const MessageIterator &)=delete
virtual ~EventQueue()
Definition: blpapi_event.h:565
Status updates for a session.
Definition: blpapi_event.h:205
Definition: blpapi_event.h:231
int blpapi_Event_release(const blpapi_Event_t *event)