libvata2  [unstable] git snapshot
vm-dispatch.hh
Go to the documentation of this file.
1 /* vm-dispatch.hh -- dispatcher for virtual machine function calls
2  *
3  * Copyright (c) 2018 Ondrej Lengal <ondra.lengal@gmail.com>
4  *
5  * This file is a part of libvata2.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  */
17 
18 // The purpose of this code is to provide an interface through which classes
19 // for automata types could register their 'dispatcher' functions for
20 // operations called from within the virtual machine. The code is deliberately
21 // made separate from vata2/vm.hh in order to decrease the number of
22 // dependencies.
23 
24 #ifndef _VM_DISPATCH_HH_
25 #define _VM_DISPATCH_HH_
26 
27 // VATA headers
28 #include <vata2/vm.hh>
29 
30 namespace Vata2
31 {
32 namespace VM
33 {
34 
35 /// Data type for function names
36 using VMFuncName = std::string;
37 /// Data type for function arguments
38 using VMFuncArgs = std::vector<VMValue>;
39 /// Data type for dispatcher function pointer
40 using VMDispatcherFunc = std::function<VMValue(const VMFuncName&, const VMFuncArgs&)>;
41 
42 
43 /// registers a dispatcher function for a VATA data type
44 void reg_dispatcher(
45  const std::string& type_name,
46  const VMDispatcherFunc& func);
47 
48 
49 /// finds the dispatcher function for a given type
50 const VMDispatcherFunc& find_dispatcher(const std::string& type_name);
51 
52 
53 /// calls a dispatcher function for the given value with it as the only argument
55  const VMValue& val,
56  const VMFuncName& func_name)
57 { return find_dispatcher(val.type)(func_name, {val}); }
58 
59 
60 /// Default dispatcher function
61 VMValue default_dispatch(
62  const VMFuncName& func_name,
63  const VMFuncArgs& func_args);
64 
65 
66 // CLOSING NAMESPACES AND GUARDS
67 } /* VM */
68 } /* Vata2 */
69 
70 #endif /* _VM_DISPATCH_HH_ */
const VMDispatcherFunc & find_dispatcher(const std::string &type_name)
finds the dispatcher function for a given type
std::function< VMValue(const VMFuncName &, const VMFuncArgs &)> VMDispatcherFunc
Data type for dispatcher function pointer.
Definition: vm-dispatch.hh:40
void reg_dispatcher(const std::string &type_name, const VMDispatcherFunc &func)
registers a dispatcher function for a VATA data type
std::string VMFuncName
Data type for function names.
Definition: vm-dispatch.hh:36
std::vector< VMValue > VMFuncArgs
Data type for function arguments.
Definition: vm-dispatch.hh:38
std::string type
name of the type
Definition: vm.hh:42
VMValue call_dispatch_with_self(const VMValue &val, const VMFuncName &func_name)
calls a dispatcher function for the given value with it as the only argument
Definition: vm-dispatch.hh:54
Data type representing a value, which is composed of a type and a pointer to a general memory...
Definition: vm.hh:38
VMValue default_dispatch(const VMFuncName &func_name, const VMFuncArgs &func_args)
Default dispatcher function.