util.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. /* Useful C++ classes/bindings for util data and pointers */
  15. #pragma once
  16. #include <string.h>
  17. #include <stdarg.h>
  18. #include "config-file.h"
  19. #include "text-lookup.h"
  20. /* RAII wrappers */
  21. template<typename T> class BPtr {
  22. T *ptr;
  23. BPtr(BPtr const&) = delete;
  24. BPtr &operator=(BPtr const&) = delete;
  25. public:
  26. inline BPtr(T *p=nullptr) : ptr(p) {}
  27. inline BPtr(BPtr &&other) : ptr(other.ptr) {other.ptr = nullptr;}
  28. inline ~BPtr() {bfree(ptr);}
  29. inline T *operator=(T *p) {bfree(ptr); ptr = p; return p;}
  30. inline operator T*() {return ptr;}
  31. inline T **operator&() {bfree(ptr); ptr = nullptr; return &ptr;}
  32. inline bool operator!() {return ptr == NULL;}
  33. inline bool operator==(T p) {return ptr == p;}
  34. inline bool operator!=(T p) {return ptr != p;}
  35. };
  36. class ConfigFile {
  37. config_t config;
  38. ConfigFile(ConfigFile const&) = delete;
  39. ConfigFile &operator=(ConfigFile const&) = delete;
  40. public:
  41. inline ConfigFile() : config(NULL) {}
  42. inline ConfigFile(ConfigFile &&other) : config(other.config)
  43. {
  44. other.config = nullptr;
  45. }
  46. inline ~ConfigFile()
  47. {
  48. config_close(config);
  49. }
  50. inline bool Create(const char *file)
  51. {
  52. Close();
  53. config = config_create(file);
  54. return config != NULL;
  55. }
  56. inline int Open(const char *file, config_open_type openType)
  57. {
  58. Close();
  59. return config_open(&config, file, openType);
  60. }
  61. inline int Save()
  62. {
  63. return config_save(config);
  64. }
  65. inline void Close()
  66. {
  67. config_close(config);
  68. config = NULL;
  69. }
  70. inline operator config_t() const {return config;}
  71. };
  72. class TextLookup {
  73. lookup_t lookup;
  74. TextLookup(TextLookup const&) = delete;
  75. TextLookup &operator=(TextLookup const&) = delete;
  76. public:
  77. inline TextLookup(lookup_t lookup=nullptr) : lookup(lookup) {}
  78. inline TextLookup(TextLookup &&other) : lookup(other.lookup)
  79. {
  80. other.lookup = nullptr;
  81. }
  82. inline ~TextLookup() {text_lookup_destroy(lookup);}
  83. inline TextLookup& operator=(lookup_t val)
  84. {
  85. text_lookup_destroy(lookup);
  86. lookup = val;
  87. return *this;
  88. }
  89. inline operator lookup_t() const {return lookup;}
  90. inline const char *GetString(const char *lookupVal) const
  91. {
  92. const char *out;
  93. if (!text_lookup_getstr(lookup, lookupVal, &out))
  94. return lookupVal;
  95. return out;
  96. }
  97. };