obs-wrappers.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #pragma once
  15. #include <string.h>
  16. #include <stdarg.h>
  17. #include <util/config-file.h>
  18. #include <util/text-lookup.h>
  19. #include <obs.h>
  20. /* RAII wrappers */
  21. template<typename T> class BPtr {
  22. T ptr;
  23. public:
  24. inline BPtr() : ptr(NULL) {}
  25. inline BPtr(T p) : ptr(p) {}
  26. inline ~BPtr() {bfree(ptr);}
  27. inline T operator=(T p) {bfree(ptr); ptr = p;}
  28. inline operator T() {return ptr;}
  29. inline T *operator&() {bfree(ptr); ptr = NULL; return &ptr;}
  30. inline bool operator!() {return ptr == NULL;}
  31. inline bool operator==(T p) {return ptr == p;}
  32. inline bool operator!=(T p) {return ptr != p;}
  33. };
  34. class ConfigFile {
  35. config_t config;
  36. public:
  37. inline ConfigFile() : config(NULL) {}
  38. inline ~ConfigFile()
  39. {
  40. config_close(config);
  41. }
  42. inline bool Create(const char *file)
  43. {
  44. Close();
  45. config = config_create(file);
  46. return config != NULL;
  47. }
  48. int Open(const char *file, config_open_type openType)
  49. {
  50. Close();
  51. return config_open(&config, file, openType);
  52. }
  53. int Save()
  54. {
  55. return config_save(config);
  56. }
  57. void Close()
  58. {
  59. config_close(config);
  60. config = NULL;
  61. }
  62. inline operator config_t() {return config;}
  63. };
  64. class TextLookup {
  65. lookup_t lookup;
  66. public:
  67. inline TextLookup() : lookup(NULL) {}
  68. inline TextLookup(lookup_t lookup) : lookup(lookup) {}
  69. inline ~TextLookup() {text_lookup_destroy(lookup);}
  70. inline TextLookup& operator=(lookup_t val)
  71. {
  72. text_lookup_destroy(lookup);
  73. lookup = val;
  74. return *this;
  75. }
  76. inline operator lookup_t() {return lookup;}
  77. inline const char *GetString(const char *lookupVal)
  78. {
  79. const char *out;
  80. if (!text_lookup_getstr(lookup, lookupVal, &out))
  81. return lookupVal;
  82. return out;
  83. }
  84. };
  85. class OBSSource {
  86. obs_source_t source;
  87. public:
  88. inline OBSSource(obs_source_t source) : source(source) {}
  89. inline ~OBSSource() {obs_source_release(source);}
  90. inline OBSSource& operator=(obs_source_t p) {source = p; return *this;}
  91. inline operator obs_source_t() {return source;}
  92. inline bool operator==(obs_source_t p) const {return source == p;}
  93. inline bool operator!=(obs_source_t p) const {return source != p;}
  94. };
  95. class OBSSourceRef {
  96. obs_source_t source;
  97. public:
  98. inline OBSSourceRef(obs_source_t source) : source(source)
  99. {
  100. obs_source_addref(source);
  101. }
  102. inline OBSSourceRef(const OBSSourceRef &ref) : source(ref.source)
  103. {
  104. obs_source_addref(source);
  105. }
  106. inline OBSSourceRef(OBSSourceRef &&ref) : source(ref.source)
  107. {
  108. ref.source = NULL;
  109. }
  110. inline ~OBSSourceRef() {obs_source_release(source);}
  111. inline OBSSourceRef &operator=(obs_source_t sourceIn)
  112. {
  113. obs_source_addref(sourceIn);
  114. obs_source_release(source);
  115. source = sourceIn;
  116. return *this;
  117. }
  118. inline OBSSourceRef &operator=(const OBSSourceRef &ref)
  119. {
  120. obs_source_addref(ref.source);
  121. obs_source_release(source);
  122. source = ref.source;
  123. return *this;
  124. }
  125. inline OBSSourceRef &operator=(OBSSourceRef &&ref)
  126. {
  127. if (this != &ref) {
  128. source = ref.source;
  129. ref.source = NULL;
  130. }
  131. return *this;
  132. }
  133. inline operator obs_source_t() const {return source;}
  134. inline bool operator==(obs_source_t p) const {return source == p;}
  135. inline bool operator!=(obs_source_t p) const {return source != p;}
  136. };