BLPAPI C++  3.22.1
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  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  virtual ~EventQueue();
402  virtual Event nextEvent(int timeout = 0);
411  virtual int tryNextEvent(Event *event);
419  virtual void purge();
427  blpapi_EventQueue_t *handle() const;
428 };
429 
446 
447  blpapi_MessageIterator_t *d_impl_p;
448  blpapi_Message_t *d_current_p;
449 
450  public:
451  MessageIterator(const MessageIterator&) = delete;
452  MessageIterator& operator=(const MessageIterator&) = delete;
453 
454  MessageIterator(const Event& event);
462  ~MessageIterator();
467  bool next();
476  bool isValid() const;
482  Message message(bool createClonable = false) const;
492 };
493 
494 void swap(Event::iterator& lhs, Event::iterator& rhs);
496 
500 //=============================================================================
501 // INLINE FUNCTION DEFINITIONS
502 //=============================================================================
503 
504 // -----------
505 // class Event
506 // -----------
507 
508 inline Event::Event()
509  : d_impl_p(0)
510 {
511 }
512 
514  : d_impl_p(handle)
515 {
516 }
517 
518 inline Event::Event(const Event& original)
519  : d_impl_p(original.d_impl_p)
520 {
521  if (d_impl_p) {
522  blpapi_Event_addRef(d_impl_p);
523  }
524 }
525 
527 {
528  if (d_impl_p) {
529  blpapi_Event_release(d_impl_p);
530  }
531 }
532 
533 inline Event& Event::operator=(const Event& rhs)
534 {
535  using std::swap;
536 
537  Event tmp(rhs);
538  swap(tmp.d_impl_p, d_impl_p);
539 
540  return *this;
541 }
542 
544 {
545  return (EventType)blpapi_Event_eventType(d_impl_p);
546 }
547 
548 inline bool Event::isValid() const { return d_impl_p ? true : false; }
549 
550 inline blpapi_Event_t *Event::impl() const { return d_impl_p; }
551 
552 inline Event::iterator Event::begin() const { return iterator(*this); }
553 
554 inline Event::iterator Event::end() const { return iterator(); }
555 
556 // ----------------
557 // class EventQueue
558 // ----------------
559 
561 
563 
564 inline Event EventQueue::nextEvent(int timeout)
565 {
566  return blpapi_EventQueue_nextEvent(d_handle_p, timeout);
567 }
568 
569 inline int EventQueue::tryNextEvent(Event *event)
570 {
571  assert(event);
572 
573  blpapi_Event_t *impl = 0;
574  int ret = blpapi_EventQueue_tryNextEvent(d_handle_p, &impl);
575  if (0 == ret) {
576  *event = Event(impl);
577  }
578  return ret;
579 }
580 
581 inline void EventQueue::purge() { blpapi_EventQueue_purge(d_handle_p); }
582 
583 inline blpapi_EventQueue_t *EventQueue::handle() const { return d_handle_p; }
584 
585 // ---------------------
586 // class MessageIterator
587 // ---------------------
588 
590  : d_impl_p(nullptr)
591  , d_current_p(nullptr)
592 {
593  d_impl_p = blpapi_MessageIterator_create(event.impl());
594 }
595 
597 {
599 }
600 
602 {
603  return !blpapi_MessageIterator_next(d_impl_p, &d_current_p);
604 }
605 
606 inline bool MessageIterator::isValid() const
607 {
608  return d_current_p ? true : false;
609 }
610 
611 inline Message MessageIterator::message(bool createClonable) const
612 {
613  if (createClonable) {
614  BLPAPI_CALL_MESSAGE_ADDREF(d_current_p);
615  }
616  return Message(d_current_p, createClonable);
617 }
618 
619 // --------------------------
620 // class Event::iterator
621 // --------------------------
622 
624  : d_impl_p(nullptr)
625  , d_currentMessage(nullptr)
626 {
627 }
628 
629 inline Event::iterator::iterator(const Event& event)
630  : iterator()
631 {
632  if (event.impl()) {
633  d_impl_p = blpapi_MessageIterator_create(event.impl());
634  ++(*this);
635  }
636 }
637 
638 inline Event::iterator::iterator(const iterator& original)
639  : d_impl_p(original.d_impl_p)
640  , d_currentMessage(original.d_currentMessage)
641 {
642  if (d_impl_p) {
644  }
645 }
646 
648 {
650 }
651 
653 {
654  auto copy(original);
655  copy.swap(*this);
656  return *this;
657 }
658 
659 inline const Message& Event::iterator::operator*() const noexcept
660 {
661  return d_currentMessage;
662 }
663 
664 inline const Message *Event::iterator::operator->() const noexcept
665 {
666  return &d_currentMessage;
667 }
668 
670 {
671  d_currentMessage = Message(nullptr);
672 
673  blpapi_Message_t *nextMessage = nullptr;
674  blpapi_MessageIterator_next(d_impl_p, &nextMessage);
675  d_currentMessage = Message(nextMessage);
676 
677  return *this;
678 }
679 
681 {
682  auto copy = *this;
683  ++(*this);
684  return copy;
685 }
686 
687 inline void Event::iterator::swap(iterator& other)
688 {
689  using std::swap;
690  swap(d_impl_p, other.d_impl_p);
691  swap(d_currentMessage, other.d_currentMessage);
692 }
693 
694 } // close namespace blpapi
695 
696 inline bool blpapi::operator==(
697  Event::iterator const& lhs, Event::iterator const& rhs)
698 {
699  if (&lhs == &rhs) {
700  return true;
701  }
702 
703  if (lhs.d_impl_p == rhs.d_impl_p
704  && lhs.d_currentMessage.impl() == rhs.d_currentMessage.impl()) {
705  return true;
706  }
707 
708  const bool lend = !lhs.d_impl_p || !lhs.d_currentMessage.impl();
709  const bool rend = !rhs.d_impl_p || !rhs.d_currentMessage.impl();
710 
711  return lend && rend;
712 }
713 
714 inline bool blpapi::operator!=(
715  Event::iterator const& lhs, Event::iterator const& rhs)
716 {
717  return !(lhs == rhs);
718 }
719 
721 {
722  lhs.swap(rhs);
723 }
724 
725 } // close namespace BloombergLP
726 
727 #endif // #ifdef __cplusplus
728 #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
Definition: blpapi_message.h:159
#define BLPAPI_EVENTTYPE_RESOLUTION_STATUS
Definition: blpapi_defs.h:91
iterator end() const
Definition: blpapi_event.h:554
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:659
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:720
struct blpapi_Event blpapi_Event_t
Definition: blpapi_types.h:139
blpapi_EventQueue_t * handle() const
Definition: blpapi_event.h:583
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:220
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:664
#define BLPAPI_CALL(FUNCNAME)
Definition: blpapi_call.h:353
Event & operator=(const Event &rhs)
Definition: blpapi_event.h:533
iterator & operator=(const iterator &original)
Definition: blpapi_event.h:652
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:564
Definition: blpapi_event.h:243
bool isValid() const
Definition: blpapi_event.h:606
#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:569
struct blpapi_EventQueue blpapi_EventQueue_t
Definition: blpapi_types.h:148
EventType
The possible types of event.
Definition: blpapi_event.h:202
struct blpapi_Message blpapi_Message_t
Definition: blpapi_message.h:70
EventQueue()
Definition: blpapi_event.h:560
#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:601
int blpapi_MessageIterator_addRef(const blpapi_MessageIterator_t *iterator)
iterator & operator++()
Definition: blpapi_event.h:669
EventType eventType() const
Definition: blpapi_event.h:543
#define BLPAPI_EVENTTYPE_TOPIC_STATUS
Definition: blpapi_defs.h:92
~MessageIterator()
Definition: blpapi_event.h:596
struct blpapi_MessageIterator blpapi_MessageIterator_t
Definition: blpapi_types.h:151
~Event()
Definition: blpapi_event.h:526
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:508
iterator()
Definition: blpapi_event.h:623
void swap(iterator &other)
Definition: blpapi_event.h:687
iterator begin() const
Definition: blpapi_event.h:552
#define BLPAPI_EVENTTYPE_REQUEST_STATUS
Definition: blpapi_defs.h:84
virtual void purge()
Definition: blpapi_event.h:581
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:548
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:445
#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:550
~iterator()
Definition: blpapi_event.h:647
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:611
MessageIterator(const MessageIterator &)=delete
virtual ~EventQueue()
Definition: blpapi_event.h:562
Status updates for a session.
Definition: blpapi_event.h:205
Definition: blpapi_event.h:231
int blpapi_Event_release(const blpapi_Event_t *event)