Skip to content

Commit

Permalink
Avoid modifications over constexpr
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-matsui committed Feb 5, 2024
1 parent d3ba660 commit 49270a2
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions src/Cli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ class CliBase {
constexpr CliBase& operator=(CliBase&&) noexcept = default;

constexpr explicit CliBase(const StringRef name) noexcept : name(name) {}
constexpr Derived& setDesc(const StringRef desc) noexcept {
this->desc = desc;
return static_cast<Derived&>(*this);
constexpr Derived setDesc(const StringRef desc) noexcept {
Derived derived = static_cast<Derived&>(*this);
derived.desc = desc;
return derived;
}
};

Expand All @@ -47,13 +48,15 @@ class ShortAndHidden {
// NOLINTEND(cppcoreguidelines-non-private-member-variables-in-classes,misc-non-private-member-variables-in-classes)

public:
constexpr Derived& setShort(const StringRef shortName) noexcept {
this->shortName = shortName;
constexpr Derived setShort(const StringRef shortName) noexcept {
Derived derived = static_cast<Derived&>(*this);
derived.shortName = shortName;
return static_cast<Derived&>(*this);
}
constexpr Derived& setHidden(const bool isHidden) noexcept {
this->isHidden = isHidden;
return static_cast<Derived&>(*this);
constexpr Derived setHidden(const bool isHidden) noexcept {
Derived derived = static_cast<Derived&>(*this);
derived.isHidden = isHidden;
return derived;
}
};

Expand All @@ -77,16 +80,19 @@ class Opt : public CliBase<Opt>, public ShortAndHidden<Opt> {
printOpts(const Vec<Opt>& opts, usize maxShortSize, usize maxOffset) noexcept;

constexpr Opt setPlaceholder(const StringRef placeholder) noexcept {
this->placeholder = placeholder;
return *this;
Opt opt = *this;
opt.placeholder = placeholder;
return opt;
}
constexpr Opt setDefault(const StringRef defaultVal) noexcept {
this->defaultVal = defaultVal;
return *this;
Opt opt = *this;
opt.defaultVal = defaultVal;
return opt;
}
constexpr Opt setGlobal(const bool isGlobal) noexcept {
this->isGlobal = isGlobal;
return *this;
Opt opt = *this;
opt.isGlobal = isGlobal;
return opt;
}

private:
Expand All @@ -113,12 +119,14 @@ class Arg : public CliBase<Arg> {
using CliBase::CliBase;

constexpr Arg setRequired(const bool required) noexcept {
this->required = required;
return *this;
Arg arg = *this;
arg.required = required;
return arg;
}
constexpr Arg setVariadic(const bool variadic) noexcept {
this->variadic = variadic;
return *this;
Arg arg = *this;
arg.variadic = variadic;
return arg;
}

private:
Expand Down

0 comments on commit 49270a2

Please sign in to comment.