This file is indexed.

/usr/include/lo/lo_cpp.h is in liblo-dev 0.28-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
#ifndef _LO_CPP_H_
#define _LO_CPP_H_

#include <lo/lo.h>
#include <lo/lo_throw.h>

#include <functional>
#include <memory>
#include <list>
#include <unordered_map>
#include <string>
#include <sstream>
#include <initializer_list>

#define LO_ADD_METHOD_RT(ht, argtypes, args, rt, r, r1, r2)             \
    template <typename H>                                               \
    auto add_method(const string_type path, const string_type types, H&& h) \
    -> rt const                                                         \
    {                                                                   \
        std::string key = std::string(path._s?:"") + "," + (types._s?:""); \
        _handlers[key].push_front(                                      \
            std::unique_ptr<handler>(new handler_type<r ht>(h)));       \
        _add_method(path, types,                                        \
            [](const char *path, const char *types,                     \
               lo_arg **argv, int argc, void *msg,                      \
               void *data)->int                                         \
            {                                                           \
                r1 (*static_cast<handler_type<r ht>*>(data)) args; \
                r2;                                                     \
            }, _handlers[key].front().get());                           \
    }

#define RT_INT(argtypes) \
    typename std::enable_if<std::is_same<decltype(h argtypes), int>::value, void>::type
#define RT_VOID(argtypes) \
    typename std::enable_if<std::is_same<decltype(h argtypes), void>::value, void>::type

#define LO_ADD_METHOD(ht, argtypes, args)                         \
    LO_ADD_METHOD_RT(ht, argtypes, args,                          \
                     RT_INT(argtypes), int, return,);             \
    LO_ADD_METHOD_RT(ht, argtypes, args,                          \
                     RT_VOID(argtypes), void, , return 0)

namespace lo {

    // Helper classes to allow polymorphism on "const char *",
    // "std::string", and "int".
    class string_type {
      public:
        string_type(const char *s=0) { _s = s; }
        string_type(const std::string &s) { _s = s.c_str(); }
        operator const char*() const { return _s; }
        const char *_s;
    };

    class num_string_type : public string_type {
      public:
      num_string_type(const char *s) : string_type(s) {}
      num_string_type(const std::string &s) : string_type(s) {}
        num_string_type(int n) { std::ostringstream ss; ss << n;
            _p.reset(new std::string(ss.str())); _s = _p->c_str(); }
        std::unique_ptr<std::string> _p;
    };

    class ServerThread;

    class Address
    {
      public:
        Address(const string_type &host, const num_string_type &port,
                int proto=LO_UDP)
          { address = lo_address_new_with_proto(proto, host, port); owned=true; }

        Address(const string_type &url)
          { address = lo_address_new_from_url(url); owned=true; }

        Address(lo_address a, bool _owned=true)
          { address = a; owned=_owned; }

        ~Address()
          { if (address && owned) lo_address_free(address); }

        Address& operator=(Address b) { b.swap(*this); return *this; }
        void swap(Address& b) throw () { std::swap(this->address, b.address); }

        int ttl() const
          { return lo_address_get_ttl(address); }

        void set_ttl(int ttl)
          { lo_address_set_ttl(address, ttl); }

        int send(const string_type &path) const
          { return lo_send(address, path, ""); }

        // In these functions we append "$$" to the type string, which
        // simply instructs lo_message_add_varargs() not to use
        // LO_MARKER checking at the end of the argument list.
        int send(const string_type &path, const string_type &type, ...) const
        {
            va_list q;
            va_start(q, type);
            lo_message m = lo_message_new();
            std::string t = std::string(type) + "$$";
            lo_message_add_varargs(m, t.c_str(), q);
            int r = lo_send_message(address, path, m);
            lo_message_free(m);
            return r;
        }

        int send(lo_timetag ts, const string_type &path,
                 const string_type &type, ...) const
        {
            va_list q;
            va_start(q, type);
            lo_message m = lo_message_new();
            std::string t = std::string(type) + "$$";
            lo_message_add_varargs(m, t.c_str(), q);
            lo_bundle b = lo_bundle_new(ts);
            lo_bundle_add_message(b, path, m);
            int r = lo_send_bundle(address, b);
            lo_bundle_free_messages(b);
            return r;
        }

