util.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "config-file.h"
  21. #include "text-lookup.h"
  22. /* RAII wrappers */
  23. template<typename T> class BPtr {
  24. T *ptr;
  25. BPtr(BPtr const&) = delete;
  26. BPtr &operator=(BPtr const&) = delete;
  27. public:
  28. inline BPtr(T *p=nullptr) : ptr(p) {}
  29. inline BPtr(BPtr &&other) : ptr(other.ptr) {other.ptr = nullptr;}
  30. inline ~BPtr() {bfree(ptr);}
  31. inline T *operator=(T *p) {bfree(ptr); ptr = p; return p;}
  32. inline operator T*() {return ptr;}
  33. inline T **operator&() {bfree(ptr); ptr = nullptr; return &ptr;}
  34. inline bool operator!() {return ptr == NULL;}
  35. inline bool operator==(T p) {return ptr == p;}
  36. inline bool operator!=(T p) {return ptr != p;}
  37. };
  38. class ConfigFile {
  39. config_t config;
  40. ConfigFile(ConfigFile const&) = delete;
  41. ConfigFile &operator=(ConfigFile const&) = delete;
  42. public:
  43. inline ConfigFile() : config(NULL) {}
  44. inline ConfigFile(ConfigFile &&other) : config(other.config)
  45. {
  46. other.config = nullptr;
  47. }
  48. inline ~ConfigFile()
  49. {
  50. config_close(config);
  51. }
  52. inline bool Create(const char *file)
  53. {
  54. Close();
  55. config = config_create(file);
  56. return config != NULL;
  57. }
  58. inline int Open(const char *file, config_open_type openType)
  59. {
  60. Close();
  61. return config_open(&config, file, openType);
  62. }
  63. inline int Save()
  64. {
  65. return config_save(config);
  66. }
  67. inline void Close()
  68. {
  69. config_close(config);
  70. config = NULL;
  71. }
  72. inline operator config_t() const {return config;}
  73. };
  74. class TextLookup {
  75. lookup_t lookup;
  76. TextLookup(TextLookup const&) = delete;
  77. TextLookup &operator=(TextLookup const&) = delete;
  78. public:
  79. inline TextLookup(lookup_t lookup=nullptr) : lookup(lookup) {}
  80. inline TextLookup(TextLookup &&other) : lookup(other.lookup)
  81. {
  82. other.lookup = nullptr;
  83. }
  84. inline ~TextLookup() {text_lookup_destroy(lookup);}
  85. inline TextLookup& operator=(lookup_t val)
  86. {
  87. text_lookup_destroy(lookup);
  88. lookup = val;
  89. return *this;
  90. }
  91. inline operator lookup_t() const {return lookup;}
  92. inline const char *GetString(const char *lookupVal) const
  93. {
  94. const char *out;
  95. if (!text_lookup_getstr(lookup, lookupVal, &out))
  96. return lookupVal;
  97. return out;
  98. }
  99. };