From dd8a568141b43546521ca438d861e00c26051f6c Mon Sep 17 00:00:00 2001 From: iphydf Date: Wed, 10 Aug 2016 13:46:04 +0100 Subject: Make self_connection_status callback stateless. **What are we doing?** We are moving towards stateless callbacks. This means that when registering a callback, you no longer pass a user data pointer. Instead, you pass a user data pointer to tox_iterate. This pointer is threaded through the code, passed to each callback. The callback can modify the data pointed at. An extra indirection will be needed if the pointer itself can change. **Why?** Currently, callbacks are registered with a user data pointer. This means the library has N pointers for N different callbacks. These pointers need to be managed by the client code. Managing the lifetime of the pointee can be difficult. In C++, it takes special effort to ensure that the lifetime of user data extends at least beyond the lifetime of the Tox instance. For other languages, the situation is much worse. Java and other garbage collected languages may move objects in memory, so the pointers are not stable. Tox4j goes through a lot of effort to make the Java/Scala user experience a pleasant one by keeping a global array of Tox+userdata on the C++ side, and communicating via protobufs. A Haskell FFI would have to do similarly complex tricks. Stateless callbacks ensure that a user data pointer only needs to live during a single function call. This means that the user code (or language runtime) can move the data around at will, as long as it sets the new location in the callback. **How?** We are doing this change one callback at a time. After each callback, we ensure that everything still works as expected. This means the toxcore change will require 15 Pull Requests. --- other/apidsl/README.md | 20 +++++++++++--------- other/apidsl/tox.in.h | 19 +++++++++++++++---- 2 files changed, 26 insertions(+), 13 deletions(-) (limited to 'other/apidsl') diff --git a/other/apidsl/README.md b/other/apidsl/README.md index 3ba44678..99694dca 100644 --- a/other/apidsl/README.md +++ b/other/apidsl/README.md @@ -18,14 +18,16 @@ If you want to do it quickly and you don't have time for anything other than cop Command to run from ``toxcore`` directory (quick way, involves using curl): ```bash -rm toxcore/tox.h && \ -( curl -X POST --data-binary @- https://apidsl.herokuapp.com/apidsl < ./other/apidsl/tox.in.h > ./toxcore/tox.h ) && \ -astyle --options=./other/astyle/astylerc ./toxcore/tox.h -``` - -When formatting will be complete, you should see output like: -``` -Formatted ./toxcore/tox.h +# For tox.h: +curl -X POST --data-binary @- https://apidsl.herokuapp.com/apidsl \ + < other/apidsl/tox.in.h \ + | astyle --options=other/astyle/astylerc \ + > toxcore/tox.h +# For toxav.h: +curl -X POST --data-binary @- https://apidsl.herokuapp.com/apidsl \ + < other/apidsl/toxav.in.h \ + | astyle --options=other/astyle/astylerc \ + > toxav/toxav.h ``` You may want to make sure with ``git diff`` that changes made in ``tox.h`` reflect changes in ``tox.in.h``. @@ -43,7 +45,7 @@ If you prefer to have more control over what is happening, there are steps below 4. Use ``apidsl`` ``??`` 5. Parse generated ``tox.h`` with astyle, minimal command for it would be: ```bash -astyle --options=./other/astyle/astylerc ./toxcore/tox.h +astyle --options=other/astyle/astylerc toxcore/tox.h ``` **Always pass output from ``apidsl`` through astyle.** diff --git a/other/apidsl/tox.in.h b/other/apidsl/tox.in.h index 577515b1..6e65ed1f 100644 --- a/other/apidsl/tox.in.h +++ b/other/apidsl/tox.in.h @@ -86,6 +86,19 @@ extern "C" { * callback will result in no callback being registered for that event. Only * one callback per event can be registered, so if a client needs multiple * event listeners, it needs to implement the dispatch functionality itself. + * + * The last argument to a callback is the user data pointer. It is passed from + * ${tox.iterate} to each callback in sequence. + * + * The user data pointer is never stored or dereferenced by any library code, so + * can be any pointer, including NULL. Callbacks must all operate on the same + * object type. In the apidsl code (tox.in.h), this is denoted with `any`. The + * `any` in ${tox.iterate} must be the same `any` as in all callbacks. In C, + * lacking parametric polymorphism, this is a pointer to void. + * + * Old style callbacks that are registered together with a user data pointer + * receive that pointer as argument when they are called. They can each have + * their own user data pointer of their own type. */ /** \subsection threading Threading implications @@ -713,7 +726,7 @@ inline namespace self { * * TODO: how long should a client wait before bootstrapping again? */ - event connection_status { + event connection_status const { /** * @param connection_status Whether we are connected to the DHT. */ @@ -734,7 +747,7 @@ const uint32_t iteration_interval(); * The main loop that needs to be run in intervals of $iteration_interval() * milliseconds. */ -void iterate(); +void iterate(any user_data); /******************************************************************************* @@ -858,7 +871,6 @@ inline namespace self { * If this parameter is NULL, the function has no effect. */ get(); - } @@ -1058,7 +1070,6 @@ namespace friend { */ const bool exists(uint32_t friend_number); - } inline namespace self { -- cgit v1.2.3