blob: 2ca79b8926c1a80e5094e8758bc32981afb1bfbb [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001namespace aos {
2
3template <class T>
4bool ScopedMessagePtr<T>::Send() {
5 assert(msg_ != NULL);
6 msg_->SetTimeToNow();
7 assert(queue_ != NULL);
Brian Silverman08661c72013-09-01 17:24:38 -07008 bool return_value = queue_->WriteMessage(msg_, RawQueue::kOverride);
brians343bc112013-02-10 01:53:46 +00009 msg_ = NULL;
10 return return_value;
11}
12
13template <class T>
brians343bc112013-02-10 01:53:46 +000014void ScopedMessagePtr<T>::reset(T *msg) {
15 if (queue_ != NULL && msg_ != NULL) {
Brian Silverman078aee92013-11-12 22:17:05 -080016 queue_->FreeMessage(msg_);
brians343bc112013-02-10 01:53:46 +000017 }
18 msg_ = msg;
19}
20
brians343bc112013-02-10 01:53:46 +000021template <class T>
22void Queue<T>::Init() {
23 if (queue_ == NULL) {
Brian Silverman08661c72013-09-01 17:24:38 -070024 queue_ = RawQueue::Fetch(queue_name_, sizeof(T),
25 static_cast<int>(T::kHash),
26 T::kQueueLength);
brians343bc112013-02-10 01:53:46 +000027 queue_msg_.set_queue(queue_);
28 }
29}
30
31template <class T>
Austin Schuhdc1c84a2013-02-23 16:33:10 -080032void Queue<T>::Clear() {
Austin Schuhfa033692013-02-24 01:00:55 -080033 if (queue_ != NULL) {
Austin Schuhdc1c84a2013-02-23 16:33:10 -080034 queue_msg_.reset();
35 queue_ = NULL;
36 queue_msg_.set_queue(NULL);
37 }
Brian Silvermand8a7cf02014-03-22 17:15:23 -070038 index_ = 0;
Austin Schuhdc1c84a2013-02-23 16:33:10 -080039}
40
41template <class T>
brians343bc112013-02-10 01:53:46 +000042bool Queue<T>::FetchNext() {
43 Init();
Brian Silverman08661c72013-09-01 17:24:38 -070044 const T *msg = static_cast<const T *>(
Austin Schuh287d98e2014-03-09 00:41:55 -080045 queue_->ReadMessageIndex(RawQueue::kNonBlock, &index_));
brians343bc112013-02-10 01:53:46 +000046 // Only update the internal pointer if we got a new message.
47 if (msg != NULL) {
48 queue_msg_.reset(msg);
49 }
50 return msg != NULL;
51}
52
53template <class T>
Brian Silverman428de562014-04-10 15:59:19 -070054void Queue<T>::FetchNextBlocking() {
brians343bc112013-02-10 01:53:46 +000055 Init();
Brian Silverman428de562014-04-10 15:59:19 -070056 const T *msg = static_cast<const T *>(
57 queue_->ReadMessageIndex(RawQueue::kBlock, &index_));
brians343bc112013-02-10 01:53:46 +000058 queue_msg_.reset(msg);
59 assert (msg != NULL);
brians343bc112013-02-10 01:53:46 +000060}
61
62template <class T>
63bool Queue<T>::FetchLatest() {
64 Init();
Brian Silverman7faaec72014-05-26 16:25:38 -070065 static constexpr Options<RawQueue> kOptions =
66 RawQueue::kFromEnd | RawQueue::kNonBlock;
67 const T *msg =
68 static_cast<const T *>(queue_->ReadMessageIndex(kOptions, &index_));
brians343bc112013-02-10 01:53:46 +000069 // Only update the internal pointer if we got a new message.
70 if (msg != NULL && msg != queue_msg_.get()) {
71 queue_msg_.reset(msg);
72 return true;
73 }
Brian Silverman08661c72013-09-01 17:24:38 -070074 // The message has to get freed if we didn't use it (and RawQueue::FreeMessage
75 // is ok to call on NULL).
76 queue_->FreeMessage(msg);
brians343bc112013-02-10 01:53:46 +000077 return false;
78}
79
80template <class T>
Brian Silverman428de562014-04-10 15:59:19 -070081void Queue<T>::FetchAnother() {
82 if (!FetchLatest()) FetchNextBlocking();
83}
84
85template <class T>
brians343bc112013-02-10 01:53:46 +000086ScopedMessagePtr<T> Queue<T>::MakeMessage() {
87 Init();
88 return ScopedMessagePtr<T>(queue_, MakeRawMessage());
89}
90
91template <class T>
92T *Queue<T>::MakeRawMessage() {
Brian Silverman08661c72013-09-01 17:24:38 -070093 T *ret = static_cast<T *>(queue_->GetMessage());
brians35b15c42013-02-26 04:39:09 +000094 assert(ret != NULL);
Brian Silvermanc06af4c2015-10-25 01:50:41 -040095 ret->Zero();
brians35b15c42013-02-26 04:39:09 +000096 return ret;
brians343bc112013-02-10 01:53:46 +000097}
98
99template <class T>
100aos::MessageBuilder<T> Queue<T>::MakeWithBuilder() {
101 Init();
Brian Silverman42456d82014-08-19 12:43:59 -0400102 T *const ret = MakeRawMessage();
Brian Silverman42456d82014-08-19 12:43:59 -0400103 return aos::MessageBuilder<T>(queue_, ret);
brians343bc112013-02-10 01:53:46 +0000104}
105
brians343bc112013-02-10 01:53:46 +0000106} // namespace aos