        int send(const string_type &path, lo_message m) const
            { return lo_send_message(address, path, m); }

        int send(lo_bundle b)
            { return lo_send_bundle(address, b); }

        int send_from(lo::ServerThread &from, const string_type &path,
                      const string_type &type, ...) const;

        int send_from(lo_server from, const string_type &path,
                      const string_type &type, ...) const
        {
            va_list q;
            va_start(q, type);
            lo_message m = lo_message_new();
            std::string t = std::string(type) + "$$";
            lo_message_add_varargs(m, t.c_str(), q);
            int r = lo_send_message_from(address, from, path, m);
            lo_message_free(m);
            return r;
        }

        int send_from(lo_server from, lo_timetag ts, 
                      const string_type &path,
                      const string_type &type, ...) const
        {
            va_list q;
            va_start(q, type);
            lo_message m = lo_message_new();
            std::string t = std::string(type) + "$$";
            lo_message_add_varargs(m, t.c_str(), q);
            lo_bundle b = lo_bundle_new(ts);
            lo_bundle_add_message(b, path, m);
            int r = lo_send_bundle_from(address, from, b);
            lo_bundle_free_messages(b);
            return r;
        }

        int send_from(lo_server from, const string_type &path, lo_message m) const
            { return lo_send_message_from(address, from, path, m); }

        int send(lo_server from, lo_bundle b) const
            { return lo_send_bundle_from(address, from, b); }

        int get_errno() const
          { return lo_address_errno(address); }

        std::string errstr() const
          { return std::string(lo_address_errstr(address)?:""); }

        std::string hostname() const
          { return std::string(lo_address_get_hostname(address)?:""); }

        std::string port() const
          { return std::string(lo_address_get_port(address)?:""); }

        int protocol() const
          { return lo_address_get_protocol(address); }

        std::string url() const
          { return std::string(lo_address_get_url(address)?:""); }

        std::string iface() const
          { return std::string(lo_address_get_iface(address)?:""); }

        void set_iface(const string_type &iface, const string_type &ip)
          { lo_address_set_iface(address, iface._s?:0, ip._s?:0); }

        int set_tcp_nodelay(int enable)
          { return lo_address_set_tcp_nodelay(address, enable); }

        int set_stream_slip(int enable)
          { return lo_address_set_stream_slip(address, enable); }

        operator lo_address() const
            { return address; }

      protected:
        lo_address address;
        bool owned;
    };

    class Message
    {
      public:
        Message()
            : message(lo_message_new()) { lo_message_incref(message); }

        Message(lo_message m)
            : message(m) { if (m) { lo_message_incref(m); } }

        Message(const Message &m)
            : message(m.message) { if (m.message)
                                       lo_message_incref(m.message); }

        Message(const string_type &types, ...)
        {
            message = lo_message_new();
            lo_message_incref(message);
            va_list q;
            va_start(q, types);
            std::string t(std::string(types)+"$$");
            add_varargs(t.c_str(), q);
        }

        ~Message()
            { if (message) lo_message_free(message); }

        Message& operator=(Message m) { m.swap(*this); return *this; }
        void swap(Message& m) throw () { std::swap(this->message, m.message); }

        int add(const string_type &types, ...)
        {
            va_list q;
            va_start(q, types);
            std::string t(std::string(types)+"$$");
            return add_varargs(t.c_str(), q);
        }

        int add_varargs(const string_type &types, va_list ap)
            { return lo_message_add_varargs(message, types, ap); }

        int add_int32(int32_t a)
            { return lo_message_add_int32(message, a); }

        int add_float(float a)
            { return lo_message_add_float(message, a); }

        int add_string(const string_type &a)
            { return lo_message_add_string(message, a); }

        int add_blob(lo_blob a)
            { return lo_message_add_blob(message, a); }

        int add_int64(int64_t a)
            { return lo_message_add_int64(message, a); }

        int add_timetag(lo_timetag a)
            { return lo_message_add_timetag(message, a); }

        int add_double(double a)
            { return lo_message_add_double(message, a); }

        int add_symbol(const string_type &a)
            { return lo_message_add_symbol(message, a); }

        int add_char(char a)
            { return lo_message_add_char(message, a); }

