annotate src/capnproto-git-20161025/c++/samples/calculator.capnp @ 83:ae30d91d2ffe

Replace these with versions built using an older toolset (so as to avoid ABI compatibilities when linking on Ubuntu 14.04 for packaging purposes)
author Chris Cannam
date Fri, 07 Feb 2020 11:51:13 +0000
parents 9530b331f8c1
children
rev   line source
cannam@48 1 # Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
cannam@48 2 # Licensed under the MIT License:
cannam@48 3 #
cannam@48 4 # Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@48 5 # of this software and associated documentation files (the "Software"), to deal
cannam@48 6 # in the Software without restriction, including without limitation the rights
cannam@48 7 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@48 8 # copies of the Software, and to permit persons to whom the Software is
cannam@48 9 # furnished to do so, subject to the following conditions:
cannam@48 10 #
cannam@48 11 # The above copyright notice and this permission notice shall be included in
cannam@48 12 # all copies or substantial portions of the Software.
cannam@48 13 #
cannam@48 14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@48 15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@48 16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@48 17 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@48 18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@48 19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@48 20 # THE SOFTWARE.
cannam@48 21
cannam@48 22 @0x85150b117366d14b;
cannam@48 23
cannam@48 24 interface Calculator {
cannam@48 25 # A "simple" mathematical calculator, callable via RPC.
cannam@48 26 #
cannam@48 27 # But, to show off Cap'n Proto, we add some twists:
cannam@48 28 #
cannam@48 29 # - You can use the result from one call as the input to the next
cannam@48 30 # without a network round trip. To accomplish this, evaluate()
cannam@48 31 # returns a `Value` object wrapping the actual numeric value.
cannam@48 32 # This object may be used in a subsequent expression. With
cannam@48 33 # promise pipelining, the Value can actually be used before
cannam@48 34 # the evaluate() call that creates it returns!
cannam@48 35 #
cannam@48 36 # - You can define new functions, and then call them. This again
cannam@48 37 # shows off pipelining, but it also gives the client the
cannam@48 38 # opportunity to define a function on the client side and have
cannam@48 39 # the server call back to it.
cannam@48 40 #
cannam@48 41 # - The basic arithmetic operators are exposed as Functions, and
cannam@48 42 # you have to call getOperator() to obtain them from the server.
cannam@48 43 # This again demonstrates pipelining -- using getOperator() to
cannam@48 44 # get each operator and then using them in evaluate() still
cannam@48 45 # only takes one network round trip.
cannam@48 46
cannam@48 47 evaluate @0 (expression :Expression) -> (value :Value);
cannam@48 48 # Evaluate the given expression and return the result. The
cannam@48 49 # result is returned wrapped in a Value interface so that you
cannam@48 50 # may pass it back to the server in a pipelined request. To
cannam@48 51 # actually get the numeric value, you must call read() on the
cannam@48 52 # Value -- but again, this can be pipelined so that it incurs
cannam@48 53 # no additional latency.
cannam@48 54
cannam@48 55 struct Expression {
cannam@48 56 # A numeric expression.
cannam@48 57
cannam@48 58 union {
cannam@48 59 literal @0 :Float64;
cannam@48 60 # A literal numeric value.
cannam@48 61
cannam@48 62 previousResult @1 :Value;
cannam@48 63 # A value that was (or, will be) returned by a previous
cannam@48 64 # evaluate().
cannam@48 65
cannam@48 66 parameter @2 :UInt32;
cannam@48 67 # A parameter to the function (only valid in function bodies;
cannam@48 68 # see defFunction).
cannam@48 69
cannam@48 70 call :group {
cannam@48 71 # Call a function on a list of parameters.
cannam@48 72 function @3 :Function;
cannam@48 73 params @4 :List(Expression);
cannam@48 74 }
cannam@48 75 }
cannam@48 76 }
cannam@48 77
cannam@48 78 interface Value {
cannam@48 79 # Wraps a numeric value in an RPC object. This allows the value
cannam@48 80 # to be used in subsequent evaluate() requests without the client
cannam@48 81 # waiting for the evaluate() that returns the Value to finish.
cannam@48 82
cannam@48 83 read @0 () -> (value :Float64);
cannam@48 84 # Read back the raw numeric value.
cannam@48 85 }
cannam@48 86
cannam@48 87 defFunction @1 (paramCount :Int32, body :Expression)
cannam@48 88 -> (func :Function);
cannam@48 89 # Define a function that takes `paramCount` parameters and returns the
cannam@48 90 # evaluation of `body` after substituting these parameters.
cannam@48 91
cannam@48 92 interface Function {
cannam@48 93 # An algebraic function. Can be called directly, or can be used inside
cannam@48 94 # an Expression.
cannam@48 95 #
cannam@48 96 # A client can create a Function that runs on the server side using
cannam@48 97 # `defFunction()` or `getOperator()`. Alternatively, a client can
cannam@48 98 # implement a Function on the client side and the server will call back
cannam@48 99 # to it. However, a function defined on the client side will require a
cannam@48 100 # network round trip whenever the server needs to call it, whereas
cannam@48 101 # functions defined on the server and then passed back to it are called
cannam@48 102 # locally.
cannam@48 103
cannam@48 104 call @0 (params :List(Float64)) -> (value :Float64);
cannam@48 105 # Call the function on the given parameters.
cannam@48 106 }
cannam@48 107
cannam@48 108 getOperator @2 (op :Operator) -> (func :Function);
cannam@48 109 # Get a Function representing an arithmetic operator, which can then be
cannam@48 110 # used in Expressions.
cannam@48 111
cannam@48 112 enum Operator {
cannam@48 113 add @0;
cannam@48 114 subtract @1;
cannam@48 115 multiply @2;
cannam@48 116 divide @3;
cannam@48 117 }
cannam@48 118 }