Robot Raconteur Core C++ Library
Loading...
Searching...
No Matches
Generator.h
Go to the documentation of this file.
1
23
24#pragma once
25
30
31namespace RobotRaconteur
32{
66
67template <typename Return, typename Param>
68class Generator : private boost::noncopyable
69{
70 public:
80 virtual Return Next(const Param& v) = 0;
92 virtual bool TryNext(const Param& v, Return& ret)
93 {
94 // TODO: This is not the most efficient implementation
95 try
96 {
97 ret = Next(v);
98 return true;
99 }
101 {
102 return false;
103 }
104 }
105
114 virtual void AsyncNext(
115 const Param& v, boost::function<void(const Return&, const RR_SHARED_PTR<RobotRaconteurException>& err)> handler,
116 int32_t timeout = RR_TIMEOUT_INFINITE) = 0;
124 virtual void Abort() = 0;
133 virtual void AsyncAbort(boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>& err)> handler,
134 int32_t timeout = RR_TIMEOUT_INFINITE) = 0;
143 virtual void Close() = 0;
152 virtual void AsyncClose(boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>& err)> handler,
153 int32_t timeout = RR_TIMEOUT_INFINITE) = 0;
154 virtual ~Generator() {}
155};
156
163template <typename Return>
164class Generator<Return, void> : private boost::noncopyable
165{
166 public:
175 virtual Return Next() = 0;
176
187 virtual bool TryNext(Return& ret)
188 {
189 try
190 {
191 ret = Next();
192 return true;
193 }
195 {
196 return false;
197 }
198 }
199
207 virtual void AsyncNext(
208 boost::function<void(const Return&, const RR_SHARED_PTR<RobotRaconteurException>& err)> handler,
209 int32_t timeout = RR_TIMEOUT_INFINITE) = 0;
211 virtual void Abort() = 0;
213 virtual void AsyncAbort(boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>& err)> handler,
214 int32_t timeout = RR_TIMEOUT_INFINITE) = 0;
216 virtual void Close() = 0;
218 virtual void AsyncClose(boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>& err)> handler,
219 int32_t timeout = RR_TIMEOUT_INFINITE) = 0;
225 virtual std::vector<Return> NextAll()
226 {
227 std::vector<Return> ret;
228 try
229 {
230 while (true)
231 {
232 ret.push_back(Next());
233 }
234 }
236 {}
237 return ret;
238 }
239 virtual ~Generator() {}
240};
241
248template <typename Param>
249class Generator<void, Param> : private boost::noncopyable
250{
251 public:
260 virtual void Next(const Param& v) = 0;
261
272 virtual bool TryNext(const Param& v)
273 {
274 try
275 {
276 Next(v);
277 }
279 {
280 return false;
281 }
282 return true;
283 }
284
293 virtual void AsyncNext(const Param& v,
294 boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>& err)> handler,
295 int32_t timeout = RR_TIMEOUT_INFINITE) = 0;
297 virtual void Abort() = 0;
299 virtual void AsyncAbort(boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>& err)> handler,
300 int32_t timeout = RR_TIMEOUT_INFINITE) = 0;
302 virtual void Close() = 0;
304 virtual void AsyncClose(boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)> handler,
305 int32_t timeout = RR_TIMEOUT_INFINITE) = 0;
306 virtual ~Generator() {}
307};
308
309class ROBOTRACONTEUR_CORE_API GeneratorClientBase
310{
311 protected:
312 std::string name;
313 int32_t id;
314 RR_WEAK_PTR<ServiceStub> stub;
315 RR_WEAK_PTR<RobotRaconteurNode> node;
316 uint32_t endpoint;
317 std::string service_path;
318
319 GeneratorClientBase(boost::string_ref name, int32_t id, const RR_SHARED_PTR<ServiceStub>& stub);
320
321 virtual RR_INTRUSIVE_PTR<MessageElement> NextBase(const RR_INTRUSIVE_PTR<MessageElement>& v);
322 virtual void AsyncNextBase(
323 const RR_INTRUSIVE_PTR<MessageElement>& v,
324 boost::function<void(const RR_INTRUSIVE_PTR<MessageElement>&, const RR_SHARED_PTR<RobotRaconteurException>&,
325 const RR_SHARED_PTR<RobotRaconteurNode>&)>
326 handler,
327 int32_t timeout);
328
329 static void AsyncNextBase1(
330 const RR_INTRUSIVE_PTR<MessageEntry>& ret, const RR_SHARED_PTR<RobotRaconteurException>& err,
331 boost::function<void(const RR_INTRUSIVE_PTR<MessageElement>&, const RR_SHARED_PTR<RobotRaconteurException>&,
332 const RR_SHARED_PTR<RobotRaconteurNode>&)>
333 handler,
334 const RR_WEAK_PTR<RobotRaconteurNode>& node);
335
336 public:
337 RR_SHARED_PTR<ServiceStub> GetStub();
338
339 virtual void Abort();
340 virtual void AsyncAbort(boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)> handler,
341 int32_t timeout = RR_TIMEOUT_INFINITE);
342 virtual void Close();
343 virtual void AsyncClose(boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)> handler,
344 int32_t timeout = RR_TIMEOUT_INFINITE);
345 std::string GetMemberName();
346
347 virtual ~GeneratorClientBase() {}
348};
349
350namespace detail
351{
352template <typename Return>
353static void GeneratorClient_AsyncNext1(
354 const RR_INTRUSIVE_PTR<MessageElement>& v2, const RR_SHARED_PTR<RobotRaconteurException>& err,
355 const RR_SHARED_PTR<RobotRaconteurNode>& node,
356 boost::function<void(const Return&, const RR_SHARED_PTR<RobotRaconteurException>&)> handler)
357{
358 if (err)
359 {
360 detail::InvokeHandlerWithException(node, handler, err);
361 return;
362 }
363 Return ret;
364 try
365 {
366 ret = RRPrimUtil<Return>::PreUnpack(node->UnpackAnyType<typename RRPrimUtil<Return>::BoxedType>(v2));
367 }
368 catch (std::exception& e)
369 {
370 detail::InvokeHandlerWithException(node, handler, e);
371 return;
372 }
373 detail::InvokeHandler<Return>(node, handler, ret);
374}
375
376ROBOTRACONTEUR_CORE_API void GeneratorClient_AsyncNext2(
377 const RR_INTRUSIVE_PTR<MessageElement>& v2, const RR_SHARED_PTR<RobotRaconteurException>& err,
378 const RR_SHARED_PTR<RobotRaconteurNode>& node,
379 boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)> handler);
380} // namespace detail
381
382template <typename Return, typename Param>
383class GeneratorClient : public Generator<Return, Param>, public GeneratorClientBase
384{
385 public:
386 GeneratorClient(boost::string_ref name, int32_t id, const RR_SHARED_PTR<ServiceStub>& stub)
387 : GeneratorClientBase(name, id, stub)
388 {}
389
390 RR_OVIRTUAL Return Next(const Param& v) RR_OVERRIDE
391 {
392 RR_INTRUSIVE_PTR<MessageElement> v1 = CreateMessageElement(
393 "", GetStub()->RRGetNode()->template PackAnyType<typename RRPrimUtil<Param>::BoxedType>(
394 RRPrimUtil<Param>::PrePack(v)));
395 RR_INTRUSIVE_PTR<MessageElement> v2 = NextBase(v1);
396 return RRPrimUtil<Return>::PreUnpack(
397 GetStub()->RRGetNode()->template UnpackAnyType<typename RRPrimUtil<Return>::BoxedType>(v2));
398 }
399 RR_OVIRTUAL void AsyncNext(
400 const Param& v, boost::function<void(const Return&, const RR_SHARED_PTR<RobotRaconteurException>&)> handler,
401 int32_t timeout = RR_TIMEOUT_INFINITE) RR_OVERRIDE
402 {
403 RR_INTRUSIVE_PTR<MessageElement> v1 = CreateMessageElement(
404 "", GetStub()->RRGetNode()->template PackAnyType<typename RRPrimUtil<Param>::BoxedType>(
405 RRPrimUtil<Param>::PrePack(v)));
406 AsyncNextBase(v1,
407 boost::bind<void>(&detail::GeneratorClient_AsyncNext1<Return>, RR_BOOST_PLACEHOLDERS(_1),
408 RR_BOOST_PLACEHOLDERS(_2), RR_BOOST_PLACEHOLDERS(_3), handler),
409 timeout);
410 }
411 RR_OVIRTUAL void Abort() RR_OVERRIDE { GeneratorClientBase::Abort(); }
412 RR_OVIRTUAL void AsyncAbort(boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)> handler,
413 int32_t timeout = RR_TIMEOUT_INFINITE) RR_OVERRIDE
414 {
415 GeneratorClientBase::AsyncAbort(handler, timeout);
416 }
417 RR_OVIRTUAL void Close() RR_OVERRIDE { GeneratorClientBase::Close(); }
418 RR_OVIRTUAL void AsyncClose(boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)> handler,
419 int32_t timeout = RR_TIMEOUT_INFINITE) RR_OVERRIDE
420 {
421 GeneratorClientBase::AsyncClose(handler, timeout);
422 }
423};
424
425template <typename Return>
426class GeneratorClient<Return, void> : public Generator<Return, void>, public GeneratorClientBase
427{
428 public:
429 GeneratorClient(boost::string_ref name, int32_t id, const RR_SHARED_PTR<ServiceStub>& stub)
430 : GeneratorClientBase(name, id, stub)
431 {}
432
433 RR_OVIRTUAL Return Next() RR_OVERRIDE
434 {
435 RR_INTRUSIVE_PTR<MessageElement> v2 = NextBase(RR_INTRUSIVE_PTR<MessageElement>());
436 return RRPrimUtil<Return>::PreUnpack(
437 GetStub()->RRGetNode()->template UnpackAnyType<typename RRPrimUtil<Return>::BoxedType>(v2));
438 }
439 RR_OVIRTUAL void AsyncNext(
440 boost::function<void(const Return&, const RR_SHARED_PTR<RobotRaconteurException>&)> handler,
441 int32_t timeout = RR_TIMEOUT_INFINITE) RR_OVERRIDE
442 {
443 AsyncNextBase(RR_INTRUSIVE_PTR<MessageElement>(),
444 boost::bind<void>(&detail::GeneratorClient_AsyncNext1<Return>, RR_BOOST_PLACEHOLDERS(_1),
445 RR_BOOST_PLACEHOLDERS(_2), RR_BOOST_PLACEHOLDERS(_3), handler),
446 timeout);
447 }
448 RR_OVIRTUAL void Abort() RR_OVERRIDE { GeneratorClientBase::Abort(); }
449 RR_OVIRTUAL void AsyncAbort(boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)> handler,
450 int32_t timeout = RR_TIMEOUT_INFINITE) RR_OVERRIDE
451 {
452 GeneratorClientBase::AsyncAbort(handler, timeout);
453 }
454 RR_OVIRTUAL void Close() RR_OVERRIDE { GeneratorClientBase::Close(); }
455 RR_OVIRTUAL void AsyncClose(boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)> handler,
456 int32_t timeout = RR_TIMEOUT_INFINITE) RR_OVERRIDE
457 {
458 GeneratorClientBase::AsyncClose(handler, timeout);
459 }
460};
461
462template <typename Param>
463class GeneratorClient<void, Param> : public Generator<void, Param>, public GeneratorClientBase
464{
465 public:
466 GeneratorClient(boost::string_ref name, int32_t id, const RR_SHARED_PTR<ServiceStub>& stub)
467 : GeneratorClientBase(name, id, stub)
468 {}
469
470 RR_OVIRTUAL void Next(const Param& v) RR_OVERRIDE
471 {
472 RR_INTRUSIVE_PTR<MessageElement> v1 = CreateMessageElement(
473 "", GetStub()->RRGetNode()->template PackAnyType<typename RRPrimUtil<Param>::BoxedType>(
474 RRPrimUtil<Param>::PrePack(v)));
475 NextBase(v1);
476 }
477 RR_OVIRTUAL void AsyncNext(const Param& v,
478 boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)> handler,
479 int32_t timeout = RR_TIMEOUT_INFINITE) RR_OVERRIDE
480 {
481 RR_INTRUSIVE_PTR<MessageElement> v1 = CreateMessageElement(
482 "", GetStub()->RRGetNode()->template PackAnyType<typename RRPrimUtil<Param>::BoxedType>(
483 RRPrimUtil<Param>::PrePack(v)));
484 AsyncNextBase(v1,
485 boost::bind<void>(&detail::GeneratorClient_AsyncNext2, RR_BOOST_PLACEHOLDERS(_1),
486 RR_BOOST_PLACEHOLDERS(_2), RR_BOOST_PLACEHOLDERS(_3), RR_MOVE(handler)),
487 timeout);
488 }
489 RR_OVIRTUAL void Abort() RR_OVERRIDE { GeneratorClientBase::Abort(); }
490 RR_OVIRTUAL void AsyncAbort(boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)> handler,
491 int32_t timeout = RR_TIMEOUT_INFINITE) RR_OVERRIDE
492 {
493 GeneratorClientBase::AsyncAbort(handler, timeout);
494 }
495 RR_OVIRTUAL void Close() RR_OVERRIDE { GeneratorClientBase::Close(); }
496 RR_OVIRTUAL void AsyncClose(boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)> handler,
497 int32_t timeout = RR_TIMEOUT_INFINITE) RR_OVERRIDE
498 {
499 GeneratorClientBase::AsyncClose(handler, timeout);
500 }
501};
502
503class ServerEndpoint;
504
505class ROBOTRACONTEUR_CORE_API GeneratorServerBase : private boost::noncopyable
506{
507 protected:
508 std::string name;
509 int32_t index;
510 RR_WEAK_PTR<ServiceSkel> skel;
511 RR_INTRUSIVE_PTR<MessageElement> m;
512 RR_SHARED_PTR<ServerEndpoint> ep;
513 RR_WEAK_PTR<RobotRaconteurNode> node;
514 std::string service_path;
515
516 boost::posix_time::ptime last_access_time;
517
518 GeneratorServerBase(boost::string_ref name, int32_t index, const RR_SHARED_PTR<ServiceSkel>& skel,
519 const RR_SHARED_PTR<ServerEndpoint>& ep);
520
521 public:
522 friend class ServiceSkel;
523
524 virtual void CallNext(const RR_INTRUSIVE_PTR<MessageEntry>& m) = 0;
525
526 virtual uint32_t GetEndpoint();
527
528 virtual ~GeneratorServerBase() {}
529
530 protected:
531 static void EndAsyncCallNext(const RR_WEAK_PTR<ServiceSkel>& skel, const RR_INTRUSIVE_PTR<MessageElement>& ret,
532 const RR_SHARED_PTR<RobotRaconteurException>& err, int32_t index,
533 const RR_INTRUSIVE_PTR<MessageEntry>& m, const RR_SHARED_PTR<ServerEndpoint>& ep);
534
535 template <typename Return>
536 static void CallNext1(Return v2, const RR_SHARED_PTR<RobotRaconteurException>& err, int32_t index,
537 const RR_WEAK_PTR<ServiceSkel>& skel, const RR_INTRUSIVE_PTR<MessageEntry>& m,
538 const RR_SHARED_PTR<ServerEndpoint>& ep)
539 {
540 RR_SHARED_PTR<ServiceSkel> skel1 = skel.lock();
541 RR_SHARED_PTR<RobotRaconteurException> err2 = err;
542 // TODO: Improve null handling
543 if (!skel1 && !err2)
544 {
545 err2 = RR_MAKE_SHARED<InvalidOperationException>("Service skel released");
546 }
547 if (err2)
548 {
549 GeneratorServerBase::EndAsyncCallNext(skel, RR_INTRUSIVE_PTR<MessageElement>(), err2, index, m, ep);
550 return;
551 }
552
553 RR_INTRUSIVE_PTR<MessageElement> v3;
554 try
555 {
556 v3 = CreateMessageElement("",
557 skel1->RRGetNode()->template PackAnyType<typename RRPrimUtil<Return>::BoxedType>(
558 RRPrimUtil<Return>::PrePack(v2)));
559 }
560 catch (std::exception& exp)
561 {
562 // Reply with the error: an exception thrown here would go to the generator's caller, and the client
563 // would wait for a reply until its request timed out
564 GeneratorServerBase::EndAsyncCallNext(skel, RR_INTRUSIVE_PTR<MessageElement>(),
565 RobotRaconteurExceptionUtil::ExceptionToSharedPtr(exp), index, m, ep);
566 return;
567 }
568 GeneratorServerBase::EndAsyncCallNext(skel, v3, err, index, m, ep);
569 }
570
571 static void CallNext2(const RR_SHARED_PTR<RobotRaconteurException>& err, int32_t index,
572 const RR_WEAK_PTR<ServiceSkel>& skel, const RR_INTRUSIVE_PTR<MessageEntry>& m,
573 const RR_SHARED_PTR<ServerEndpoint>& ep)
574 {
575 RR_SHARED_PTR<ServiceSkel> skel1 = skel.lock();
576 RR_SHARED_PTR<RobotRaconteurException> err2 = err;
577 // TODO: Improve null handling
578 if (!skel1 && !err2)
579 {
580 err2 = RR_MAKE_SHARED<InvalidOperationException>("Service skel released");
581 }
582 if (err2)
583 {
584 GeneratorServerBase::EndAsyncCallNext(skel, RR_INTRUSIVE_PTR<MessageElement>(), err2, index, m, ep);
585 return;
586 }
587
588 RR_INTRUSIVE_PTR<MessageElement> v3;
589 GeneratorServerBase::EndAsyncCallNext(skel, v3, err2, index, m, ep);
590 }
591};
592
593template <typename Return, typename Param>
594class GeneratorServer : public GeneratorServerBase
595{
596 protected:
597 RR_SHARED_PTR<Generator<Return, Param> > generator;
598
599 public:
600 GeneratorServer(const RR_SHARED_PTR<Generator<Return, Param> >& generator, boost::string_ref name, int32_t id,
601 const RR_SHARED_PTR<ServiceSkel>& skel, const RR_SHARED_PTR<ServerEndpoint>& ep)
602 : GeneratorServerBase(name, id, skel, ep)
603 {
604 if (!generator)
605 throw InvalidOperationException("Generator must not be null");
606 this->generator = generator;
607 }
608
609 RR_OVIRTUAL void CallNext(const RR_INTRUSIVE_PTR<MessageEntry>& m) RR_OVERRIDE
610 {
611 ROBOTRACONTEUR_LOG_TRACE_COMPONENT_PATH(node, Service, ep->GetLocalEndpoint(), service_path, name,
612 "Generator CallNext with id " << index);
613 RR_SHARED_PTR<ServiceSkel> skel1 = skel.lock();
614 if (!skel1)
615 {
616 throw InvalidOperationException("Skel has been released");
617 }
618
619 if (m->Error != MessageErrorType_None)
620 {
621 if (m->Error == MessageErrorType_StopIteration)
622 {
623 generator->AsyncClose(boost::bind<void>(&GeneratorServerBase::CallNext1<int32_t>, 0,
624 RR_BOOST_PLACEHOLDERS(_1), index, skel, m, ep));
625 }
626 else
627 {
628 generator->AsyncAbort(boost::bind<void>(&GeneratorServerBase::CallNext1<int32_t>, 0,
629 RR_BOOST_PLACEHOLDERS(_1), index, skel, m, ep));
630 }
631 }
632 else
633 {
634 Param v = RRPrimUtil<Param>::PreUnpack(
635 skel1->RRGetNode()->template UnpackAnyType<typename RRPrimUtil<Param>::BoxedType>(
636 m->FindElement("parameter")));
637 generator->AsyncNext(v,
638 boost::bind<void>(&GeneratorServerBase::CallNext1<Return>, RR_BOOST_PLACEHOLDERS(_1),
639 RR_BOOST_PLACEHOLDERS(_2), index, skel, m, ep));
640 }
641 }
642
643 RR_OVIRTUAL ~GeneratorServer() RR_OVERRIDE {}
644};
645
646template <typename Return>
647class GeneratorServer<Return, void> : public GeneratorServerBase
648{
649 protected:
650 RR_SHARED_PTR<Generator<Return, void> > generator;
651
652 public:
653 GeneratorServer(RR_SHARED_PTR<Generator<Return, void> > generator, boost::string_ref name, int32_t id,
654 const RR_SHARED_PTR<ServiceSkel>& skel, const RR_SHARED_PTR<ServerEndpoint>& ep)
655 : GeneratorServerBase(name, id, skel, ep)
656 {
657 if (!generator)
658 throw InvalidOperationException("Generator must not be null");
659 this->generator = generator;
660 }
661
662 RR_OVIRTUAL void CallNext(const RR_INTRUSIVE_PTR<MessageEntry>& m) RR_OVERRIDE
663 {
664 RR_SHARED_PTR<ServiceSkel> skel1 = skel.lock();
665 if (!skel1)
666 {
667 throw InvalidOperationException("Skel has been released");
668 }
669
670 if (m->Error != MessageErrorType_None)
671 {
672 if (m->Error == MessageErrorType_StopIteration)
673 {
674 generator->AsyncClose(boost::bind<void>(&GeneratorServerBase::CallNext1<int32_t>, 0,
675 RR_BOOST_PLACEHOLDERS(_1), index, skel, m, ep));
676 }
677 else
678 {
679 generator->AsyncAbort(boost::bind<void>(&GeneratorServerBase::CallNext1<int32_t>, 0,
680 RR_BOOST_PLACEHOLDERS(_1), index, skel, m, ep));
681 }
682 }
683 else
684 {
685 generator->AsyncNext(boost::bind<void>(&GeneratorServerBase::CallNext1<Return>, RR_BOOST_PLACEHOLDERS(_1),
686 RR_BOOST_PLACEHOLDERS(_2), index, skel, m, ep));
687 }
688 }
689 RR_OVIRTUAL ~GeneratorServer() RR_OVERRIDE {}
690};
691
692template <typename Param>
693class GeneratorServer<void, Param> : public GeneratorServerBase
694{
695 protected:
696 RR_SHARED_PTR<Generator<void, Param> > generator;
697
698 public:
699 GeneratorServer(RR_SHARED_PTR<Generator<void, Param> > generator, boost::string_ref name, int32_t id,
700 const RR_SHARED_PTR<ServiceSkel>& skel, const RR_SHARED_PTR<ServerEndpoint>& ep)
701 : GeneratorServerBase(name, id, skel, ep)
702 {
703 if (!generator)
704 throw InvalidOperationException("Generator must not be null");
705 this->generator = generator;
706 }
707
708 RR_OVIRTUAL void CallNext(const RR_INTRUSIVE_PTR<MessageEntry>& m) RR_OVERRIDE
709 {
710 RR_SHARED_PTR<ServiceSkel> skel1 = skel.lock();
711 if (!skel1)
712 {
713 throw InvalidOperationException("Skel has been released");
714 }
715
716 if (m->Error != MessageErrorType_None)
717 {
718 if (m->Error == MessageErrorType_StopIteration)
719 {
720 generator->AsyncClose(boost::bind<void>(&GeneratorServerBase::CallNext1<int32_t>, 0,
721 RR_BOOST_PLACEHOLDERS(_1), index, skel, m, ep));
722 }
723 else
724 {
725 generator->AsyncAbort(boost::bind<void>(&GeneratorServerBase::CallNext1<int32_t>, 0,
726 RR_BOOST_PLACEHOLDERS(_1), index, skel, m, ep));
727 }
728 }
729 else
730 {
731 Param v = RRPrimUtil<Param>::PreUnpack(
732 skel1->RRGetNode()->template UnpackAnyType<typename RRPrimUtil<Param>::BoxedType>(
733 m->FindElement("parameter")));
734 generator->AsyncNext(
735 v, boost::bind<void>(&GeneratorServerBase::CallNext2, RR_BOOST_PLACEHOLDERS(_1), index, skel, m, ep));
736 }
737 }
738 RR_OVIRTUAL ~GeneratorServer() RR_OVERRIDE {}
739};
740
741// NOLINTBEGIN(cppcoreguidelines-virtual-class-destructor)
742
751template <typename Return, typename Param>
752class SyncGenerator : public Generator<Return, Param>
753{
754 public:
756 RR_OVIRTUAL Return Next(const Param& v) RR_OVERRIDE = 0;
758 RR_OVIRTUAL void AsyncNext(
759 const Param& v, boost::function<void(const Return&, const RR_SHARED_PTR<RobotRaconteurException>& err)> handler,
760 int32_t timeout = RR_TIMEOUT_INFINITE) RR_OVERRIDE
761 {
762 RR_UNUSED(timeout);
763 Return r;
764 RR_WEAK_PTR<RobotRaconteurNode> node;
765 try
766 {
767 r = Next(v);
768 }
769 catch (std::exception& exp)
770 {
771 detail::InvokeHandlerWithException(node, handler, exp);
772 return;
773 }
774 detail::InvokeHandler(node, handler, r);
775 }
776
777 RR_OVIRTUAL void Abort() RR_OVERRIDE = 0;
779 RR_OVIRTUAL void AsyncAbort(boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)> handler,
780 int32_t timeout = RR_TIMEOUT_INFINITE) RR_OVERRIDE
781 {
782 RR_UNUSED(timeout);
783 RR_WEAK_PTR<RobotRaconteurNode> node;
784 try
785 {
786 Abort();
787 }
788 catch (std::exception& exp)
789 {
790 detail::InvokeHandlerWithException(node, handler, exp);
791 return;
792 }
793 detail::InvokeHandler(node, handler);
794 }
795
796 RR_OVIRTUAL void Close() RR_OVERRIDE = 0;
798 RR_OVIRTUAL void AsyncClose(boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)> handler,
799 int32_t timeout = RR_TIMEOUT_INFINITE) RR_OVERRIDE
800 {
801 RR_UNUSED(timeout);
802 RR_WEAK_PTR<RobotRaconteurNode> node;
803 try
804 {
805 Close();
806 }
807 catch (std::exception& exp)
808 {
809 detail::InvokeHandlerWithException(node, handler, exp);
810 return;
811 }
812 detail::InvokeHandler(node, handler);
813 }
814 RR_OVIRTUAL ~SyncGenerator() RR_OVERRIDE {}
815};
816
818template <typename Return>
819class SyncGenerator<Return, void> : public Generator<Return, void>
820{
821 public:
823 RR_OVIRTUAL Return Next() RR_OVERRIDE = 0;
825 RR_OVIRTUAL void AsyncNext(
826 boost::function<void(const Return&, const RR_SHARED_PTR<RobotRaconteurException>& err)> handler,
827 int32_t timeout = RR_TIMEOUT_INFINITE) RR_OVERRIDE
828 {
829 RR_UNUSED(timeout);
830 Return r;
831 RR_WEAK_PTR<RobotRaconteurNode> node;
832 try
833 {
834 r = Next();
835 }
836 catch (std::exception& exp)
837 {
838 detail::InvokeHandlerWithException(node, handler, exp);
839 return;
840 }
841 detail::InvokeHandler(node, handler, r);
842 }
843
844 RR_OVIRTUAL void Abort() RR_OVERRIDE = 0;
846 RR_OVIRTUAL void AsyncAbort(boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)> handler,
847 int32_t timeout = RR_TIMEOUT_INFINITE) RR_OVERRIDE
848 {
849 RR_UNUSED(timeout);
850 RR_WEAK_PTR<RobotRaconteurNode> node;
851 try
852 {
853 Abort();
854 }
855 catch (std::exception& exp)
856 {
857 detail::InvokeHandlerWithException(node, handler, exp);
858 return;
859 }
860 detail::InvokeHandler(node, handler);
861 }
862
863 RR_OVIRTUAL void Close() RR_OVERRIDE = 0;
865 RR_OVIRTUAL void AsyncClose(boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)> handler,
866 int32_t timeout = RR_TIMEOUT_INFINITE) RR_OVERRIDE
867 {
868 RR_UNUSED(timeout);
869 RR_WEAK_PTR<RobotRaconteurNode> node;
870 try
871 {
872 Close();
873 }
874 catch (std::exception& exp)
875 {
876 detail::InvokeHandlerWithException(node, handler, exp);
877 return;
878 }
879 detail::InvokeHandler(node, handler);
880 }
881 RR_OVIRTUAL ~SyncGenerator() RR_OVERRIDE {}
882};
883
885template <typename Param>
886class SyncGenerator<void, Param> : public Generator<void, Param>
887{
888 public:
890 RR_OVIRTUAL void Next(const Param& v) RR_OVERRIDE = 0;
892 RR_OVIRTUAL void AsyncNext(const Param& v,
893 boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)> handler,
894 int32_t timeout = RR_TIMEOUT_INFINITE) RR_OVERRIDE
895 {
896 RR_UNUSED(timeout);
897 RR_WEAK_PTR<RobotRaconteurNode> node;
898 try
899 {
900 Next(v);
901 }
902 catch (std::exception& exp)
903 {
904 detail::InvokeHandlerWithException(node, handler, exp);
905 return;
906 }
907 detail::InvokeHandler(node, handler);
908 }
909
910 RR_OVIRTUAL void Abort() RR_OVERRIDE = 0;
912 RR_OVIRTUAL void AsyncAbort(boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)> handler,
913 int32_t timeout = RR_TIMEOUT_INFINITE) RR_OVERRIDE
914 {
915 RR_UNUSED(timeout);
916 RR_WEAK_PTR<RobotRaconteurNode> node;
917 try
918 {
919 Abort();
920 }
921 catch (std::exception& exp)
922 {
923 detail::InvokeHandlerWithException(node, handler, exp);
924 return;
925 }
926 detail::InvokeHandler(node, handler);
927 }
928
929 RR_OVIRTUAL void Close() RR_OVERRIDE = 0;
931 RR_OVIRTUAL void AsyncClose(boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)> handler,
932 int32_t timeout = RR_TIMEOUT_INFINITE) RR_OVERRIDE
933 {
934 RR_UNUSED(timeout);
935 RR_WEAK_PTR<RobotRaconteurNode> node;
936 try
937 {
938 Close();
939 }
940 catch (std::exception& exp)
941 {
942 detail::InvokeHandlerWithException(node, handler, exp);
943 return;
944 }
945 detail::InvokeHandler(node, handler);
946 }
947 RR_OVIRTUAL ~SyncGenerator() RR_OVERRIDE {}
948};
949
950// NOLINTEND(cppcoreguidelines-virtual-class-destructor)
951
961template <typename T>
962class RangeGenerator : public SyncGenerator<typename T::value_type, void>
963{
964 protected:
965 T range;
966 typename T::iterator iter;
967 boost::mutex range_lock;
968 bool aborted;
969
970 public:
971 RangeGenerator(const T& range)
972 {
973 this->range = range;
974 aborted = false;
975 this->iter = this->range.begin();
976 }
977
979 RR_OVIRTUAL typename T::value_type Next() RR_OVERRIDE
980 {
981 boost::mutex::scoped_lock lock(range_lock);
982
983 if (aborted)
984 {
986 }
987
988 if (iter == range.end())
989 {
990 throw StopIterationException("");
991 }
992
993 return *iter++;
994 }
995
996 RR_OVIRTUAL void Abort() RR_OVERRIDE
997 {
998 boost::mutex::scoped_lock lock(range_lock);
999 iter = range.end();
1000 aborted = true;
1001 }
1002
1003 RR_OVIRTUAL void Close() RR_OVERRIDE
1004 {
1005 boost::mutex::scoped_lock lock(range_lock);
1006 iter = range.end();
1007 }
1008 RR_OVIRTUAL ~RangeGenerator() RR_OVERRIDE {}
1009};
1010
1018template <typename T>
1019RR_SHARED_PTR<RangeGenerator<T> > CreateRangeGenerator(const T& range)
1020{
1021 return RR_MAKE_SHARED<RangeGenerator<T> >(range);
1022}
1023
1024#ifndef ROBOTRACONTEUR_NO_CXX11_TEMPLATE_ALIASES
1025template <typename Return, typename Param>
1026using GeneratorPtr = RR_SHARED_PTR<Generator<Return, Param> >;
1027template <typename Return, typename Param>
1028using GeneratorConstPtr = RR_SHARED_PTR<const Generator<Return, Param> >;
1029template <typename T>
1030using RangeGeneratorPtr = RR_SHARED_PTR<RangeGenerator<T> >;
1031template <typename T>
1032using RangeGeneratorConstPtr = RR_SHARED_PTR<const RangeGenerator<T> >;
1033#endif
1034
1035} // namespace RobotRaconteur
boost::shared_ptr< RangeGenerator< T > > CreateRangeGenerator(const T &range)
Create a RangeGenerator from a range.
Definition Generator.h:1019
#define RR_TIMEOUT_INFINITE
Disable timeout for asynchronous operations.
Definition RobotRaconteurConstants.h:566
virtual bool TryNext(Return &ret)
Try to advance the generator. Return false if no more values are available.
Definition Generator.h:187
virtual void AsyncClose(boost::function< void(const boost::shared_ptr< RobotRaconteurException > &err)> handler, int32_t timeout=RR_TIMEOUT_INFINITE)=0
Asynchronously closes the generator.
virtual void Close()=0
Close the generator.
virtual void AsyncNext(boost::function< void(const Return &, const boost::shared_ptr< RobotRaconteurException > &err)> handler, int32_t timeout=RR_TIMEOUT_INFINITE)=0
Asynchronously advance the generator.
virtual void AsyncAbort(boost::function< void(const boost::shared_ptr< RobotRaconteurException > &err)> handler, int32_t timeout=RR_TIMEOUT_INFINITE)=0
Asynchronously abort the generator.
virtual Return Next()=0
Advance the generator.
virtual std::vector< Return > NextAll()
Automatically call Next() repeatedly and return std::vector of results.
Definition Generator.h:225
virtual void Abort()=0
Abort the generator.
virtual void AsyncClose(boost::function< void(const boost::shared_ptr< RobotRaconteurException > &)> handler, int32_t timeout=RR_TIMEOUT_INFINITE)=0
Asynchronously closes the generator.
virtual void AsyncNext(const Param &v, boost::function< void(const boost::shared_ptr< RobotRaconteurException > &err)> handler, int32_t timeout=RR_TIMEOUT_INFINITE)=0
Asynchronously advance the generator.
virtual void Close()=0
Close the generator.
virtual void AsyncAbort(boost::function< void(const boost::shared_ptr< RobotRaconteurException > &err)> handler, int32_t timeout=RR_TIMEOUT_INFINITE)=0
Asynchronously abort the generator.
virtual void Next(const Param &v)=0
Advance the generator.
virtual void Abort()=0
Abort the generator.
virtual bool TryNext(const Param &v)
Try to advance the generator. Return false if no more values are available.
Definition Generator.h:272
Generator type for use with generator functions, with parameter and return.
Definition Generator.h:69
virtual void Abort()=0
Abort the generator.
virtual void Close()=0
Close the generator.
virtual bool TryNext(const Param &v, Return &ret)
Try to advance the generator. Return false if no more values are available.
Definition Generator.h:92
virtual void AsyncAbort(boost::function< void(const boost::shared_ptr< RobotRaconteurException > &err)> handler, int32_t timeout=RR_TIMEOUT_INFINITE)=0
Asynchronously abort the generator.
virtual void AsyncNext(const Param &v, boost::function< void(const Return &, const boost::shared_ptr< RobotRaconteurException > &err)> handler, int32_t timeout=RR_TIMEOUT_INFINITE)=0
Asynchronously advance the generator.
virtual void AsyncClose(boost::function< void(const boost::shared_ptr< RobotRaconteurException > &err)> handler, int32_t timeout=RR_TIMEOUT_INFINITE)=0
Asynchronously closes the generator.
virtual Return Next(const Param &v)=0
Advance the generator.
InvalidOperationException(const std::string &message, std::string sub_name="", const boost::intrusive_ptr< RRValue > &param_=boost::intrusive_ptr< RRValue >())
Construct an InvalidOperationException.
OperationAbortedException(const std::string &message, std::string sub_name="", const boost::intrusive_ptr< RRValue > &param_=boost::intrusive_ptr< RRValue >())
Construct a OperationAbortedException.
Utility class to use a C++ range with Generator<Return,void>.
Definition Generator.h:963
RR_OVIRTUAL void Close() RR_OVERRIDE
Close the generator.
Definition Generator.h:1003
RR_OVIRTUAL T::value_type Next() RR_OVERRIDE
Advance the generator.
Definition Generator.h:979
RR_OVIRTUAL void Abort() RR_OVERRIDE
Abort the generator.
Definition Generator.h:996
Base class for Robot Raconteur exceptions.
Definition Error.h:53
Server endpoint representing a client connection.
Definition Service.h:740
Exception thrown when a generator has finished sending results.
Definition Error.h:1224
StopIterationException(const std::string &message, std::string sub_name="", const boost::intrusive_ptr< RRValue > &param_=boost::intrusive_ptr< RRValue >())
Construct a StopIterationException.
RR_OVIRTUAL void Abort() RR_OVERRIDE=0
Abort the generator.
RR_OVIRTUAL void AsyncNext(boost::function< void(const Return &, const boost::shared_ptr< RobotRaconteurException > &err)> handler, int32_t timeout=RR_TIMEOUT_INFINITE) RR_OVERRIDE
Asynchronously advance the generator.
Definition Generator.h:825
RR_OVIRTUAL void Close() RR_OVERRIDE=0
Close the generator.
RR_OVIRTUAL Return Next() RR_OVERRIDE=0
Advance the generator.
RR_OVIRTUAL void AsyncAbort(boost::function< void(const boost::shared_ptr< RobotRaconteurException > &)> handler, int32_t timeout=RR_TIMEOUT_INFINITE) RR_OVERRIDE
Asynchronously abort the generator.
Definition Generator.h:846
RR_OVIRTUAL void AsyncClose(boost::function< void(const boost::shared_ptr< RobotRaconteurException > &)> handler, int32_t timeout=RR_TIMEOUT_INFINITE) RR_OVERRIDE
Asynchronously closes the generator.
Definition Generator.h:865
RR_OVIRTUAL void Close() RR_OVERRIDE=0
Close the generator.
RR_OVIRTUAL void Next(const Param &v) RR_OVERRIDE=0
Advance the generator.
RR_OVIRTUAL void AsyncAbort(boost::function< void(const boost::shared_ptr< RobotRaconteurException > &)> handler, int32_t timeout=RR_TIMEOUT_INFINITE) RR_OVERRIDE
Asynchronously abort the generator.
Definition Generator.h:912
RR_OVIRTUAL void Abort() RR_OVERRIDE=0
Abort the generator.
RR_OVIRTUAL void AsyncNext(const Param &v, boost::function< void(const boost::shared_ptr< RobotRaconteurException > &)> handler, int32_t timeout=RR_TIMEOUT_INFINITE) RR_OVERRIDE
Asynchronously advance the generator.
Definition Generator.h:892
RR_OVIRTUAL void AsyncClose(boost::function< void(const boost::shared_ptr< RobotRaconteurException > &)> handler, int32_t timeout=RR_TIMEOUT_INFINITE) RR_OVERRIDE
Asynchronously closes the generator.
Definition Generator.h:931
Helper class for synchronous service generators.
Definition Generator.h:753
RR_OVIRTUAL void AsyncClose(boost::function< void(const boost::shared_ptr< RobotRaconteurException > &)> handler, int32_t timeout=RR_TIMEOUT_INFINITE) RR_OVERRIDE
Asynchronously closes the generator.
Definition Generator.h:798
RR_OVIRTUAL void Close() RR_OVERRIDE=0
Close the generator.
RR_OVIRTUAL void AsyncNext(const Param &v, boost::function< void(const Return &, const boost::shared_ptr< RobotRaconteurException > &err)> handler, int32_t timeout=RR_TIMEOUT_INFINITE) RR_OVERRIDE
Asynchronously advance the generator.
Definition Generator.h:758
RR_OVIRTUAL Return Next(const Param &v) RR_OVERRIDE=0
Advance the generator.
RR_OVIRTUAL void Abort() RR_OVERRIDE=0
Abort the generator.
RR_OVIRTUAL void AsyncAbort(boost::function< void(const boost::shared_ptr< RobotRaconteurException > &)> handler, int32_t timeout=RR_TIMEOUT_INFINITE) RR_OVERRIDE
Asynchronously abort the generator.
Definition Generator.h:779