util.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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)
  33. {
  34. bfree(ptr);
  35. ptr = p;
  36. return p;
  37. }
  38. inline operator T *() { return ptr; }
  39. inline T **operator&()
  40. {
  41. bfree(ptr);
  42. ptr = nullptr;
  43. return &ptr;
  44. }
  45. inline bool operator!() { return ptr == NULL; }
  46. inline bool operator==(T p) { return ptr == p; }
  47. inline bool operator!=(T p) { return ptr != p; }
  48. inline T *Get() const { return ptr; }
  49. };
  50. class ConfigFile {
  51. config_t *config;
  52. ConfigFile(ConfigFile const &) = delete;
  53. ConfigFile &operator=(ConfigFile const &) = delete;
  54. public:
  55. inline ConfigFile() : config(NULL) {}
  56. inline ConfigFile(ConfigFile &&other) : config(other.config)
  57. {
  58. other.config = nullptr;
  59. }
  60. inline ~ConfigFile() { config_close(config); }
  61. inline bool Create(const char *file)
  62. {
  63. Close();
  64. config = config_create(file);
  65. return config != NULL;
  66. }
  67. inline void Swap(ConfigFile &other)
  68. {
  69. config_t *newConfig = other.config;
  70. other.config = config;
  71. config = newConfig;
  72. }
  73. inline int Open(const char *file, config_open_type openType)
  74. {
  75. Close();
  76. return config_open(&config, file, openType);
  77. }
  78. inline int Save() { return config_save(config); }
  79. inline int SaveSafe(const char *temp_ext,
  80. const char *backup_ext = nullptr)
  81. {
  82. return config_save_safe(config, temp_ext, backup_ext);
  83. }
  84. inline void Close()
  85. {
  86. config_close(config);
  87. config = NULL;
  88. }
  89. inline operator config_t *() const { return config; }
  90. };
  91. class TextLookup {
  92. lookup_t *lookup;
  93. TextLookup(TextLookup const &) = delete;
  94. TextLookup &operator=(TextLookup const &) = delete;
  95. public:
  96. inline TextLookup(lookup_t *lookup = nullptr) : lookup(lookup) {}
  97. inline TextLookup(TextLookup &&other) : lookup(other.lookup)
  98. {
  99. other.lookup = nullptr;
  100. }
  101. inline ~TextLookup() { text_lookup_destroy(lookup); }
  102. inline TextLookup &operator=(lookup_t *val)
  103. {
  104. text_lookup_destroy(lookup);
  105. lookup = val;
  106. return *this;
  107. }
  108. inline operator lookup_t *() const { return lookup; }
  109. inline const char *GetString(const char *lookupVal) const
  110. {
  111. const char *out;
  112. if (!text_lookup_getstr(lookup, lookupVal, &out))
  113. return lookupVal;
  114. return out;
  115. }
  116. };