Files
Mike Kaganski 3a9f1198ab Introduce ScopedConfigValue for easier scoped officecfg manipulation
Helps to reliably restore the correct previous value of a setting:
in many cases, the old restoration code used hardcoded values, which
could get out of sync with current defaults. Also, makes the code
easier to read.

Change-Id: Ia60b4e42192009c16bdbe58c540980c13626d5d8
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/198479
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Tested-by: Jenkins
2026-02-01 12:21:11 +01:00

42 lines
1.3 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <comphelper/configuration.hxx>
// RAII-style management of officecfg settings. Sets the setting specified as template parameter,
// to a value passed in constructor. Stores the previous value of the setting, and restores it in
// destructor.
template <class OfficeCfg> class ScopedConfigValue
{
using ConfigValue_t = decltype(OfficeCfg::get());
public:
ScopedConfigValue(const ConfigValue_t& rNewVal)
: maOrigValue(OfficeCfg::get())
{
auto pChanges(comphelper::ConfigurationChanges::create());
OfficeCfg::set(rNewVal, pChanges);
pChanges->commit();
}
ScopedConfigValue(const ScopedConfigValue&) = delete;
~ScopedConfigValue()
{
auto pChanges(comphelper::ConfigurationChanges::create());
OfficeCfg::set(maOrigValue, pChanges);
pChanges->commit();
}
private:
ConfigValue_t maOrigValue;
};
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */