1
0

util.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2013 Hugh Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /* Useful C++ classes/bindings for util data and pointers */
  17. #pragma once
  18. #include <string.h>
  19. #include <stdarg.h>
  20. #include "bmem.h"
  21. #include "config-file.h"
  22. #include "text-lookup.h"
  23. /* RAII wrappers */
  24. template<typename T> class BPtr {
  25. T *ptr;
  26. BPtr(BPtr const&) = delete;
  27. BPtr &operator=(BPtr const&) = delete;
  28. public:
  29. inline BPtr(T *p=nullptr) : ptr(p) {}
  30. inline BPtr(BPtr &&other) : ptr(other.ptr) {other.ptr = nullptr;}
  31. inline ~BPtr() {bfree(ptr);}
  32. inline T *operator=(T *p) {bfree(ptr); ptr = p; return p;}
  33. inline operator T*() {return ptr;}
  34. inline T **operator&() {bfree(ptr); ptr = nullptr; return &ptr;}
  35. inline bool operator!() {return ptr == NULL;}
  36. inline bool operator==(T p) {return ptr == p;}
  37. inline bool operator!=(T p) {return ptr != p;}
  38. };
  39. class ConfigFile {
  40. config_t *config;
  41. ConfigFile(ConfigFile const&) = delete;
  42. ConfigFile &operator=(ConfigFile const&) = delete;
  43. public:
  44. inline ConfigFile() : config(NULL) {}
  45. inline ConfigFile(ConfigFile &&other) : config(other.config)
  46. {
  47. other.config = nullptr;
  48. }
  49. inline ~ConfigFile()
  50. {
  51. config_close(config);
  52. }
  53. inline bool Create(const char *file)
  54. {
  55. Close();
  56. config = config_create(file);
  57. return config != NULL;
  58. }
  59. inline int Open(const char *file, config_open_type openType)
  60. {
  61. Close();
  62. return config_open(&config, file, openType);
  63. }
  64. inline int Save()
  65. {
  66. return config_save(config);
  67. }
  68. inline void Close()
  69. {
  70. config_close(config);
  71. config = NULL;
  72. }
  73. inline operator config_t*() const {return config;}
  74. };
  75. class TextLookup {
  76. lookup_t *lookup;
  77. TextLookup(TextLookup const&) = delete;
  78. TextLookup &operator=(TextLookup const&) = delete;
  79. public:
  80. inline TextLookup(lookup_t *lookup=nullptr) : lookup(lookup) {}
  81. inline TextLookup(TextLookup &&other) : lookup(other.lookup)
  82. {
  83. other.lookup = nullptr;
  84. }
  85. inline ~TextLookup() {text_lookup_destroy(lookup);}
  86. inline TextLookup& operator=(lookup_t *val)
  87. {
  88. text_lookup_destroy(lookup);
  89. lookup = val;
  90. return *this;
  91. }
  92. inline operator lookup_t*() const {return lookup;}
  93. inline const char *GetString(const char *lookupVal) const
  94. {
  95. const char *out;
  96. if (!text_lookup_getstr(lookup, lookupVal, &out))
  97. return lookupVal;
  98. return out;
  99. }
  100. };