        int add_midi(uint8_t a[4])
            { return lo_message_add_midi(message, a); }

        int add_bool(bool b)
            { if (b)
                return lo_message_add_true(message);
              else
                return lo_message_add_false(message); }

        int add_true()
            { return lo_message_add_true(message); }

        int add_false()
            { return lo_message_add_false(message); }

        int add_nil()
            { return lo_message_add_nil(message); }

        int add_infinitum()
            { return lo_message_add_infinitum(message); }

        // Note, for polymorphic versions of "add", below, we can't do
        // this for "string" or "symbol" types, since it is ambiguous
        // with "add(types, ...)" above.

        int add(int32_t a)
            { return lo_message_add_int32(message, a); }

        int add(float a)
            { return lo_message_add_float(message, a); }

        int add(lo_blob a)
            { return lo_message_add_blob(message, a); }

        int add(int64_t a)
            { return lo_message_add_int64(message, a); }

        int add(lo_timetag a)
            { return lo_message_add_timetag(message, a); }

        int add(double a)
            { return lo_message_add_double(message, a); }

        int add(char a)
            { return lo_message_add_char(message, a); }

        int add(uint8_t a[4])
            { return lo_message_add_midi(message, a); }

        int add(bool b)
            { if (b)
                return lo_message_add_true(message);
              else
                return lo_message_add_false(message); }

        Address source() const
            { return Address(lo_message_get_source(message), false); }

        lo_timetag timestamp() const
            { return lo_message_get_timestamp(message); }

        std::string types() const
            { return std::string(lo_message_get_types(message)?:""); }

        int argc() const
            { return lo_message_get_argc(message); }

        lo_arg **argv() const
            { return lo_message_get_argv(message); }

        size_t length(const string_type &path) const
            { return lo_message_length(message, path); }

        void *serialise(const string_type &path, void *to, size_t *size) const
            { return lo_message_serialise(message, path, to, size); }

        typedef std::pair<int, Message> maybe;

        static
        maybe deserialise(void *data, size_t size)
            { int result = 0;
              lo_message m = lo_message_deserialise(data, size, &result);
              return maybe(result, m); }

        void print() const
            { lo_message_pp(message); }

        lo::Message clone() const
            { return lo::Message(lo_message_clone(message)); }

        operator lo_message() const
            { return message; }

      protected:
        lo_message message;
    };

    class Server
    {
      public:
        Server(lo_server s) : server(s) {}

        template <typename E>
        Server(const num_string_type &port, E&& e)
            : Server(lo_server_new(port,
              [](int num, const char *msg, const char *where){
                auto h = static_cast<handler_error*>(lo_error_get_context());
                (*h)(num, msg, where);
              }))
        {
            if (server) {
                lo_server_set_error_context(server,
					    (_error_handler = std::unique_ptr<handler>(
										       new handler_error(e))).get());
            }
        }

        template <typename E>
        Server(const num_string_type &port, int proto, E&& e=0)
            : Server(lo_server_new_with_proto(port, proto,
              [](int num, const char *msg, const char *where){
                auto h = static_cast<handler_error*>(lo_error_get_context());
                (*h)(num, msg, where);
              }))
        {
            if (server) {
                lo_server_set_error_context(server,
					    (_error_handler = std::unique_ptr<handler>(
										       new handler_error(e))).get());
            }
        }

        template <typename E>
        Server(const string_type &group, const num_string_type &port,
               const string_type &iface=0, const string_type &ip=0, E&& e=0)
            : Server((!iface._s || !ip._s)
                     ? lo_server_new_multicast_iface(group, port, iface, ip,
                           [](int num, const char *msg, const char *where){
                               auto h = static_cast<handler_error*>(lo_error_get_context());
                               (*h)(num, msg, where);
                           })
                     : lo_server_new_multicast(group, port,
                           [](int num, const char *msg, const char *where){
                               auto h = static_cast<handler_error*>(lo_error_get_context());
                               (*h)(num, msg, where);
                       }))
        {
            if (server) {
                lo_server_set_error_context(server,
					    (_error_handler = std::unique_ptr<handler>(
										       new handler_error(e))).get());
            }
        }

        Server(const num_string_type &port, lo_err_handler err_h=0)
            : Server(lo_server_new(port, err_h)) {}

