annotate src/capnproto-0.6.0/c++/samples/calculator.capnp @ 147:45360b968bf4

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