|
| 1 | +/****************************************************************************** |
| 2 | + Copyright (C) 2025 by Dennis Sädtler <[email protected]> |
| 3 | +
|
| 4 | + This program is free software: you can redistribute it and/or modify |
| 5 | + it under the terms of the GNU General Public License as published by |
| 6 | + the Free Software Foundation, either version 2 of the License, or |
| 7 | + (at your option) any later version. |
| 8 | +
|
| 9 | + This program is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + GNU General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU General Public License |
| 15 | + along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +******************************************************************************/ |
| 17 | + |
| 18 | +#include "OBSCanvas.hpp" |
| 19 | + |
| 20 | +#include <utility> |
| 21 | + |
| 22 | +namespace OBS { |
| 23 | + |
| 24 | +Canvas::Canvas(obs_canvas_t *canvas) : canvas(canvas) {} |
| 25 | + |
| 26 | +Canvas::Canvas(Canvas &&other) noexcept |
| 27 | +{ |
| 28 | + canvas = std::exchange(other.canvas, nullptr); |
| 29 | +} |
| 30 | + |
| 31 | +Canvas::~Canvas() noexcept |
| 32 | +{ |
| 33 | + if (!canvas) |
| 34 | + return; |
| 35 | + |
| 36 | + obs_canvas_remove(canvas); |
| 37 | + obs_canvas_release(canvas); |
| 38 | + canvas = nullptr; |
| 39 | +} |
| 40 | + |
| 41 | +Canvas &Canvas::operator=(Canvas &&other) noexcept |
| 42 | +{ |
| 43 | + canvas = std::exchange(other.canvas, canvas); |
| 44 | + |
| 45 | + return *this; |
| 46 | +} |
| 47 | + |
| 48 | +std::optional<OBSDataAutoRelease> Canvas::Save() const |
| 49 | +{ |
| 50 | + if (!canvas) |
| 51 | + return std::nullopt; |
| 52 | + if (obs_data_t *saved = obs_save_canvas(canvas)) |
| 53 | + return saved; |
| 54 | + |
| 55 | + return std::nullopt; |
| 56 | +} |
| 57 | + |
| 58 | +std::optional<Canvas> Canvas::Load(obs_data_t *data) |
| 59 | +{ |
| 60 | + if (OBSDataAutoRelease canvas_data = obs_data_get_obj(data, "info")) { |
| 61 | + if (obs_canvas_t *canvas = obs_load_canvas(canvas_data)) { |
| 62 | + return canvas; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return std::nullopt; |
| 67 | +} |
| 68 | + |
| 69 | +std::vector<Canvas> Canvas::LoadCanvases(obs_data_array_t *canvases) |
| 70 | +{ |
| 71 | + auto cb = [](obs_data_t *data, void *param) -> void { |
| 72 | + auto vec = static_cast<std::vector<Canvas> *>(param); |
| 73 | + if (auto canvas = Canvas::Load(data)) |
| 74 | + vec->emplace_back(std::move(*canvas)); |
| 75 | + }; |
| 76 | + |
| 77 | + std::vector<Canvas> ret; |
| 78 | + obs_data_array_enum(canvases, cb, &ret); |
| 79 | + |
| 80 | + return ret; |
| 81 | +} |
| 82 | + |
| 83 | +OBSDataArrayAutoRelease Canvas::SaveCanvases(const std::vector<Canvas> &canvases) |
| 84 | +{ |
| 85 | + OBSDataArrayAutoRelease savedCanvases = obs_data_array_create(); |
| 86 | + |
| 87 | + for (auto &canvas : canvases) { |
| 88 | + auto canvas_data = canvas.Save(); |
| 89 | + if (!canvas_data) |
| 90 | + continue; |
| 91 | + |
| 92 | + OBSDataAutoRelease data = obs_data_create(); |
| 93 | + obs_data_set_obj(data, "info", *canvas_data); |
| 94 | + obs_data_array_push_back(savedCanvases, data); |
| 95 | + } |
| 96 | + |
| 97 | + return savedCanvases; |
| 98 | +} |
| 99 | + |
| 100 | +} // namespace OBS |
0 commit comments