| 1 | /* |
| 2 | * Copyright (C) 2013 Teluu Inc. (http://www.teluu.com) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | #ifndef __PJSUA2_PERSISTENT_HPP__ |
| 19 | #define __PJSUA2_PERSISTENT_HPP__ |
| 20 | |
| 21 | /** |
| 22 | * @file pjsua2/persistent.hpp |
| 23 | * @brief PJSUA2 Persistent Services |
| 24 | */ |
| 25 | #include <pjsua2/types.hpp> |
| 26 | |
| 27 | #include <string> |
| 28 | #include <vector> |
| 29 | |
| 30 | /** PJSUA2 API is inside pj namespace */ |
| 31 | namespace pj |
| 32 | { |
| 33 | |
| 34 | /** |
| 35 | * @defgroup PJSUA2_PERSISTENT Persistent API |
| 36 | * @ingroup PJSUA2_Ref |
| 37 | * @{ |
| 38 | * The persistent API provides functionality to read/write data from/to |
| 39 | * a document (string or file). The data can be simple data types such |
| 40 | * as boolean, number, string, and string arrays, or a user defined object. |
| 41 | * Currently the implementation supports reading and writing from/to JSON |
| 42 | * document, but the framework allows application to extend the API to |
| 43 | * support other document formats. |
| 44 | */ |
| 45 | |
| 46 | using std::string; |
| 47 | using std::vector; |
| 48 | |
| 49 | /* Forward declaration for ContainerNode */ |
| 50 | class ContainerNode; |
| 51 | |
| 52 | /** |
| 53 | * This is the abstract base class of objects that can be serialized to/from |
| 54 | * persistent document. |
| 55 | */ |
| 56 | class PersistentObject |
| 57 | { |
| 58 | public: |
| 59 | /** |
| 60 | * Virtual destructor |
| 61 | */ |
| 62 | virtual ~PersistentObject() |
| 63 | {} |
| 64 | |
| 65 | /** |
| 66 | * Read this object from a container node. |
| 67 | * |
| 68 | * @param node Container to read values from. |
| 69 | */ |
| 70 | virtual void readObject(const ContainerNode &node) PJSUA2_THROW(Error) = 0; |
| 71 | |
| 72 | /** |
| 73 | * Write this object to a container node. |
| 74 | * |
| 75 | * @param node Container to write values to. |
| 76 | */ |
| 77 | virtual void writeObject(ContainerNode &node) const PJSUA2_THROW(Error) = 0; |
| 78 | }; |
| 79 | |
| 80 | |
| 81 | /** |
| 82 | * This a the abstract base class for a persistent document. A document |
| 83 | * is created either by loading from a string or a file, or by constructing |
| 84 | * it manually when writing data to it. The document then can be saved |
| 85 | * to either string or to a file. A document contains one root ContainerNode |
| 86 | * where all data are stored under. |
| 87 | * |
| 88 | * Document is read and written serially, hence the order of reading must be |
| 89 | * the same as the order of writing. The PersistentDocument class provides |
| 90 | * API to read and write to the root node, but for more flexible operations |
| 91 | * application can use the ContainerNode methods instead. Indeed the read |
| 92 | * and write API in PersistentDocument is just a shorthand which calls the |
| 93 | * relevant methods in the ContainerNode. As a tip, normally application only |
| 94 | * uses the readObject() and writeObject() methods declared here to read/write |
| 95 | * top level objects, and use the macros that are explained in ContainerNode |
| 96 | * documentation to read/write more detailed data. |
| 97 | */ |
| 98 | class PersistentDocument |
| 99 | { |
| 100 | public: |
| 101 | /** |
| 102 | * Virtual destructor |
| 103 | */ |
| 104 | virtual ~PersistentDocument() |
| 105 | {} |
| 106 | |
| 107 | /** |
| 108 | * Load this document from a file. |
| 109 | * |
| 110 | * @param filename The file name. |
| 111 | */ |
| 112 | virtual void loadFile(const string &filename) |
| 113 | PJSUA2_THROW(Error) = 0; |
| 114 | |
| 115 | /** |
| 116 | * Load this document from string. |
| 117 | * |
| 118 | * @param input The string. |
| 119 | */ |
| 120 | virtual void loadString(const string &input) |
| 121 | PJSUA2_THROW(Error) = 0; |
| 122 | |
| 123 | /** |
| 124 | * Write this document to a file. |
| 125 | * |
| 126 | * @param filename The file name. |
| 127 | */ |
| 128 | virtual void saveFile(const string &filename) |
| 129 | PJSUA2_THROW(Error) = 0; |
| 130 | |
| 131 | /** |
| 132 | * Write this document to string. |
| 133 | * |
| 134 | * @return The string document. |
| 135 | */ |
| 136 | virtual string saveString() PJSUA2_THROW(Error) = 0; |
| 137 | |
| 138 | /** |
| 139 | * Get the root container node for this document |
| 140 | * |
| 141 | * @return The root node. |
| 142 | */ |
| 143 | virtual ContainerNode & getRootContainer() const = 0; |
| 144 | |
| 145 | |
| 146 | /* |
| 147 | * Shorthand functions for reading and writing from/to the root container |
| 148 | */ |
| 149 | |
| 150 | |
| 151 | /** |
| 152 | * Determine if there is unread element. If yes, then app can use one of |
| 153 | * the readXxx() functions to read it. |
| 154 | * |
| 155 | * @return True if there is. |
| 156 | */ |
| 157 | bool hasUnread() const; |
| 158 | |
| 159 | /** |
| 160 | * Get the name of the next unread element. It will throw Error if there |
| 161 | * is no more element to read. |
| 162 | * |
| 163 | * @return The name of the next element . |
| 164 | */ |
| 165 | string unreadName() const PJSUA2_THROW(Error); |
| 166 | |
| 167 | /** |
| 168 | * Read an integer value from the document and return the value. |
| 169 | * This will throw Error if the current element is not a number. |
| 170 | * The read position will be advanced to the next element. |
| 171 | * |
| 172 | * @param name If specified, then the function will check if the |
| 173 | * name of the next element matches the specified |
| 174 | * name and throw Error if it doesn't match. |
| 175 | * |
| 176 | * @return The value. |
| 177 | */ |
| 178 | int readInt(const string &name="") const |
| 179 | PJSUA2_THROW(Error); |
| 180 | |
| 181 | /** |
| 182 | * Read a float value from the document and return the value. |
| 183 | * This will throw Error if the current element is not a number. |
| 184 | * The read position will be advanced to the next element. |
| 185 | * |
| 186 | * @param name If specified, then the function will check if the |
| 187 | * name of the next element matches the specified |
| 188 | * name and throw Error if it doesn't match. |
| 189 | * |
| 190 | * @return The value. |
| 191 | */ |
| 192 | float readNumber(const string &name="") const |
| 193 | PJSUA2_THROW(Error); |
| 194 | |
| 195 | /** |
| 196 | * Read a boolean value from the container and return the value. |
| 197 | * This will throw Error if the current element is not a boolean. |
| 198 | * The read position will be advanced to the next element. |
| 199 | * |
| 200 | * @param name If specified, then the function will check if the |
| 201 | * name of the next element matches the specified |
| 202 | * name and throw Error if it doesn't match. |
| 203 | * |
| 204 | * @return The value. |
| 205 | */ |
| 206 | bool readBool(const string &name="") const |
| 207 | PJSUA2_THROW(Error); |
| 208 | |
| 209 | /** |
| 210 | * Read a string value from the container and return the value. |
| 211 | * This will throw Error if the current element is not a string. |
| 212 | * The read position will be advanced to the next element. |
| 213 | * |
| 214 | * @param name If specified, then the function will check if the |
| 215 | * name of the next element matches the specified |
| 216 | * name and throw Error if it doesn't match. |
| 217 | * |
| 218 | * @return The value. |
| 219 | */ |
| 220 | string readString(const string &name="") const |
| 221 | PJSUA2_THROW(Error); |
| 222 | |
| 223 | /** |
| 224 | * Read a string array from the container. This will throw Error |
| 225 | * if the current element is not a string array. The read position |
| 226 | * will be advanced to the next element. |
| 227 | * |
| 228 | * @param name If specified, then the function will check if the |
| 229 | * name of the next element matches the specified |
| 230 | * name and throw Error if it doesn't match. |
| 231 | * |
| 232 | * @return The value. |
| 233 | */ |
| 234 | StringVector readStringVector(const string &name="") const |
| 235 | PJSUA2_THROW(Error); |
| 236 | |
| 237 | /** |
| 238 | * Read the specified object from the container. This is equal to |
| 239 | * calling PersistentObject.readObject(ContainerNode); |
| 240 | * |
| 241 | * @param obj The object to read. |
| 242 | */ |
| 243 | void readObject(PersistentObject &obj) const |
| 244 | PJSUA2_THROW(Error); |
| 245 | |
| 246 | /** |
| 247 | * Read a container from the container. This will throw Error if the |
| 248 | * current element is not an object. The read position will be advanced |
| 249 | * to the next element. |
| 250 | * |
| 251 | * @param name If specified, then the function will check if the |
| 252 | * name of the next element matches the specified |
| 253 | * name and throw Error if it doesn't match. |
| 254 | * |
| 255 | * @return Container object. |
| 256 | */ |
| 257 | ContainerNode readContainer(const string &name="") const |
| 258 | PJSUA2_THROW(Error); |
| 259 | |
| 260 | /** |
| 261 | * Read array container from the container. This will throw Error if the |
| 262 | * current element is not an array. The read position will be advanced |
| 263 | * to the next element. |
| 264 | * |
| 265 | * @param name If specified, then the function will check if the |
| 266 | * name of the next element matches the specified |
| 267 | * name and throw Error if it doesn't match. |
| 268 | * |
| 269 | * @return Container object. |
| 270 | */ |
| 271 | ContainerNode readArray(const string &name="") const |
| 272 | PJSUA2_THROW(Error); |
| 273 | |
| 274 | /** |
| 275 | * Write a number value to the container. |
| 276 | * |
| 277 | * @param name The name for the value in the container. |
| 278 | * @param num The value to be written. |
| 279 | */ |
| 280 | void writeNumber(const string &name, |
| 281 | float num) PJSUA2_THROW(Error); |
| 282 | |
| 283 | /** |
| 284 | * Write a number value to the container. |
| 285 | * |
| 286 | * @param name The name for the value in the container. |
| 287 | * @param num The value to be written. |
| 288 | */ |
| 289 | void writeInt(const string &name, |
| 290 | int num) PJSUA2_THROW(Error); |
| 291 | |
| 292 | /** |
| 293 | * Write a boolean value to the container. |
| 294 | * |
| 295 | * @param name The name for the value in the container. |
| 296 | * @param value The value to be written. |
| 297 | */ |
| 298 | void writeBool(const string &name, |
| 299 | bool value) PJSUA2_THROW(Error); |
| 300 | |
| 301 | /** |
| 302 | * Write a string value to the container. |
| 303 | * |
| 304 | * @param name The name for the value in the container. |
| 305 | * @param value The value to be written. |
| 306 | */ |
| 307 | void writeString(const string &name, |
| 308 | const string &value) PJSUA2_THROW(Error); |
| 309 | |
| 310 | /** |
| 311 | * Write string vector to the container. |
| 312 | * |
| 313 | * @param name The name for the value in the container. |
| 314 | * @param arr The vector to be written. |
| 315 | */ |
| 316 | void writeStringVector(const string &name, |
| 317 | const StringVector &arr) |
| 318 | PJSUA2_THROW(Error); |
| 319 | |
| 320 | /** |
| 321 | * Write an object to the container. This is equal to calling |
| 322 | * PersistentObject.writeObject(ContainerNode); |
| 323 | * |
| 324 | * @param obj The object to be written |
| 325 | */ |
| 326 | void writeObject(const PersistentObject &obj) |
| 327 | PJSUA2_THROW(Error); |
| 328 | |
| 329 | /** |
| 330 | * Create and write an empty Object node that can be used as parent |
| 331 | * for subsequent write operations. |
| 332 | * |
| 333 | * @param name The name for the new container in the container. |
| 334 | * |
| 335 | * @return A sub-container. |
| 336 | */ |
| 337 | ContainerNode writeNewContainer(const string &name) |
| 338 | PJSUA2_THROW(Error); |
| 339 | |
| 340 | /** |
| 341 | * Create and write an empty array node that can be used as parent |
| 342 | * for subsequent write operations. |
| 343 | * |
| 344 | * @param name The name for the array. |
| 345 | * |
| 346 | * @return A sub-container. |
| 347 | */ |
| 348 | ContainerNode writeNewArray(const string &name) |
| 349 | PJSUA2_THROW(Error); |
| 350 | }; |
| 351 | |
| 352 | |
| 353 | /** |
| 354 | * Forward declaration of container_node_op. |
| 355 | */ |
| 356 | struct container_node_op; |
| 357 | |
| 358 | |
| 359 | /** |
| 360 | * Internal data for ContainerNode. See ContainerNode implementation notes |
| 361 | * for more info. |
| 362 | */ |
| 363 | struct container_node_internal_data |
| 364 | { |
| 365 | void *doc; /**< The document. */ |
| 366 | void *data1; /**< Internal data 1 */ |
| 367 | void *data2; /**< Internal data 2 */ |
| 368 | }; |
| 369 | |
| 370 | /** |
| 371 | * A container node is a placeholder for storing other data elements, which |
| 372 | * could be boolean, number, string, array of strings, or another container. |
| 373 | * Each data in the container is basically a name/value pair, with a type |
| 374 | * internally associated with it so that written data can be read in the |
| 375 | * correct type. Data is read and written serially, hence the order of |
| 376 | * reading must be the same as the order of writing. |
| 377 | * |
| 378 | * Application can read data from it by using the various read methods, and |
| 379 | * write data to it using the various write methods. Alternatively, it |
| 380 | * may be more convenient to use the provided macros below to read and write |
| 381 | * the data, because these macros set the name automatically: |
| 382 | * - NODE_READ_BOOL(node,item) |
| 383 | * - NODE_READ_UNSIGNED(node,item) |
| 384 | * - NODE_READ_INT(node,item) |
| 385 | * - NODE_READ_FLOAT(node,item) |
| 386 | * - NODE_READ_NUM_T(node,type,item) |
| 387 | * - NODE_READ_STRING(node,item) |
| 388 | * - NODE_READ_STRINGV(node,item) |
| 389 | * - NODE_READ_OBJ(node,item) |
| 390 | * - NODE_WRITE_BOOL(node,item) |
| 391 | * - NODE_WRITE_UNSIGNED(node,item) |
| 392 | * - NODE_WRITE_INT(node,item) |
| 393 | * - NODE_WRITE_FLOAT(node,item) |
| 394 | * - NODE_WRITE_NUM_T(node,type,item) |
| 395 | * - NODE_WRITE_STRING(node,item) |
| 396 | * - NODE_WRITE_STRINGV(node,item) |
| 397 | * - NODE_WRITE_OBJ(node,item) |
| 398 | * |
| 399 | * Implementation notes: |
| 400 | * |
| 401 | * The ContainerNode class is subclass-able, but not in the usual C++ way. |
| 402 | * With the usual C++ inheritance, some methods will be made pure virtual |
| 403 | * and must be implemented by the actual class. However, doing so will |
| 404 | * require dynamic instantiation of the ContainerNode class, which means |
| 405 | * we will need to pass around the class as pointer, for example as the |
| 406 | * return value of readContainer() and writeNewContainer() methods. Then |
| 407 | * we will need to establish who needs or how to delete these objects, or |
| 408 | * use shared pointer mechanism, each of which is considered too inconvenient |
| 409 | * or complicated for the purpose. |
| 410 | * |
| 411 | * So hence we use C style "inheritance", where the methods are declared in |
| 412 | * container_node_op and the data in container_node_internal_data structures. |
| 413 | * An implementation of ContainerNode class will need to set up these members |
| 414 | * with values that makes sense to itself. The methods in container_node_op |
| 415 | * contains the pointer to the actual implementation of the operation, which |
| 416 | * would be specific according to the format of the document. The methods in |
| 417 | * this ContainerNode class are just thin wrappers which call the |
| 418 | * implementation in the container_node_op structure. |
| 419 | * |
| 420 | */ |
| 421 | class ContainerNode |
| 422 | { |
| 423 | public: |
| 424 | /** |
| 425 | * Determine if there is unread element. If yes, then app can use one of |
| 426 | * the readXxx() functions to read it. |
| 427 | */ |
| 428 | bool hasUnread() const; |
| 429 | |
| 430 | /** |
| 431 | * Get the name of the next unread element. |
| 432 | */ |
| 433 | string unreadName() const PJSUA2_THROW(Error); |
| 434 | |
| 435 | /** |
| 436 | * Read an integer value from the document and return the value. |
| 437 | * This will throw Error if the current element is not a number. |
| 438 | * The read position will be advanced to the next element. |
| 439 | * |
| 440 | * @param name If specified, then the function will check if the |
| 441 | * name of the next element matches the specified |
| 442 | * name and throw Error if it doesn't match. |
| 443 | * |
| 444 | * @return The value. |
| 445 | */ |
| 446 | int readInt(const string &name="") const |
| 447 | PJSUA2_THROW(Error); |
| 448 | |
| 449 | /** |
| 450 | * Read a number value from the document and return the value. |
| 451 | * This will throw Error if the current element is not a number. |
| 452 | * The read position will be advanced to the next element. |
| 453 | * |
| 454 | * @param name If specified, then the function will check if the |
| 455 | * name of the next element matches the specified |
| 456 | * name and throw Error if it doesn't match. |
| 457 | * |
| 458 | * @return The value. |
| 459 | */ |
| 460 | float readNumber(const string &name="") const |
| 461 | PJSUA2_THROW(Error); |
| 462 | |
| 463 | /** |
| 464 | * Read a boolean value from the container and return the value. |
| 465 | * This will throw Error if the current element is not a boolean. |
| 466 | * The read position will be advanced to the next element. |
| 467 | * |
| 468 | * @param name If specified, then the function will check if the |
| 469 | * name of the next element matches the specified |
| 470 | * name and throw Error if it doesn't match. |
| 471 | * |
| 472 | * @return The value. |
| 473 | */ |
| 474 | bool readBool(const string &name="") const |
| 475 | PJSUA2_THROW(Error); |
| 476 | |
| 477 | /** |
| 478 | * Read a string value from the container and return the value. |
| 479 | * This will throw Error if the current element is not a string. |
| 480 | * The read position will be advanced to the next element. |
| 481 | * |
| 482 | * @param name If specified, then the function will check if the |
| 483 | * name of the next element matches the specified |
| 484 | * name and throw Error if it doesn't match. |
| 485 | * |
| 486 | * @return The value. |
| 487 | */ |
| 488 | string readString(const string &name="") const |
| 489 | PJSUA2_THROW(Error); |
| 490 | |
| 491 | /** |
| 492 | * Read a string array from the container. This will throw Error |
| 493 | * if the current element is not a string array. The read position |
| 494 | * will be advanced to the next element. |
| 495 | * |
| 496 | * @param name If specified, then the function will check if the |
| 497 | * name of the next element matches the specified |
| 498 | * name and throw Error if it doesn't match. |
| 499 | * |
| 500 | * @return The value. |
| 501 | */ |
| 502 | StringVector readStringVector(const string &name="") const |
| 503 | PJSUA2_THROW(Error); |
| 504 | |
| 505 | /** |
| 506 | * Read the specified object from the container. This is equal to |
| 507 | * calling PersistentObject.readObject(ContainerNode); |
| 508 | * |
| 509 | * @param obj The object to read. |
| 510 | */ |
| 511 | void readObject(PersistentObject &obj) const |
| 512 | PJSUA2_THROW(Error); |
| 513 | |
| 514 | /** |
| 515 | * Read a container from the container. This will throw Error if the |
| 516 | * current element is not a container. The read position will be advanced |
| 517 | * to the next element. |
| 518 | * |
| 519 | * @param name If specified, then the function will check if the |
| 520 | * name of the next element matches the specified |
| 521 | * name and throw Error if it doesn't match. |
| 522 | * |
| 523 | * @return Container object. |
| 524 | */ |
| 525 | ContainerNode readContainer(const string &name="") const |
| 526 | PJSUA2_THROW(Error); |
| 527 | |
| 528 | /** |
| 529 | * Read array container from the container. This will throw Error if the |
| 530 | * current element is not an array. The read position will be advanced |
| 531 | * to the next element. |
| 532 | * |
| 533 | * @param name If specified, then the function will check if the |
| 534 | * name of the next element matches the specified |
| 535 | * name and throw Error if it doesn't match. |
| 536 | * |
| 537 | * @return Container object. |
| 538 | */ |
| 539 | ContainerNode readArray(const string &name="") const |
| 540 | PJSUA2_THROW(Error); |
| 541 | |
| 542 | /** |
| 543 | * Write a number value to the container. |
| 544 | * |
| 545 | * @param name The name for the value in the container. |
| 546 | * @param num The value to be written. |
| 547 | */ |
| 548 | void writeNumber(const string &name, |
| 549 | float num) PJSUA2_THROW(Error); |
| 550 | |
| 551 | /** |
| 552 | * Write a number value to the container. |
| 553 | * |
| 554 | * @param name The name for the value in the container. |
| 555 | * @param num The value to be written. |
| 556 | */ |
| 557 | void writeInt(const string &name, |
| 558 | int num) PJSUA2_THROW(Error); |
| 559 | |
| 560 | /** |
| 561 | * Write a boolean value to the container. |
| 562 | * |
| 563 | * @param name The name for the value in the container. |
| 564 | * @param value The value to be written. |
| 565 | */ |
| 566 | void writeBool(const string &name, |
| 567 | bool value) PJSUA2_THROW(Error); |
| 568 | |
| 569 | /** |
| 570 | * Write a string value to the container. |
| 571 | * |
| 572 | * @param name The name for the value in the container. |
| 573 | * @param value The value to be written. |
| 574 | */ |
| 575 | void writeString(const string &name, |
| 576 | const string &value) PJSUA2_THROW(Error); |
| 577 | |
| 578 | /** |
| 579 | * Write string vector to the container. |
| 580 | * |
| 581 | * @param name The name for the value in the container. |
| 582 | * @param arr The vector to be written. |
| 583 | */ |
| 584 | void writeStringVector(const string &name, |
| 585 | const StringVector &arr) |
| 586 | PJSUA2_THROW(Error); |
| 587 | |
| 588 | /** |
| 589 | * Write an object to the container. This is equal to calling |
| 590 | * PersistentObject.writeObject(ContainerNode); |
| 591 | * |
| 592 | * @param obj The object to be written |
| 593 | */ |
| 594 | void writeObject(const PersistentObject &obj) |
| 595 | PJSUA2_THROW(Error); |
| 596 | |
| 597 | /** |
| 598 | * Create and write an empty Object node that can be used as parent |
| 599 | * for subsequent write operations. |
| 600 | * |
| 601 | * @param name The name for the new container in the container. |
| 602 | * |
| 603 | * @return A sub-container. |
| 604 | */ |
| 605 | ContainerNode writeNewContainer(const string &name) |
| 606 | PJSUA2_THROW(Error); |
| 607 | |
| 608 | /** |
| 609 | * Create and write an empty array node that can be used as parent |
| 610 | * for subsequent write operations. |
| 611 | * |
| 612 | * @param name The name for the array. |
| 613 | * |
| 614 | * @return A sub-container. |
| 615 | */ |
| 616 | ContainerNode writeNewArray(const string &name) |
| 617 | PJSUA2_THROW(Error); |
| 618 | |
| 619 | public: |
| 620 | /* internal data */ |
| 621 | container_node_op *op; /**< Method table. */ |
| 622 | container_node_internal_data data; /**< Internal data */ |
| 623 | |
| 624 | ContainerNode() |
| 625 | : op(NULL) |
| 626 | { |
| 627 | pj_bzero(&data, sizeof(data)); |
| 628 | } |
| 629 | }; |
| 630 | |
| 631 | |
| 632 | /** |
| 633 | * Pointer to actual ContainerNode implementation. See ContainerNode |
| 634 | * implementation notes for more info. |
| 635 | */ |
| 636 | //! @cond Doxygen_Suppress |
| 637 | struct container_node_op |
| 638 | { |
| 639 | bool (*hasUnread)(const ContainerNode*); |
| 640 | string (*unreadName)(const ContainerNode*) |
| 641 | PJSUA2_THROW(Error); |
| 642 | float (*readNumber)(const ContainerNode*, |
| 643 | const string&) |
| 644 | PJSUA2_THROW(Error); |
| 645 | bool (*readBool)(const ContainerNode*, |
| 646 | const string&) |
| 647 | PJSUA2_THROW(Error); |
| 648 | string (*readString)(const ContainerNode*, |
| 649 | const string&) |
| 650 | PJSUA2_THROW(Error); |
| 651 | StringVector (*readStringVector)(const ContainerNode*, |
| 652 | const string&) |
| 653 | PJSUA2_THROW(Error); |
| 654 | ContainerNode (*readContainer)(const ContainerNode*, |
| 655 | const string &) |
| 656 | PJSUA2_THROW(Error); |
| 657 | ContainerNode (*readArray)(const ContainerNode*, |
| 658 | const string &) |
| 659 | PJSUA2_THROW(Error); |
| 660 | void (*writeNumber)(ContainerNode*, |
| 661 | const string &name, |
| 662 | float num) |
| 663 | PJSUA2_THROW(Error); |
| 664 | void (*writeBool)(ContainerNode*, |
| 665 | const string &name, |
| 666 | bool value) |
| 667 | PJSUA2_THROW(Error); |
| 668 | void (*writeString)(ContainerNode*, |
| 669 | const string &name, |
| 670 | const string &value) |
| 671 | PJSUA2_THROW(Error); |
| 672 | void (*writeStringVector)(ContainerNode*, |
| 673 | const string &name, |
| 674 | const StringVector &value) |
| 675 | PJSUA2_THROW(Error); |
| 676 | ContainerNode (*writeNewContainer)(ContainerNode*, |
| 677 | const string &name) |
| 678 | PJSUA2_THROW(Error); |
| 679 | ContainerNode (*writeNewArray)(ContainerNode*, |
| 680 | const string &name) |
| 681 | PJSUA2_THROW(Error); |
| 682 | }; |
| 683 | |
| 684 | /* |
| 685 | * Convenient macros. |
| 686 | */ |
| 687 | #define NODE_READ_BOOL(node,item) item = node.readBool(#item) |
| 688 | #define NODE_READ_UNSIGNED(node,item) item = (unsigned)node.readNumber(#item) |
| 689 | #define NODE_READ_INT(node,item) item = (int) node.readNumber(#item) |
| 690 | #define NODE_READ_FLOAT(node,item) item = node.readNumber(#item) |
| 691 | #define NODE_READ_NUM_T(node,T,item) item = (T)(int)node.readNumber(#item) |
| 692 | #define NODE_READ_STRING(node,item) item = node.readString(#item) |
| 693 | #define NODE_READ_STRINGV(node,item) item = node.readStringVector(#item) |
| 694 | #define NODE_READ_OBJ(node,item) node.readObject(item) |
| 695 | |
| 696 | #define NODE_WRITE_BOOL(node,item) node.writeBool(#item, item) |
| 697 | #define NODE_WRITE_UNSIGNED(node,item) node.writeNumber(#item, (float)item) |
| 698 | #define NODE_WRITE_INT(node,item) node.writeNumber(#item, (float)item) |
| 699 | #define NODE_WRITE_NUM_T(node,T,item) node.writeNumber(#item, (float)item) |
| 700 | #define NODE_WRITE_FLOAT(node,item) node.writeNumber(#item, item) |
| 701 | #define NODE_WRITE_STRING(node,item) node.writeString(#item, item) |
| 702 | #define NODE_WRITE_STRINGV(node,item) node.writeStringVector(#item, item) |
| 703 | #define NODE_WRITE_OBJ(node,item) node.writeObject(item) |
| 704 | |
| 705 | /* |
| 706 | * Optional-read variants: leave the item unchanged (i.e. at its |
| 707 | * constructor-initialized default) when the field is absent in the config |
| 708 | * file (e.g. loading a config file written by an older version that did not |
| 709 | * yet have the field). Each macro checks hasUnread() and the name of the |
| 710 | * next element before attempting the read, so no exception is thrown and the |
| 711 | * read cursor is left in the correct position for the next field. |
| 712 | * |
| 713 | * Convention: always append new fields at the END of readObject() and use |
| 714 | * these _OPT variants so that older config files remain loadable. |
| 715 | */ |
| 716 | #define NODE_READ_BOOL_OPT(node,item) \ |
| 717 | do { if ((node).hasUnread() && (node).unreadName()==#item) \ |
| 718 | (item)=(node).readBool(#item); } while(0) |
| 719 | #define NODE_READ_UNSIGNED_OPT(node,item) \ |
| 720 | do { if ((node).hasUnread() && (node).unreadName()==#item) \ |
| 721 | (item)=(unsigned)(node).readNumber(#item); } while(0) |
| 722 | #define NODE_READ_INT_OPT(node,item) \ |
| 723 | do { if ((node).hasUnread() && (node).unreadName()==#item) \ |
| 724 | (item)=(int)(node).readNumber(#item); } while(0) |
| 725 | #define NODE_READ_FLOAT_OPT(node,item) \ |
| 726 | do { if ((node).hasUnread() && (node).unreadName()==#item) \ |
| 727 | (item)=(node).readNumber(#item); } while(0) |
| 728 | #define NODE_READ_NUM_T_OPT(node,T,item) \ |
| 729 | do { if ((node).hasUnread() && (node).unreadName()==#item) \ |
| 730 | (item)=(T)(int)(node).readNumber(#item); } while(0) |
| 731 | #define NODE_READ_STRING_OPT(node,item) \ |
| 732 | do { if ((node).hasUnread() && (node).unreadName()==#item) \ |
| 733 | (item)=(node).readString(#item); } while(0) |
| 734 | #define NODE_READ_STRINGV_OPT(node,item) \ |
| 735 | do { if ((node).hasUnread() && (node).unreadName()==#item) \ |
| 736 | (item)=(node).readStringVector(#item); } while(0) |
| 737 | #define NODE_READ_OBJ_OPT(node,item) \ |
| 738 | do { if ((node).hasUnread() && (node).unreadName()==#item) \ |
| 739 | (node).readObject(item); } while(0) |
| 740 | |
| 741 | //! @endcond |
| 742 | |
| 743 | /** |
| 744 | * @} PJSUA2 |
| 745 | */ |
| 746 | |
| 747 | } // namespace pj |
| 748 | |
| 749 | |
| 750 | |
| 751 | #endif /* __PJSUA2_PERSISTENT_HPP__ */ |