aboutsummaryrefslogtreecommitdiffstats
path: root/include/afb
diff options
context:
space:
mode:
authorLoïc Collignon <loic.collignon@iot.bzh>2019-01-04 16:50:06 +0100
committerLoïc Collignon <loic.collignon@iot.bzh>2019-03-11 13:40:45 +0100
commit3f367d8ef3409d3f671816d1a8e4c1504838ad7e (patch)
tree3f53d60035f74f2073459f49257b868c93086895 /include/afb
parentcadb0673ff6f125640c504690d6fb4e3e935665b (diff)
c++: Add the wrapper class 'afb::api'
New 'api' wrapper for 'afb_api_t' related functions. Change-Id: I695b65ff3d27b0ecc0f95f660a13b7a98fd92223 Signed-off-by: Loïc Collignon <loic.collignon@iot.bzh>
Diffstat (limited to 'include/afb')
-rw-r--r--include/afb/c++/binding-wrap.hpp156
1 files changed, 155 insertions, 1 deletions
diff --git a/include/afb/c++/binding-wrap.hpp b/include/afb/c++/binding-wrap.hpp
index 6fdf0f62..ae0e5eeb 100644
--- a/include/afb/c++/binding-wrap.hpp
+++ b/include/afb/c++/binding-wrap.hpp
@@ -42,6 +42,7 @@ namespace afb {
/* pre-declaration of classes */
/*************************************************************************/
+class api;
class arg;
class event;
class req;
@@ -86,6 +87,92 @@ bool callsync(const char *api, const char *verb, struct json_object *args, struc
/* effective declaration of classes */
/*************************************************************************/
+/* apis */
+class api
+{
+protected:
+ afb_api_t api_;
+public:
+ using call_cb = void (*)(void *closure, struct json_object *object, const char *error, const char *info, afb_api_t api);
+ using queue_cb = void (*)(int signum, void *arg);
+ using event_cb = void (*)(void *, const char *, struct json_object *, afb_api_t);
+ using preinit_cb = int (*)(void *, afb_api_t);
+ using verb_cb = void (*)(afb_req_t req);
+ using onevent_cb = void (*)(afb_api_t api, const char *event, struct json_object *object);
+ using oninit_cb = int (*)(afb_api_t api);
+
+ api();
+ api(afb_api_t a);
+ api(const api &other) = delete;
+ api(api &&other);
+ ~api();
+ api &operator=(const api &other) = delete;
+ api &operator=(api &&other);
+
+ operator afb_api_t() const;
+ afb_api_t operator->() const;
+
+ /* General functions */
+ const char *name() const;
+ void *get_userdata() const;
+ void set_userdata(void *value) const;
+ int require_api(const char *name, int initialized) const;
+ int require_api(const std::string &name, int initialized) const;
+
+ /* Verbosity functions */
+ int wants_log_level(int level) const;
+ void vverbose(int level, const char *file, int line, const char *func, const char *fmt, va_list args) const;
+ void verbose(int level, const char *file, int line, const char *func, const char *fmt, ...) const;
+
+ /* Data retrieval functions */
+ int rootdir_get_fd() const;
+ int rootdir_open_locale(const char *filename, int flags, const char *locale) const;
+ int rootdir_open_locale(const std::string &filename, int flags, const std::string &locale) const;
+ struct json_object *settings() const;
+
+ /* Calls and job functions */
+ void call(const char *apiname, const char *verb, struct json_object *args, call_cb callback, void *closure) const;
+ void call(const std::string &apiname, const std::string &verb, struct json_object *args, call_cb callback, void *closure) const;
+ int call_sync(const char *apiname, const char *verb, struct json_object *args, struct json_object **object, char **error, char **info) const;
+ int call_sync(const std::string &apiname, const std::string &verb, struct json_object *args, struct json_object **object, std::string &error, std::string &info) const;
+ int queue_job(queue_cb callback, void *argument, void *group, int timeout) const;
+
+ /* Event functions */
+ int broadcast_event(const char *name, struct json_object *object) const;
+ int broadcast_event(const std::string &name, struct json_object *object) const;
+ event make_event(const char *name) const;
+ event make_event(const std::string &name) const;
+ int event_handler_add(const char *pattern, event_cb callback, void *closure) const;
+ int event_handler_add(const std::string &pattern, event_cb callback, void *closure) const;
+ int event_handler_del(const char *pattern, void **closure) const;
+ int event_handler_del(const std::string &pattern, void **closure) const;
+
+ /* Systemd functions */
+ struct sd_event *get_event_loop() const;
+ struct sd_bus *get_user_bus() const;
+ struct sd_bus *get_system_bus() const;
+
+ /* Dynamic api functions */
+ api new_api(const char *apiname, const char *info, int noconcurrency, preinit_cb preinit, void *closure) const;
+ api new_api(const std::string &apiname, const std::string &info, int noconcurrency, preinit_cb preinit, void *closure) const;
+ int set_verbs(const struct afb_verb_v2 *verbs) const;
+ int set_verbs(const struct afb_verb_v3 *verbs) const;
+ int add_verb(const char *verb, const char *info, verb_cb callback, void *vcbdata, const struct afb_auth *auth, uint32_t session, int glob) const;
+ int add_verb(const std::string &verb, const std::string &info, verb_cb callback, void *vcbdata, const struct afb_auth *auth, uint32_t session, int glob) const;
+ int del_verb(const char *verb, void **vcbdata) const;
+ int del_verb(const std::string &verb, void **vcbdata) const;
+ int on_event(onevent_cb onevent) const;
+ int on_init(oninit_cb oninit) const;
+ int provide_class(const char *name) const;
+ int provide_class(const std::string &name) const;
+ int require_class(const char *name) const;
+ int require_class(const std::string &name) const;
+ void seal() const;
+ int delete_api() const;
+ int add_alias(const char *name, const char *as_name) const;
+ int add_alias(const std::string &name, const std::string &as_name) const;
+};
+
/* events */
class event
{
@@ -234,6 +321,74 @@ public:
/* effective declaration of classes */
/*************************************************************************/
+/* apis */
+inline api::api() : api_{nullptr} { }
+inline api::api(afb_api_t a) : api_{a} { }
+inline api::api(api &&other) : api_{other.api_} { other.api_ = nullptr; }
+inline api::~api() { api_ = nullptr; }
+inline api &api::operator=(api &&other) { api_ = other.api_; other.api_ = nullptr; return *this;}
+inline api::operator afb_api_t() const { return api_; }
+inline afb_api_t api::operator->() const { return api_; }
+inline const char *api::name() const { return afb_api_name(api_); }
+inline void *api::get_userdata() const { return afb_api_get_userdata(api_); }
+inline void api::set_userdata(void *value) const { afb_api_set_userdata(api_, value); }
+inline int api::require_api(const char *name, int initialized) const { return afb_api_require_api(api_, name, initialized); }
+inline int api::require_api(const std::string& name, int initialized) const { return afb_api_require_api(api_, name.c_str(), initialized); }
+inline int api::wants_log_level(int level) const { return afb_api_wants_log_level(api_, level); }
+inline void api::vverbose(int level, const char *file, int line, const char *func, const char *fmt, va_list args) const { afb_api_vverbose(api_, level, file, line, func, fmt, args); }
+inline void api::verbose(int level, const char *file, int line, const char *func, const char *fmt, ...) const
+{
+ va_list args;
+ va_start(args, fmt);
+ vverbose(level, file, line, func, fmt, args);
+ va_end(args);
+}
+inline int api::rootdir_get_fd() const { return afb_api_rootdir_get_fd(api_); }
+inline int api::rootdir_open_locale(const char *filename, int flags, const char *locale) const { return afb_api_rootdir_open_locale(api_, filename, flags, locale); }
+inline int api::rootdir_open_locale(const std::string &filename, int flags, const std::string &locale) const { return afb_api_rootdir_open_locale(api_, filename.c_str(), flags, locale.c_str()); }
+inline struct json_object *api::settings() const { return afb_api_settings(api_); }
+inline void api::call(const char *apiname, const char *verb, struct json_object *args, call_cb callback, void *closure) const { afb_api_call(api_, apiname, verb, args, callback, closure); }
+inline void api::call(const std::string &apiname, const std::string &verb, struct json_object *args, call_cb callback, void *closure) const { afb_api_call(api_, apiname.c_str(), verb.c_str(), args, callback, closure); }
+inline int api::call_sync(const char *apiname, const char *verb, struct json_object *args, struct json_object **object, char **error, char **info) const { return afb_api_call_sync(api_, apiname, verb, args, object, error, info); }
+inline int api::call_sync(const std::string &apiname, const std::string &verb, struct json_object *args, struct json_object **object, std::string &error, std::string& info) const
+{
+ char *err, *inf;
+ int ret = afb_api_call_sync(api_, apiname.c_str(), verb.c_str(), args, object, &err, &inf);
+ error = err;
+ info = inf;
+ return ret;
+}
+inline int api::queue_job(queue_cb callback, void *argument, void *group, int timeout) const { return afb_api_queue_job(api_, callback, argument, group, timeout); }
+inline int api::broadcast_event(const char *name, struct json_object *object) const { return afb_api_broadcast_event(api_, name, object); }
+inline int api::broadcast_event(const std::string &name, struct json_object *object) const { return afb_api_broadcast_event(api_, name.c_str(), object); }
+inline event api::make_event(const char *name) const { return event(afb_api_make_event(api_, name)); }
+inline event api::make_event(const std::string &name) const { return event(afb_api_make_event(api_, name.c_str())); }
+inline int api::event_handler_add(const char *pattern, event_cb callback, void *closure) const { return afb_api_event_handler_add(api_, pattern, callback, closure); }
+inline int api::event_handler_add(const std::string &pattern, event_cb callback, void *closure) const { return afb_api_event_handler_add(api_, pattern.c_str(), callback, closure); }
+inline int api::event_handler_del(const char *pattern, void **closure) const { return afb_api_event_handler_del(api_, pattern, closure); }
+inline int api::event_handler_del(const std::string &pattern, void **closure) const { return afb_api_event_handler_del(api_, pattern.c_str(), closure); }
+inline struct sd_event *api::get_event_loop() const { return afb_api_get_event_loop(api_); }
+inline struct sd_bus *api::get_user_bus() const { return afb_api_get_user_bus(api_); }
+inline struct sd_bus *api::get_system_bus() const { return afb_api_get_system_bus(api_); }
+inline api api::new_api(const char *apiname, const char *info, int noconcurrency, preinit_cb preinit, void *closure) const { return api(afb_api_new_api(api_, apiname, info, noconcurrency, preinit, closure)); }
+inline api api::new_api(const std::string &apiname, const std::string &info, int noconcurrency, preinit_cb preinit, void *closure) const { return api(afb_api_new_api(api_, apiname.c_str(), info.c_str(), noconcurrency, preinit, closure)); }
+inline int api::set_verbs(const struct afb_verb_v2 *verbs) const { return afb_api_set_verbs_v2(api_, verbs); }
+inline int api::set_verbs(const struct afb_verb_v3 *verbs) const { return afb_api_set_verbs_v3(api_, verbs); }
+inline int api::add_verb(const char *verb, const char *info, verb_cb callback, void *vcbdata, const struct afb_auth *auth, uint32_t session, int glob) const { return afb_api_add_verb(api_, verb, info, callback, vcbdata, auth, session, glob); }
+inline int api::add_verb(const std::string &verb, const std::string &info, verb_cb callback, void *vcbdata, const struct afb_auth *auth, uint32_t session, int glob) const { return afb_api_add_verb(api_, verb.c_str(), info.c_str(), callback, vcbdata, auth, session, glob); }
+inline int api::del_verb(const char *verb, void **vcbdata) const { return afb_api_del_verb(api_, verb, vcbdata); }
+inline int api::del_verb(const std::string &verb, void **vcbdata) const { return afb_api_del_verb(api_, verb.c_str(), vcbdata); }
+inline int api::on_event(onevent_cb onevent) const { return afb_api_on_event(api_, onevent); }
+inline int api::on_init(oninit_cb oninit) const { return afb_api_on_init(api_, oninit); }
+inline int api::provide_class(const char *name) const { return afb_api_provide_class(api_, name); }
+inline int api::provide_class(const std::string &name) const { return afb_api_provide_class(api_, name.c_str()); }
+inline int api::require_class(const char *name) const { return afb_api_require_class(api_, name); }
+inline int api::require_class(const std::string &name) const { return afb_api_require_class(api_, name.c_str()); }
+inline void api::seal() const { afb_api_seal(api_); }
+inline int api::delete_api() const { return afb_api_delete_api(api_); }
+inline int api::add_alias(const char *name, const char *as_name) const { return afb_api_add_alias(api_, name, as_name); }
+inline int api::add_alias(const std::string &name, const std::string &as_name) const { return afb_api_add_alias(api_, name.c_str(), as_name.c_str()); }
+
/* events */
inline event::event() : event_{nullptr} { }
inline event::event(afb_event_t e) : event_{e} { }
@@ -273,7 +428,6 @@ inline const char *arg::path() const { return arg_.path; }
/* req(uests)s */
-
inline req::req(afb_req_t r) : req_(r) {}
inline req::req(const req &other) : req_(other.req_) {}
inline req &req::operator=(const req &other) { req_ = other.req_; return *this; }
id='n685' href='#n685'>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 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307