        Server(const num_string_type &port, int proto, lo_err_handler err_h=0)
            : Server(lo_server_new_with_proto(port, proto, err_h)) {}

        Server(const string_type &group, const num_string_type &port,
               const string_type &iface="", const string_type &ip="", lo_err_handler err_h=0)
            : Server((iface._s || ip._s)
                     ? lo_server_new_multicast_iface(group, port,
                                                     iface._s?:0,
                                                     ip._s?:0, err_h)
                     : lo_server_new_multicast(group, port, err_h)) {}

        virtual ~Server()
            { if (server) lo_server_free(server); }

        bool is_valid() const { return server!=nullptr; }

        // Regular old liblo method handlers
        void add_method(const string_type &path, const string_type &types,
                        lo_method_handler h, void *data) const
            { _add_method(path, types, h, data); }

        // Alternative callback prototypes
        LO_ADD_METHOD( (const char*, const char*, lo_arg**, int),
                       ((char*)0, (char*)0, (lo_arg**)0, (int)0),
                       (path, types, argv, argc) );
        LO_ADD_METHOD( (const char*, lo_arg**, int),
                       ((char*)0, (lo_arg**)0, (int)0),
                       (types, argv, argc) );
        LO_ADD_METHOD( (const char*, lo_arg**, int, const Message&),
                       ((char*)0, (lo_arg**)0, (int)0, Message((lo_message)0)),
                       (types, argv, argc, Message(msg)) );
        LO_ADD_METHOD( (const char*, const Message&),
                       ((char*)0, Message((lo_message)0)),
                       (path, Message(msg)) );
        LO_ADD_METHOD( (lo_arg**, int), ((lo_arg**)0, (int)0), (argv, argc) )
        LO_ADD_METHOD( (lo_arg**, int, const Message& ),
                       ((lo_arg**)0, (int)0, Message((lo_message)0)),
                       (argv, argc, Message(msg)) );
        LO_ADD_METHOD( (const Message&),
                       (Message((lo_message)0)),
                       (Message(msg)) );
        LO_ADD_METHOD( (), (), () );

        void del_method(const string_type &path, const string_type &typespec)
        {
            _handlers.erase(std::string(path._s?:"") + ","
                            + (typespec._s?:""));
            lo_server_del_method(server, path, typespec);
        }

        int dispatch_data(void *data, size_t size)
            { return lo_server_dispatch_data(server, data, size); }

        int wait(int timeout)
            { return lo_server_wait(server, timeout); }

        int recv()
            { return lo_server_recv(server); }

        int recv(int timeout)
            { return lo_server_recv_noblock(server, timeout); }

        int add_bundle_handlers(lo_bundle_start_handler sh,
                                lo_bundle_end_handler eh,
                                void *user_data)
        {
            return lo_server_add_bundle_handlers(server, sh, eh, user_data);
        }

        template <typename S, typename E>
        int add_bundle_handlers(S&& s, E&& e)
        {
            _bundle_handlers.reset(new std::pair<handler_bundle_start,
                                                 handler_bundle_end>(
                                       handler_bundle_start(s),
                                       handler_bundle_end(e)));
            return lo_server_add_bundle_handlers(
                server,
                [](lo_timetag time, void *user_data)->int{
                    auto h = (std::pair<handler_bundle_start,
                                        handler_bundle_end>*) user_data;
                    h->first(time);
                    return 0;
                },
                [](void *user_data)->int{
                    auto h = (std::pair<handler_bundle_start,
                                        handler_bundle_end>*) user_data;
                    h->second();
                    return 0;
                },
                _bundle_handlers.get());
        }

        int socket_fd() const
            { return lo_server_get_socket_fd(server); }

        int port() const
            { return lo_server_get_port(server); }

        int protocol() const
            { return lo_server_get_protocol(server); }

        std::string url() const
            { return std::string(lo_server_get_url(server)?:""); }

        int enable_queue(int queue_enabled,
                         int dispatch_remaining=1)
            { return lo_server_enable_queue(server,
                                            queue_enabled,
                                            dispatch_remaining); }

        int events_pending() const
            { return lo_server_events_pending(server); }

        double next_event_delay() const
            { return lo_server_next_event_delay(server); }

        operator lo_server() const
            { return server; }

