Files
obs-studio/frontend/utility/OBSCanvas.cpp
Damian Marcin Szymański 9331fb7dac frontend: Fix build failure with Clang 20+ in OBSCanvas
Canvas is a move-only type without a copy constructor. Since C++17
requires types stored in std::optional to be copy-constructible unless
explicitly allowed, Clang 20+ emits an error when attempting to
instantiate std::optional<Canvas>.

While GCC allows this as an extension, Clang enforces the standard
more strictly.

This PR replaces std::optional<Canvas> with std::unique_ptr<Canvas> to
resolve the build error with Clang 20+ while keeping functional
behavior identical.

Tested with GCC 15.1.0 and Clang 20.1.7.

Co-Authored-By: Ryan Foster <ryan@obsproject.com>
2025-07-15 14:29:05 -04:00

101 lines
2.5 KiB
C++

/******************************************************************************
Copyright (C) 2025 by Dennis Sädtler <saedtler@twitch.tv>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "OBSCanvas.hpp"
#include <utility>
namespace OBS {
Canvas::Canvas(obs_canvas_t *canvas) : canvas(canvas) {}
Canvas::Canvas(Canvas &&other) noexcept
{
canvas = std::exchange(other.canvas, nullptr);
}
Canvas::~Canvas() noexcept
{
if (!canvas)
return;
obs_canvas_remove(canvas);
obs_canvas_release(canvas);
canvas = nullptr;
}
Canvas &Canvas::operator=(Canvas &&other) noexcept
{
canvas = std::exchange(other.canvas, canvas);
return *this;
}
std::optional<OBSDataAutoRelease> Canvas::Save() const
{
if (!canvas)
return std::nullopt;
if (obs_data_t *saved = obs_save_canvas(canvas))
return saved;
return std::nullopt;
}
std::unique_ptr<Canvas> Canvas::Load(obs_data_t *data)
{
if (OBSDataAutoRelease canvas_data = obs_data_get_obj(data, "info")) {
if (obs_canvas_t *canvas = obs_load_canvas(canvas_data)) {
return std::make_unique<Canvas>(canvas);
}
}
return nullptr;
}
std::vector<Canvas> Canvas::LoadCanvases(obs_data_array_t *canvases)
{
auto cb = [](obs_data_t *data, void *param) -> void {
auto vec = static_cast<std::vector<Canvas> *>(param);
if (auto canvas = Canvas::Load(data))
vec->emplace_back(std::move(*canvas));
};
std::vector<Canvas> ret;
obs_data_array_enum(canvases, cb, &ret);
return ret;
}
OBSDataArrayAutoRelease Canvas::SaveCanvases(const std::vector<Canvas> &canvases)
{
OBSDataArrayAutoRelease savedCanvases = obs_data_array_create();
for (auto &canvas : canvases) {
auto canvas_data = canvas.Save();
if (!canvas_data)
continue;
OBSDataAutoRelease data = obs_data_create();
obs_data_set_obj(data, "info", *canvas_data);
obs_data_array_push_back(savedCanvases, data);
}
return savedCanvases;
}
} // namespace OBS