lib: intro `Float.lerp` for simple linear interpolation
[nit.git] / lib / signals.nit
index 91015da..f9709da 100644 (file)
 # limitations under the License.
 
 # Module to manage standard C signals
 # limitations under the License.
 
 # Module to manage standard C signals
+#
+# Common usage imply 5 steps:
+#
+# 1. Implement the `SignalHandler` interface
+# 2. `redef receive_signal_unsafe` to handle `sigsegv`
+# 3. `redef receive_signal` to handle other signals safely
+# 4, Notify what signals to handle with `handle_signal`
+# 5. If using the safe handler method, routinely call `check_signals`
+#
+# Usage example:
+#
+# ~~~~
+# class MyReceiver
+#      super SignalHandler
+#
+#      redef fun receive_signal(signal)
+#      do
+#              print "received safely {signal}"
+#              if signal == sigalarm then print "Alarm!"
+#      end
+#      redef fun receive_signal_unsafe( signal ) do print "received unsafely {signal}"
+# end
+#
+# var r = new MyReceiver
+#
+# # Handle `sigsegv` signal unsafely (the only way for this one)
+# r.handle_signal(sigsegv, false)
+#
+# # Handle `sigint` and `sigalarm` safely
+# r.handle_signal(sigint, true)
+# r.handle_signal(sigalarm, true)
+#
+# # Ask system to receive a `sigalarm` signal in 1 second
+# set_alarm(1)
+#
+# loop
+#      # Check signals and callback `receive_signal`
+#      var hit = check_signals
+#
+#      if hit then break
+# end
+# ~~~~
 module signals
 
 `{
 module signals
 
 `{
@@ -24,6 +66,21 @@ module signals
        #include <stdio.h>
 
        /*
        #include <stdio.h>
 
        /*
+               This guard prevents errors by the C compiler when the Nit code imports this
+               module but do not use handle_signal. When it is _not_ used, the C type
+               SignalHandler and C function SignalHandler_receive_signal are not generated.
+               Which does not please the C compiler. This guard ensure that we compile this
+               code only if the type SignalHandler has been defined.
+
+               This is a HACK, FIXME by:
+               * Adding the macro to the FFI spec, or
+               * Attach the callbacks to this code block (or the module itself)
+               * Avoid using Nit types and callbacks or use them only in the C implementation
+                 of Nit method.
+       */
+       #ifdef NIT_TYPE_SignalHandler
+
+       /*
                Structure to manage each possible signals
                are used in an array of 32 maximum signals.
                This array is global to the software.
                Structure to manage each possible signals
                are used in an array of 32 maximum signals.
                This array is global to the software.
@@ -49,6 +106,8 @@ module signals
                        }
                }
        }
                        }
                }
        }
+
+       #endif
 `}
 
 # Receives the callback from system when a given signal arise
 `}
 
 # Receives the callback from system when a given signal arise
@@ -69,8 +128,7 @@ interface SignalHandler
        #     r.handle_signal(sigint, true) # will call back when "check_signals" is called
        #     # ...
        #     check_signals # if a signal was received, it will invoke `r.receive_signal`
        #     r.handle_signal(sigint, true) # will call back when "check_signals" is called
        #     # ...
        #     check_signals # if a signal was received, it will invoke `r.receive_signal`
-       fun receive_signal(signal: Int) `{
-       `}
+       fun receive_signal(signal: Int) do end
 
        # Called immediatly on receiving an unsafe signal (should be redefed by subclasses)
        #
 
        # Called immediatly on receiving an unsafe signal (should be redefed by subclasses)
        #
@@ -88,8 +146,7 @@ interface SignalHandler
        #     
        #     var r = new MyReceiver
        #     r.handle_signal(sigsegv, false) # `r.receive_signal_unsafe` will be invoked on sigsegv
        #     
        #     var r = new MyReceiver
        #     r.handle_signal(sigsegv, false) # `r.receive_signal_unsafe` will be invoked on sigsegv
-       fun receive_signal_unsafe(signal: Int) `{
-       `}
+       fun receive_signal_unsafe(signal: Int) do end
 
        # Set the receiver as the handler of the signal
        #
 
        # Set the receiver as the handler of the signal
        #