      protected:
        lo_server server;

        friend class ServerThread;

        class handler {};
        template <typename T>
        class handler_type : public handler, public std::function<T> {
          public: template<typename H>handler_type(H&& h) : std::function<T>(h) {}
        };
        typedef handler_type<void(int, const char *, const char *)> handler_error;
        typedef handler_type<void(int, const std::string&, const std::string&)> handler_error_s;
        typedef handler_type<void(lo_timetag)> handler_bundle_start;
        typedef handler_type<void()> handler_bundle_end;

        // Keep std::functions here so they are freed correctly
        std::unordered_map<std::string,
            std::list<std::unique_ptr<handler>>> _handlers;
        std::unique_ptr<handler> _error_handler;
        std::unique_ptr<std::pair<handler_bundle_start,
                                  handler_bundle_end>> _bundle_handlers;

        virtual void _add_method(const char *path, const char *types,
                        lo_method_handler h, void *data) const
        {
            lo_server_add_method(server, path, types, h, data);
        }
    };

    class ServerThread : public Server
    {
      public:
        ServerThread(const num_string_type &port, lo_err_handler err_h=0)
            : Server(lo_server_thread_get_server(
                  server_thread = lo_server_thread_new(port, err_h))) {}

        template <typename E>
        ServerThread(const num_string_type &port, E&& e)
            : Server(lo_server_thread_get_server(
                  server_thread = lo_server_thread_new(port,
                  [](int num, const char *msg, const char *where){
                      auto h = static_cast<handler_error*>(lo_error_get_context());
                      (*h)(num, msg, where);})))
            {
                if (server_thread) {
                    auto h = new handler_error(e);
                    _error_handler.reset(h);
                    lo_server_thread_set_error_context(server_thread, h);
                }
            }

        ServerThread(const num_string_type &port, int proto, lo_err_handler err_h)
            : Server(lo_server_thread_get_server(
                  server_thread = lo_server_thread_new_with_proto(port, proto, err_h))) {}

        virtual ~ServerThread()
            { server = 0;
              if (server_thread) lo_server_thread_free(server_thread); }

        void start() { lo_server_thread_start(server_thread); }
        void stop() { lo_server_thread_stop(server_thread); }

        operator lo_server_thread() const
            { return server_thread; }

      protected:
        lo_server_thread server_thread;

        // Regular old liblo method handlers
        virtual void _add_method(const char *path, const char *types,
                                 lo_method_handler h, void *data) const
        {
            lo_server_thread_add_method(server_thread, path, types, h, data);
        }
    };

	// This function needed since lo::ServerThread doesn't
	// properly auto-upcast to lo::Server -> lo_server.  (Because
	// both lo_server and lo_serverthread are typedef'd as void*)
	int Address::send_from(lo::ServerThread &from, const string_type &path,
						   const string_type &type, ...) const
	{
		va_list q;
		va_start(q, type);
		lo_message m = lo_message_new();
		std::string t = std::string(type) + "$$";
		lo_message_add_varargs(m, t.c_str(), q);
		lo_server s = static_cast<lo::Server&>(from);
		int r = lo_send_message_from(address, s, path, m);
		lo_message_free(m);
		return r;
	}

    class Blob
    {
      public:
        Blob(int32_t size, const void *data=0)
            : blob(lo_blob_new(size, data)) {}

        template <typename T>
        Blob(const T &t)
            : blob(lo_blob_new(t.size()*sizeof(t[0]), &t[0])) {}

        virtual ~Blob()
            { lo_blob_free(blob); }

        Blob& operator=(Blob b) { b.swap(*this); return *this; }
        void swap(Blob& b) throw () { std::swap(this->blob, b.blob); }

        uint32_t datasize() const
            { return lo_blob_datasize(blob); }

        void *dataptr() const
            { return lo_blob_dataptr(blob); }

        uint32_t size() const
            { return lo_blobsize(blob); }

        operator lo_blob() const
            { return blob; };

      protected:
        lo_blob blob;
    };

    struct PathMsg
    {
        PathMsg() {}
        PathMsg(const string_type _path, const Message& _msg)
          : path(_path), msg(_msg) {}
        std::string path;
        Message msg;
    };

