obs-wrappers.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 <obs.h>
  19. /* RAII wrappers */
  20. template<typename T> class BPtr {
  21. T ptr;
  22. public:
  23. inline BPtr() : ptr(NULL) {}
  24. inline BPtr(T p) : ptr(p) {}
  25. inline ~BPtr() {bfree(ptr);}
  26. inline T operator=(T p) {bfree(ptr); ptr = p;}
  27. inline operator T() {return ptr;}
  28. inline T *operator&() {bfree(ptr); ptr = NULL; return &ptr;}
  29. inline bool operator!() {return ptr == NULL;}
  30. inline bool operator==(T p) {return ptr == p;}
  31. inline bool operator!=(T p) {return ptr != p;}
  32. };
  33. class ConfigFile {
  34. config_t config;
  35. public:
  36. inline ConfigFile() : config(NULL) {}
  37. inline ~ConfigFile()
  38. {
  39. config_close(config);
  40. }
  41. inline bool Create(const char *file)
  42. {
  43. Close();
  44. config = config_create(file);
  45. return config != NULL;
  46. }
  47. int Open(const char *file, config_open_type openType)
  48. {
  49. Close();
  50. return config_open(&config, file, openType);
  51. }
  52. int Save()
  53. {
  54. return config_save(config);
  55. }
  56. void Close()
  57. {
  58. config_close(config);
  59. config = NULL;
  60. }
  61. inline operator config_t() {return config;}
  62. };
  63. class OBSSource {
  64. obs_source_t source;
  65. public:
  66. inline OBSSource(obs_source_t source) : source(source) {}
  67. inline ~OBSSource() {obs_source_release(source);}
  68. inline OBSSource& operator=(obs_source_t p) {source = p;}
  69. inline operator obs_source_t() {return source;}
  70. inline bool operator==(obs_source_t p) const {return source == p;}
  71. inline bool operator!=(obs_source_t p) const {return source != p;}
  72. };
  73. class OBSSourceRef {
  74. obs_source_t source;
  75. public:
  76. inline OBSSourceRef(obs_source_t source) : source(source)
  77. {
  78. obs_source_addref(source);
  79. }
  80. inline OBSSourceRef(const OBSSourceRef &ref) : source(ref.source)
  81. {
  82. obs_source_addref(source);
  83. }
  84. inline OBSSourceRef(OBSSourceRef &&ref) : source(ref.source)
  85. {
  86. ref.source = NULL;
  87. }
  88. inline ~OBSSourceRef() {obs_source_release(source);}
  89. inline OBSSourceRef &operator=(obs_source_t sourceIn)
  90. {
  91. obs_source_addref(sourceIn);
  92. obs_source_release(source);
  93. source = sourceIn;
  94. return *this;
  95. }
  96. inline OBSSourceRef &operator=(const OBSSourceRef &ref)
  97. {
  98. obs_source_addref(ref.source);
  99. obs_source_release(source);
  100. source = ref.source;
  101. return *this;
  102. }
  103. inline OBSSourceRef &operator=(OBSSourceRef &&ref)
  104. {
  105. if (this != &ref) {
  106. source = ref.source;
  107. ref.source = NULL;
  108. }
  109. return *this;
  110. }
  111. inline operator obs_source_t() const {return source;}
  112. inline bool operator==(obs_source_t p) const {return source == p;}
  113. inline bool operator!=(obs_source_t p) const {return source != p;}
  114. };