Lab 1 Program C
Fork of mbed by
rpc.h@4:5d1359a283bc, 2008-11-27 (annotated)
- Committer:
- simon.ford@mbed.co.uk
- Date:
- Thu Nov 27 16:23:24 2008 +0000
- Revision:
- 4:5d1359a283bc
- Child:
- 5:62573be585e9
New version of framework: vectors, environment, platform, base and file system
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon.ford@mbed.co.uk | 4:5d1359a283bc | 1 | /* mbed Microcontroller Library |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 2 | * Copyright (c) 2008 ARM Limited. All rights reserved. |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 3 | */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 4 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 5 | #ifndef MBED_RPC_H |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 6 | #define MBED_RPC_H |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 7 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 8 | /* Section rpc |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 9 | * Helpers for rpc handling. |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 10 | */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 11 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 12 | #include <stdlib.h> |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 13 | #include <stdio.h> |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 14 | #include <string.h> |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 15 | #include "Base.h" |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 16 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 17 | namespace mbed { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 18 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 19 | /* Function parse_arg |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 20 | * Parses and returns a value from a string. |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 21 | * |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 22 | * Variable |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 23 | * arg - The string to pase |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 24 | * next - If not NULL a pointer to after the last |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 25 | * character parsed is written here |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 26 | */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 27 | template<typename T> T parse_arg(const char *arg, const char **next); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 28 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 29 | /* signed integer types */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 30 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 31 | template<> inline char parse_arg<char>(const char *arg, const char **next) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 32 | if(next != NULL) *next = arg+1; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 33 | return *arg; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 34 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 35 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 36 | template<> inline short int parse_arg<short int>(const char *arg, const char **next) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 37 | return strtol(arg, const_cast<char**>(next), 10); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 38 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 39 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 40 | template<> inline long int parse_arg<long int>(const char *arg, const char **next) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 41 | return strtol(arg, const_cast<char**>(next), 10); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 42 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 43 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 44 | template<> inline int parse_arg<int>(const char *arg, const char **next) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 45 | return strtol(arg, const_cast<char**>(next), 10); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 46 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 47 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 48 | template<> inline long long parse_arg<long long>(const char *arg, const char **next) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 49 | return strtoll(arg, const_cast<char**>(next), 10); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 50 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 51 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 52 | /* unsigned integer types */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 53 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 54 | template<> inline unsigned char parse_arg<unsigned char>(const char *arg, const char **next) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 55 | if(next != NULL) *next = arg+1; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 56 | return *arg; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 57 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 58 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 59 | template<> inline unsigned short int parse_arg<unsigned short int>(const char *arg, const char **next) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 60 | return strtoul(arg, const_cast<char**>(next), 10); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 61 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 62 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 63 | template<> inline unsigned long int parse_arg<unsigned long int>(const char *arg, const char **next) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 64 | return strtoul(arg, const_cast<char**>(next), 10); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 65 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 66 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 67 | template<> inline unsigned int parse_arg<unsigned int>(const char *arg, const char **next) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 68 | return strtoul(arg, const_cast<char**>(next), 10); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 69 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 70 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 71 | template<> inline unsigned long long parse_arg<unsigned long long>(const char *arg, const char **next) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 72 | return strtoull(arg, const_cast<char**>(next), 10); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 73 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 74 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 75 | /* floating types */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 76 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 77 | template<> inline float parse_arg<float>(const char *arg, const char **next) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 78 | #if !defined(__ARMCC_VERSION) || __ARMCC_VERSION >= 310000 |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 79 | return strtof(arg,const_cast<char**>(next)); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 80 | #else |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 81 | return strtod(arg,const_cast<char**>(next)); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 82 | #endif |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 83 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 84 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 85 | template<> inline double parse_arg<double>(const char *arg, const char **next) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 86 | return strtod(arg,const_cast<char**>(next)); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 87 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 88 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 89 | template<> inline long double parse_arg<long double>(const char *arg, const char **next) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 90 | return strtod(arg,const_cast<char**>(next)); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 91 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 92 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 93 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 94 | /* Function write_result |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 95 | * Writes a value in to a result string in an appropriate manner |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 96 | * |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 97 | * Variable |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 98 | * val - The value to write |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 99 | * result - A pointer to the array to write the value into |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 100 | */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 101 | template<typename T> void write_result(T val, char *result); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 102 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 103 | /* signed integer types */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 104 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 105 | template<> inline void write_result<char>(char val, char *result) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 106 | result[0] = val; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 107 | result[1] = '\0'; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 108 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 109 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 110 | template<> inline void write_result<short int>(short int val, char *result) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 111 | sprintf(result, "%hi", val); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 112 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 113 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 114 | template<> inline void write_result<int>(int val, char *result) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 115 | sprintf(result, "%i", val); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 116 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 117 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 118 | template<> inline void write_result<long int>(long int val, char *result) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 119 | sprintf(result, "%li", val); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 120 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 121 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 122 | template<> inline void write_result<long long int>(long long int val, char *result) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 123 | sprintf(result, "%lli", val); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 124 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 125 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 126 | /* unsigned integer types */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 127 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 128 | template<> inline void write_result<unsigned char>(unsigned char val, char *result) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 129 | result[0] = val; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 130 | result[1] = '\0'; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 131 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 132 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 133 | template<> inline void write_result<unsigned short int>(unsigned short int val, char *result) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 134 | sprintf(result, "%hu", val); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 135 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 136 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 137 | template<> inline void write_result<unsigned int>(unsigned int val, char *result) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 138 | sprintf(result, "%u", val); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 139 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 140 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 141 | template<> inline void write_result<unsigned long int>(unsigned long int val, char *result) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 142 | sprintf(result, "%lu", val); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 143 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 144 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 145 | template<> inline void write_result<unsigned long long int>(unsigned long long int val, char *result) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 146 | sprintf(result, "%llu", val); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 147 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 148 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 149 | /* floating types */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 150 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 151 | template<> inline void write_result<float>(float val, char *result) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 152 | sprintf(result, "%g", val); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 153 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 154 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 155 | template<> inline void write_result<double>(double val, char *result) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 156 | sprintf(result, "%g", val); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 157 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 158 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 159 | template<> inline void write_result<long double>(long double val, char *result) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 160 | sprintf(result, "%Lg", val); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 161 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 162 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 163 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 164 | /* Function generic_caller |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 165 | */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 166 | template<class T, void (T::*member)()> |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 167 | void generic_caller(Base *this_ptr, const char *arguments, char *result) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 168 | (static_cast<T*>(this_ptr)->*member)(); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 169 | if(result != NULL) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 170 | result[0] = '\0'; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 171 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 172 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 173 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 174 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 175 | /* Function generic_caller |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 176 | */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 177 | template<class T, typename A1, void (T::*member)(A1)> |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 178 | void generic_caller(Base *this_ptr, const char *arguments, char *result) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 179 | const char *next = arguments; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 180 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 181 | if(*next == ',' || *next == ' ') next++; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 182 | A1 arg1 = parse_arg<A1>(next,NULL); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 183 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 184 | (static_cast<T*>(this_ptr)->*member)(arg1); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 185 | if(result != NULL) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 186 | result[0] = '\0'; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 187 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 188 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 189 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 190 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 191 | /* Function generic_caller |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 192 | */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 193 | template<class T, typename A1, typename A2, void (T::*member)(A1,A2)> |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 194 | void generic_caller(Base *this_ptr, const char *arguments, char *result) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 195 | const char *next = arguments; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 196 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 197 | if(*next == ',' || *next == ' ') next++; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 198 | A1 arg1 = parse_arg<A1>(next,&next); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 199 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 200 | if(*next == ',' || *next == ' ') next++; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 201 | A2 arg2 = parse_arg<A2>(next,NULL); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 202 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 203 | (static_cast<T*>(this_ptr)->*member)(arg1,arg2); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 204 | if(result != NULL) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 205 | result[0] = '\0'; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 206 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 207 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 208 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 209 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 210 | /* Function generic_caller |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 211 | */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 212 | template<typename R, class T, R (T::*member)()> |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 213 | void generic_caller(Base *this_ptr, const char *arguments, char *result) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 214 | R res = (static_cast<T*>(this_ptr)->*member)(); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 215 | if(result != NULL) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 216 | write_result<R>(res, result); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 217 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 218 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 219 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 220 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 221 | /* Function generic_caller |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 222 | */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 223 | template<typename R, class T, typename A1, R (T::*member)(A1)> |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 224 | void generic_caller(Base *this_ptr, const char *arguments, char *result) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 225 | const char *next = arguments; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 226 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 227 | if(*next == ',' || *next == ' ') next++; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 228 | A1 arg1 = parse_arg<A1>(next,NULL); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 229 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 230 | R res = (static_cast<T*>(this_ptr)->*member)(arg1); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 231 | if(result != NULL) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 232 | write_result<R>(res, result); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 233 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 234 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 235 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 236 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 237 | /* Function generic_caller |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 238 | */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 239 | template<typename R, class T, typename A1, typename A2, R (T::*member)(A1,A2)> |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 240 | void generic_caller(Base *this_ptr, const char *arguments, char *result) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 241 | const char *next = arguments; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 242 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 243 | if(*next == ',' || *next == ' ') next++; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 244 | A1 arg1 = parse_arg<A1>(next,&next); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 245 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 246 | if(*next == ',' || *next == ' ') next++; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 247 | A2 arg2 = parse_arg<A2>(next,NULL); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 248 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 249 | R res = (static_cast<T*>(this_ptr)->*member)(arg1,arg2); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 250 | if(result != NULL) { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 251 | write_result<R>(res, result); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 252 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 253 | } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 254 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 255 | struct rpc_method { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 256 | const char *name; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 257 | void (*caller)(Base*, const char*, char*); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 258 | }; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 259 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 260 | #define RPC_METHOD_END { NULL, NULL } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 261 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 262 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 263 | /* Function rpc |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 264 | * Parse a string describing a call and then do it |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 265 | * |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 266 | * Variables |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 267 | * call - A pointer to a string describing the call, which has |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 268 | * the form /object/method arg ... argn. Arguments are |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 269 | * delimited by space characters, and the string is terminated |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 270 | * by a null character. |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 271 | * result - A pointer to an array to write the result into. |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 272 | */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 273 | bool rpc(const char *buf, char *result = 0); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 274 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 275 | } /* namespace mbed */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 276 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 277 | #endif |