    class Bundle
    {
      public:
        template <typename T>
        struct ElementT
        {
            ElementT()
            : type((lo_element_type)0), pm("", 0), bundle((lo_bundle)0) {}
            ElementT(const string_type _path, const Message& _msg)
              : type(LO_ELEMENT_MESSAGE),
                pm(PathMsg(_path, _msg)),
                bundle((lo_bundle)0) {}
            ElementT(const T& _bundle)
              : type(LO_ELEMENT_BUNDLE), pm("", 0), bundle(_bundle) {}
            lo_element_type type;
            PathMsg pm;
            T bundle;
        };
        typedef ElementT<Bundle> Element;

        Bundle() { bundle = lo_bundle_new(LO_TT_IMMEDIATE); lo_bundle_incref(bundle); }

        Bundle(lo_timetag tt)
            : bundle(lo_bundle_new(tt)) { lo_bundle_incref(bundle); }

        Bundle(lo_bundle b)
            : bundle(b) { if (b) { lo_bundle_incref(b); } }

        Bundle(const string_type &path, lo_message m,
               lo_timetag tt=LO_TT_IMMEDIATE)
            : bundle(lo_bundle_new(tt))
        {
            lo_bundle_incref(bundle);
            lo_bundle_add_message(bundle, path, m);
        }

        Bundle(const std::initializer_list<Element> &elements,
               lo_timetag tt=LO_TT_IMMEDIATE)
            : bundle(lo_bundle_new(tt))
        {
            lo_bundle_incref(bundle);
            for (auto const &e : elements) {
                if (e.type == LO_ELEMENT_MESSAGE) {
                    lo_bundle_add_message(bundle, e.pm.path.c_str(), e.pm.msg);
                }
                else if (e.type == LO_ELEMENT_BUNDLE) {
                    lo_bundle_add_bundle(bundle, e.bundle);
                }
            }
        }

        Bundle(const Bundle &b)
            : Bundle((lo_bundle)b) {}

        ~Bundle()
            { if (bundle) lo_bundle_free_recursive(bundle); }

        Bundle& operator=(Bundle b) { b.swap(*this); return *this; }
        void swap(Bundle& b) throw () { std::swap(this->bundle, b.bundle); }

        int add(const string_type &path, lo_message m)
            { return lo_bundle_add_message(bundle, path, m); }

        int add(const lo_bundle b)
            { return lo_bundle_add_bundle(bundle, b); }

        size_t length() const
            { return lo_bundle_length(bundle); }

        unsigned int count() const
            { return lo_bundle_count(bundle); }

        lo_message get_message(int index, const char **path=0) const
            { return lo_bundle_get_message(bundle, index, path); }

        Message get_message(int index, std::string &path) const
            { const char *p;
              lo_message m=lo_bundle_get_message(bundle, index, &p);
              path = p?:0;
              return Message(m); }

        PathMsg get_message(int index) const
            { const char *p;
              lo_message m = lo_bundle_get_message(bundle, index, &p);
              return PathMsg(p?:0, m); }

        Bundle get_bundle(int index) const
            { return lo_bundle_get_bundle(bundle, index); }

        Element get_element(int index, const char **path=0) const
            {
                switch (lo_bundle_get_type(bundle, index)) {
                case LO_ELEMENT_MESSAGE: {
                    const char *p;
                    lo_message m = lo_bundle_get_message(bundle, index, &p);
                    return Element(p, m);
                }
                case LO_ELEMENT_BUNDLE:
                    return Element(lo_bundle_get_bundle(bundle, index));
                default:
                    return Element();
                }
            }

        lo_timetag timestamp()
            { return lo_bundle_get_timestamp(bundle); }

        void *serialise(void *to, size_t *size) const
            { return lo_bundle_serialise(bundle, to, size); }

        void print() const
            { lo_bundle_pp(bundle); }

        operator lo_bundle() const
            { return bundle; }

      protected:
        lo_bundle bundle;
    };

    std::string version() {
        char str[32];
        lo_version(str, 32, 0, 0, 0, 0, 0, 0, 0);
        return std::string(str);
    }

    lo_timetag now() { lo_timetag tt; lo_timetag_now(&tt); return tt; }
    lo_timetag immediate() { return LO_TT_IMMEDIATE; }
};

#endif // _LO_CPP_H_