summaryrefslogtreecommitdiffstats
path: root/src/util/optstr.h
blob: f8a0b4ce2fed87d2c86f1bd24dafa34de6f52b2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// SPDX-License-Identifier: GPL-2.0-or-later
#ifndef INKSCAPE_UTIL_OPTSTR_H
#define INKSCAPE_UTIL_OPTSTR_H
/*
 * Author: PBS <pbs3141@gmail.com>
 * Copyright (C) 2022 Authors
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <string>
#include <optional>

namespace Inkscape {
namespace Util {

inline bool equal(std::optional<std::string> const &a, char const *b)
{
    return a && b ? *a == b : !a && !b;
}

inline auto to_opt(char const *s)
{
    return s ? std::make_optional<std::string>(s) : std::nullopt;
}

inline auto to_cstr(std::optional<std::string> const &s)
{
    return s ? s->c_str() : nullptr;
}

inline bool assign(std::optional<std::string> &a, char const *b)
{
    if (equal(a, b)) return false;
    a = to_opt(b);
    return true;
}

} // namespace Util
} // namespace Inkscape

#endif // INKSCAPE_UTIL_OPTSTR_H