[go: up one dir, main page]

Menu

[597796]: / lo / lo_cpp.h  Maximize  Restore  History

Download this file

1120 lines (936 with data), 41.7 kB

   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
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
#ifndef _LO_CPP_H_
#define _LO_CPP_H_
#include <lo/lo.h>
#include <lo/lo_throw.h>
#include <functional>
#include <memory>
#include <list>
#include <algorithm>
#include <unordered_map>
#include <string>
#if __cplusplus >= 201703L
#include <string_view>
#endif
#include <initializer_list>
#ifndef LO_USE_EXCEPTIONS
#include <cassert>
#endif
/**
* \file lo_cpp.h The liblo C++ wrapper
*/
/**
* \defgroup liblocpp C++ wrapper
*
* This is a header-only C++11 wrapper defining a set of classes that
* wrap liblo functionality in an object-oriented way.
*
* The classes are meant to be used instead of the C structs defined
* by liblo, and can be used to do nice C++11 things like assigning
* methods as lambda functions and other types of callbacks,
* supporting a variety of parameter combinations, as well as to
* create messages and bundles of messages using a nice initializer
* list syntax.
*
* Please see examples/cpp_example.cpp for more information on how to
* use it.
*
* @{
*/
#define LO_ADD_METHOD_RT(ht, argtypes, args, rt, r, r1, r2) \
template <typename H> \
Method add_method(const string_type path, const string_type types, \
H&& h, rt* _unused=0) \
{ \
std::string key(path.s() + "," + types.s()); \
_handlers[key].push_front( \
std::unique_ptr<handler>(new handler_type<r ht>(h))); \
lo_method m = _add_method(path, types, \
[](const char *path, const char *types, \
lo_arg **argv, int argc, lo_message msg, \
void *data)->int \
{ \
r1 (*static_cast<handler_type<r ht>*>(data)) args; \
r2; \
}, _handlers[key].front().get()); \
_handlers[key].front()->method = m; \
return m; \
}
#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 string_type& s) { _s = s._s; }
string_type(const char *s=0) { _s = s; }
string_type(const std::string &s) { _s = s.c_str(); }
#if __cplusplus >= 201703L
string_type(const std::string_view& s) {
if (s[s.length()]==0) _s = s.data();
else { _p.reset(new std::string(s)); _s = _p->c_str(); }
}
#endif
operator const char*() const { return _s; }
std::string s() const { return _s?_s:""; }
const char *_s;
std::unique_ptr<std::string> _p;
};
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) {}
#if __cplusplus >= 201703L
num_string_type(const std::string_view& s) : string_type(s) {}
#endif
num_string_type(int n)
{_p.reset(new std::string(std::to_string(n))); _s = _p->c_str(); }
};
/*
* Error handling:
*
* Define LO_USE_EXCEPTIONS to throw the following exceptions instead
* of aborting on error. The alternative (and default) is that
* assert() will crash your program in debug mode, and you should
* check is_valid() before operations that might break the assertion.
*
* Note that in the latter case, the program may return a C++ class
* that will not contain a valid liblo object, this is why
* LO_CHECK_AFTER does not do anything; it is up to user code to check
* is_valid() after constructing Server() and ServerThread(). On the
* contrary, when LO_USE_EXCEPTIONS is enabled, an Error exception
* will be thrown if the object was not successfully created.
*
* Rules:
*
* - Constructors that create underlying liblo objects shall either
* fail silently, depending on calling code to check is_valid(), or
* throw lo::Error() in the case of LO_USE_EXCEPTIONS.
*
* - Constructors that receive an existing liblo object do not throw
* any exceptions if the passed in object is nullptr.
*
* - All other functions shall assert() or throw lo::Invalid() if the
* underlying liblo object is not valid.
*
*/
#ifdef LO_USE_EXCEPTIONS
struct Invalid {};
struct Error {};
#define LO_CHECK_BEFORE if (!is_valid()) throw Invalid();
#define LO_CHECK_AFTER if (!is_valid()) throw Error();
#else
#define LO_CHECK_BEFORE assert(is_valid());
#define LO_CHECK_AFTER
#endif
class ServerThread;
/** \brief Class representing an OSC method, proxy for \ref lo_method. */
class Method
{
public:
Method(lo_method m) : method(m) {}
operator lo_method() const
{ return method; }
protected:
lo_method method;
};
/** \brief Class representing an OSC destination address, proxy
* for \ref lo_address. */
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;
LO_CHECK_AFTER; }
Address(const string_type &url)
{ address = lo_address_new_from_url(url); owned=true; LO_CHECK_AFTER; }
Address(lo_address a, bool _owned=true)
{ address = a; owned=_owned; LO_CHECK_AFTER; }
~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); }
bool is_valid() const { return address!=nullptr; }
int ttl() const
{ LO_CHECK_BEFORE; return lo_address_get_ttl(address); }
void set_ttl(int ttl)
{ LO_CHECK_BEFORE; lo_address_set_ttl(address, ttl); }
int send(const string_type &path) const
{ LO_CHECK_BEFORE; 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
{
LO_CHECK_BEFORE;
va_list q;
va_start(q, type);
lo_message m = lo_message_new();
std::string t = type.s() + "$$";
lo_message_add_varargs(m, t.c_str(), q);
int r = lo_send_message(address, path, m);
lo_message_free(m);
va_end(q);
return r;
}
int send(lo_timetag ts, const string_type &path,
const string_type type, ...) const
{
LO_CHECK_BEFORE;
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);
va_end(q);
return r;
}
int send(const string_type &path, lo_message m) const
{ LO_CHECK_BEFORE; return lo_send_message(address, path, m); }
int send(lo_bundle b)
{ LO_CHECK_BEFORE; 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
{
LO_CHECK_BEFORE;
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);
va_end(q);
return r;
}
int send_from(lo_server from, lo_timetag ts,
const string_type &path,
const string_type type, ...) const
{
LO_CHECK_BEFORE;
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);
va_end(q);
return r;
}
int send_from(lo_server from, const string_type &path, lo_message m) const
{ LO_CHECK_BEFORE; return lo_send_message_from(address, from, path, m); }
int send_from(lo::ServerThread &from, lo_bundle b) const;
int send_from(lo_server from, lo_bundle b) const
{ LO_CHECK_BEFORE; return lo_send_bundle_from(address, from, b); }
int get_errno() const
{ LO_CHECK_BEFORE; return lo_address_errno(address); }
std::string errstr() const
{ LO_CHECK_BEFORE; auto s(lo_address_errstr(address)); return std::string(s?s:""); }
std::string hostname() const
{ LO_CHECK_BEFORE; auto s(lo_address_get_hostname(address)); return std::string(s?s:""); }
std::string port() const
{ LO_CHECK_BEFORE; auto s(lo_address_get_port(address)); return std::string(s?s:""); }
int protocol() const
{ LO_CHECK_BEFORE; return lo_address_get_protocol(address); }
std::string url() const
{
LO_CHECK_BEFORE;
char* s(lo_address_get_url(address));
std::string result(s?s:"");
free(s);
return result;
}
std::string iface() const
{ LO_CHECK_BEFORE; auto s(lo_address_get_iface(address)); return std::string(s?s:""); }
void set_iface(const string_type &iface, const string_type &ip)
{ LO_CHECK_BEFORE; lo_address_set_iface(address, iface, ip); }
int set_tcp_nodelay(int enable)
{ LO_CHECK_BEFORE; return lo_address_set_tcp_nodelay(address, enable); }
int set_stream_slip(lo_slip_encoding encoding)
{ LO_CHECK_BEFORE; return lo_address_set_stream_slip(address, encoding); }
operator lo_address() const
{ return address; }
protected:
lo_address address;
bool owned;
};
/** \brief Class representing an OSC message, proxy for \ref lo_message. */
class Message
{
public:
Message()
: message(lo_message_new()) { if (message) lo_message_incref(message); LO_CHECK_AFTER; }
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();
if (message) {
lo_message_incref(message);
va_list q;
va_start(q, types);
std::string t(std::string(types)+"$$");
add_varargs(t.c_str(), q);
va_end(q);
}
LO_CHECK_AFTER;
}
~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); }
bool is_valid() const { return message!=nullptr; }
int add(const string_type types, ...)
{
LO_CHECK_BEFORE;
va_list q;
va_start(q, types);
std::string t(std::string(types)+"$$");
int ret = add_varargs(t.c_str(), q);
va_end(q);
return ret;
}
int add_varargs(const string_type &types, va_list ap)
{ LO_CHECK_BEFORE; return lo_message_add_varargs(message, types, ap); }
int add_int32(int32_t a)
{ LO_CHECK_BEFORE; return lo_message_add_int32(message, a); }
int add_float(float a)
{ LO_CHECK_BEFORE; return lo_message_add_float(message, a); }
int add_string(const string_type &a)
{ LO_CHECK_BEFORE; return lo_message_add_string(message, a); }
int add_blob(lo_blob a)
{ LO_CHECK_BEFORE; return lo_message_add_blob(message, a); }
int add_int64(int64_t a)
{ LO_CHECK_BEFORE; return lo_message_add_int64(message, a); }
int add_timetag(lo_timetag a)
{ LO_CHECK_BEFORE; return lo_message_add_timetag(message, a); }
int add_double(double a)
{ LO_CHECK_BEFORE; return lo_message_add_double(message, a); }
int add_symbol(const string_type &a)
{ LO_CHECK_BEFORE; return lo_message_add_symbol(message, a); }
int add_char(char a)
{ LO_CHECK_BEFORE; return lo_message_add_char(message, a); }
int add_midi(uint8_t a[4])
{ LO_CHECK_BEFORE; return lo_message_add_midi(message, a); }
int add_bool(bool b)
{ LO_CHECK_BEFORE;
if (b)
return lo_message_add_true(message);
else
return lo_message_add_false(message); }
int add_true()
{ LO_CHECK_BEFORE; return lo_message_add_true(message); }
int add_false()
{ LO_CHECK_BEFORE; return lo_message_add_false(message); }
int add_nil()
{ LO_CHECK_BEFORE; return lo_message_add_nil(message); }
int add_infinitum()
{ LO_CHECK_BEFORE; 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)
{ LO_CHECK_BEFORE; return lo_message_add_int32(message, a); }
int add(float a)
{ LO_CHECK_BEFORE; return lo_message_add_float(message, a); }
int add(lo_blob a)
{ LO_CHECK_BEFORE; return lo_message_add_blob(message, a); }
int add(int64_t a)
{ LO_CHECK_BEFORE; return lo_message_add_int64(message, a); }
int add(lo_timetag a)
{ LO_CHECK_BEFORE; return lo_message_add_timetag(message, a); }
int add(double a)
{ LO_CHECK_BEFORE; return lo_message_add_double(message, a); }
int add(char a)
{ LO_CHECK_BEFORE; return lo_message_add_char(message, a); }
int add(uint8_t a[4])
{ LO_CHECK_BEFORE; return lo_message_add_midi(message, a); }
int add(bool b)
{ LO_CHECK_BEFORE;
if (b)
return lo_message_add_true(message);
else
return lo_message_add_false(message); }
Address source() const
{ LO_CHECK_BEFORE; return Address(lo_message_get_source(message), false); }
lo_timetag timestamp() const
{ LO_CHECK_BEFORE; return lo_message_get_timestamp(message); }
std::string types() const
{ LO_CHECK_BEFORE;
auto s(lo_message_get_types(message)); return std::string(s?s:""); }
int argc() const
{ LO_CHECK_BEFORE; return lo_message_get_argc(message); }
lo_arg **argv() const
{ LO_CHECK_BEFORE; return lo_message_get_argv(message); }
size_t length(const string_type &path) const
{ LO_CHECK_BEFORE; return lo_message_length(message, path); }
void *serialise(const string_type &path, void *to, size_t *size) const
{ LO_CHECK_BEFORE; 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_CHECK_BEFORE; lo_message_pp(message); }
lo::Message clone() const
{ LO_CHECK_BEFORE; return lo::Message(lo_message_clone(message)); }
operator lo_message() const
{ return message; }
protected:
lo_message message;
};
/** \brief Class representing a local OSC server, proxy for \ref lo_server. */
class Server
{
public:
/** Constructor. */
Server(lo_server s) : server(s) {}
/** Constructor taking an error handler. */
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());
if (h) (*h)(num, msg, where);
}))
{
if (server) {
lo_server_set_error_context(server,
(_error_handler = std::unique_ptr<handler>(
new handler_error(e))).get());
}
LO_CHECK_AFTER;
}
/** Constructor taking a port number and error handler. */
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());
}
LO_CHECK_AFTER;
}
/** Constructor taking a multicast group, port number,
* interface identifier or IP, and error handler. */
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());
}
LO_CHECK_AFTER;
}
/** Constructor taking a port number and error handler. */
Server(const num_string_type &port, lo_err_handler err_h=0)
: Server(lo_server_new(port, err_h)) { LO_CHECK_AFTER; }
/** Constructor taking a port number, protocol, and error handler. */
Server(const num_string_type &port, int proto, lo_err_handler err_h=0)
: Server(lo_server_new_with_proto(port, proto, err_h))
{ LO_CHECK_AFTER; }
/** Constructor taking a multicast group, port number,
* interface identifier or IP, and error handler. */
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, ip, err_h)
: lo_server_new_multicast(group, port, err_h))
{ LO_CHECK_AFTER; }
/** Destructor */
virtual ~Server()
{ if (server) lo_server_free(server); }
bool is_valid() const { return server!=nullptr; }
// Regular old liblo method handlers
/** Add a method to handle a given path and type, with a
* handler and user data pointer. */
Method add_method(const string_type &path, const string_type &types,
lo_method_handler h, void *data) const
{ LO_CHECK_BEFORE; return _add_method(path, types, h, data); }
// Alternative callback prototypes
/** Add a method to handle a given path and type, with a
* handler taking (argv, argc), user data. */
LO_ADD_METHOD( (const char*, const char*, lo_arg**, int),
((char*)0, (char*)0, (lo_arg**)0, (int)0),
(path, types, argv, argc) );
/** Add a method to handle a given path and type, with a
* handler taking (types, argv, argc), user data. */
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( (), (), () );
int del_method(const string_type &path, const string_type &typespec)
{
LO_CHECK_BEFORE;
_handlers.erase(path.s() + "," + typespec.s());
lo_server_del_method(server, path, typespec);
return 0;
}
int del_method(const lo_method& m)
{
LO_CHECK_BEFORE;
for (auto &i : _handlers) {
auto it = std::remove_if(i.second.begin(), i.second.end(),
[&](std::unique_ptr<handler>& h){return h->method == m;});
i.second.erase(it, i.second.end());
}
return lo_server_del_lo_method(server, m);
}
int dispatch_data(void *data, size_t size)
{ LO_CHECK_BEFORE; return lo_server_dispatch_data(server, data, size); }
int wait(int timeout)
{ LO_CHECK_BEFORE; return lo_server_wait(server, timeout); }
int recv()
{ LO_CHECK_BEFORE; return lo_server_recv(server); }
int recv(int timeout)
{ LO_CHECK_BEFORE; return lo_server_recv_noblock(server, timeout); }
int add_bundle_handlers(lo_bundle_start_handler sh,
lo_bundle_end_handler eh,
void *user_data)
{
LO_CHECK_BEFORE;
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
{ LO_CHECK_BEFORE; return lo_server_get_socket_fd(server); }
int port() const
{ LO_CHECK_BEFORE; return lo_server_get_port(server); }
int protocol() const
{ LO_CHECK_BEFORE; return lo_server_get_protocol(server); }
std::string url() const
{
LO_CHECK_BEFORE;
char* s(lo_server_get_url(server));
std::string result(s?s:"");
free(s);
return result;
}
int enable_queue(int queue_enabled,
int dispatch_remaining=1)
{ LO_CHECK_BEFORE;
return lo_server_enable_queue(server,
queue_enabled,
dispatch_remaining); }
int events_pending() const
{ LO_CHECK_BEFORE; return lo_server_events_pending(server); }
double next_event_delay() const
{ LO_CHECK_BEFORE; return lo_server_next_event_delay(server); }
operator lo_server() const
{ return server; }
protected:
lo_server server;
friend class ServerThread;
struct handler { Method method; handler(Method m):method(m){} };
template <typename T>
class handler_type : public handler, public std::function<T> {
public: template<typename H>handler_type(H&& h, Method m=0)
: handler(m), 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 Method _add_method(const char *path, const char *types,
lo_method_handler h, void *data) const
{
LO_CHECK_BEFORE;
return lo_server_add_method(server, path, types, h, data);
}
};
/** \brief Class representing a server thread, proxy for \ref lo_server_thread. */
class ServerThread : public Server
{
public:
ServerThread(const num_string_type &port, lo_err_handler err_h=0)
: Server(0)
{ server_thread = lo_server_thread_new(port, err_h);
if (server_thread)
server = lo_server_thread_get_server(server_thread);
LO_CHECK_AFTER; }
template <typename E>
ServerThread(const num_string_type &port, E&& e)
: Server(0)
{
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());
// TODO: Can't call "e" yet since error context is not yet
// provided, port unavailable errors will not be reported!
if (h) (*h)(num, msg, where);});
if (server_thread) {
server = lo_server_thread_get_server(server_thread);
auto h = new handler_error(e);
_error_handler.reset(h);
lo_server_thread_set_error_context(server_thread, h);
lo_server_set_error_context(server,
(_error_handler = std::unique_ptr<handler>(
new handler_error(e))).get());
}
LO_CHECK_AFTER;
}
ServerThread(const num_string_type &port, int proto, lo_err_handler err_h)
: Server(0)
{ server_thread = lo_server_thread_new_with_proto(port, proto, err_h);
if (server_thread)
server = lo_server_thread_get_server(server_thread);
LO_CHECK_AFTER;
}
template <typename E>
ServerThread(const num_string_type &port, int proto, E&& e)
: Server(0)
{
server_thread = lo_server_thread_new_with_proto(port, proto,
[](int num, const char *msg, const char *where){
auto h = static_cast<handler_error*>(lo_error_get_context());
// TODO: Can't call "e" yet since error context is not yet
// provided, port unavailable errors will not be reported!
if (h) (*h)(num, msg, where);});
if (server_thread) {
server = lo_server_thread_get_server(server_thread);
auto h = new handler_error(e);
_error_handler.reset(h);
lo_server_thread_set_error_context(server_thread, h);
lo_server_set_error_context(server,
(_error_handler = std::unique_ptr<handler>(
new handler_error(e))).get());
}
LO_CHECK_AFTER;
}
ServerThread(const string_type &group, const num_string_type &port,
const string_type &iface, const string_type &ip,
lo_err_handler err_h=0) : Server(0)
{ if (iface._s || ip._s)
server_thread = lo_server_thread_new_multicast_iface(group, port,
iface, ip, err_h);
else
server_thread = lo_server_thread_new_multicast(group, port, err_h);
if (server_thread)
server = lo_server_thread_get_server(server_thread);
LO_CHECK_AFTER;
}
virtual ~ServerThread()
{ server = 0;
if (server_thread) lo_server_thread_free(server_thread); }
template <typename I, typename C>
auto set_callbacks(I&& init, C&& cleanup)
-> typename std::enable_if<
std::is_same<decltype(init()), int>::value, void>::type
{
LO_CHECK_BEFORE;
if (server_thread) {
_cb_handlers.reset(new handler_cb_pair(init, cleanup));
lo_server_thread_set_callbacks(server_thread,
[](lo_server_thread s, void *c){
auto cb = (handler_cb_pair*)c;
return (cb->first)();
},
[](lo_server_thread s, void *c){
auto cb = (handler_cb_pair*)c;
(cb->second)();
}, _cb_handlers.get());
}
}
template <typename I, typename C>
auto set_callbacks(I&& init, C&& cleanup)
-> typename std::enable_if<
std::is_same<decltype(init()), void>::value, void>::type
{
if (server_thread) {
_cb_handlers.reset(
(handler_cb_pair*)new handler_cb_pair_void(init, cleanup));
lo_server_thread_set_callbacks(server_thread,
[](lo_server_thread s, void *c){
auto cb = (handler_cb_pair_void*)c;
(cb->first)(); return 0;
},
[](lo_server_thread s, void *c){
auto cb = (handler_cb_pair_void*)c;
(cb->second)();
}, _cb_handlers.get());
}
}
void start() { LO_CHECK_BEFORE; lo_server_thread_start(server_thread); }
void stop() { LO_CHECK_BEFORE; lo_server_thread_stop(server_thread); }
operator lo_server_thread() const
{ return server_thread; }
protected:
lo_server_thread server_thread;
typedef std::pair<handler_type<int()>,handler_type<void()>> handler_cb_pair;
typedef std::pair<handler_type<void()>,handler_type<void()>> handler_cb_pair_void;
std::unique_ptr<handler_cb_pair> _cb_handlers;
// Regular old liblo method handlers
virtual Method _add_method(const char *path, const char *types,
lo_method_handler h, void *data) const
{
LO_CHECK_BEFORE;
return 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*)
inline
int Address::send_from(lo::ServerThread &from, const string_type &path,
const string_type type, ...) const
{
LO_CHECK_BEFORE;
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);
va_end(q);
return r;
}
inline
int Address::send_from(lo::ServerThread &from, lo_bundle b) const
{
LO_CHECK_BEFORE;
lo_server s = static_cast<lo::Server&>(from);
return lo_send_bundle_from(address, s, b);
}
/** \brief Class representing an OSC blob, proxy for \ref lo_blob. */
class Blob
{
public:
Blob(int32_t size, const void *data=0)
: blob(lo_blob_new(size, data)) { LO_CHECK_AFTER; }
template <typename T>
Blob(const T &t)
: blob(lo_blob_new(t.size()*sizeof(t[0]), &t[0])) { LO_CHECK_AFTER; }
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); }
bool is_valid() const { return blob!=nullptr; }
uint32_t datasize() const
{ LO_CHECK_BEFORE; return lo_blob_datasize(blob); }
void *dataptr() const
{ LO_CHECK_BEFORE; return lo_blob_dataptr(blob); }
uint32_t size() const
{ LO_CHECK_BEFORE; return lo_blobsize(blob); }
operator lo_blob() const
{ return blob; };
protected:
lo_blob blob;
};
/** \brief Class representing an OSC path (std::string) and lo::Message pair. */
struct PathMsg
{
PathMsg() {}
PathMsg(const string_type _path, const Message& _msg)
: path(_path), msg(_msg) {}
std::string path;
Message msg;
};
/** \brief Class representing an OSC bundle, proxy for \ref lo_bundle. */
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);
if (bundle) lo_bundle_incref(bundle);
LO_CHECK_AFTER; }
Bundle(lo_timetag tt)
: bundle(lo_bundle_new(tt)) { if (bundle) lo_bundle_incref(bundle);
LO_CHECK_AFTER; }
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))
{
if (bundle) {
lo_bundle_incref(bundle);
lo_bundle_add_message(bundle, path, m);
}
LO_CHECK_AFTER;
}
Bundle(const std::initializer_list<Element> &elements,
lo_timetag tt=LO_TT_IMMEDIATE)
: bundle(lo_bundle_new(tt))
{
if (bundle) {
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);
}
}
}
LO_CHECK_AFTER;
}
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); }
bool is_valid() const { return bundle!=nullptr; }
int add(const string_type &path, lo_message m)
{ LO_CHECK_BEFORE; return lo_bundle_add_message(bundle, path, m); }
int add(const lo_bundle b)
{ LO_CHECK_BEFORE; return lo_bundle_add_bundle(bundle, b); }
size_t length() const
{ LO_CHECK_BEFORE; return lo_bundle_length(bundle); }
unsigned int count() const
{ LO_CHECK_BEFORE; return lo_bundle_count(bundle); }
lo_message get_message(int index, const char **path=0) const
{ LO_CHECK_BEFORE; return lo_bundle_get_message(bundle, index, path); }
Message get_message(int index, std::string &path) const
{ LO_CHECK_BEFORE;
const char *p;
lo_message m=lo_bundle_get_message(bundle, index, &p);
path = p?p:0;
return Message(m); }
PathMsg get_message(int index) const
{ LO_CHECK_BEFORE;
const char *p;
lo_message m = lo_bundle_get_message(bundle, index, &p);
return PathMsg(p?p:0, m); }
Bundle get_bundle(int index) const
{ LO_CHECK_BEFORE; return lo_bundle_get_bundle(bundle, index); }
Element get_element(int index, const char **path=0) const
{
LO_CHECK_BEFORE;
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()
{ LO_CHECK_BEFORE; return lo_bundle_get_timestamp(bundle); }
void *serialise(void *to, size_t *size) const
{ LO_CHECK_BEFORE; return lo_bundle_serialise(bundle, to, size); }
void print() const
{ LO_CHECK_BEFORE; lo_bundle_pp(bundle); }
operator lo_bundle() const
{ return bundle; }
protected:
lo_bundle bundle;
};
/** \brief Return the library version as an std::string. */
inline std::string version() {
char str[32];
lo_version(str, 32, 0, 0, 0, 0, 0, 0, 0);
return std::string(str);
}
/** \brief Return the current time in lo_timetag format. */
inline lo_timetag now() { lo_timetag tt; lo_timetag_now(&tt); return tt; }
/** \brief Return the OSC timetag representing "immediately". */
inline lo_timetag immediate() { return LO_TT_IMMEDIATE; }
};
/** @} */
#endif // _LO_CPP_H_