Refactored config option to separate file

This commit is contained in:
Bartek Kryza 2022-02-06 23:09:20 +01:00
parent fe3f09a9c3
commit 45c26f76a3
3 changed files with 71 additions and 46 deletions

View File

@ -19,6 +19,7 @@
#include "class_diagram/model/diagram.h"
#include "common/model/enums.h"
#include "option.h"
#include "util/util.h"
#include <spdlog/spdlog.h>
@ -66,52 +67,6 @@ struct filter {
std::string to_string(const diagram_type t);
template <typename T> void append_value(T &l, const T &r) { l = r; }
enum class option_inherit_mode { override, append };
template <typename T> struct option {
option(const std::string &name_,
option_inherit_mode im = option_inherit_mode::override)
: name{name_}
, value{{}}
{
}
option(const std::string &name_, const T &initial_value,
option_inherit_mode im = option_inherit_mode::override)
: name{name_}
, value{initial_value}
{
}
void set(const T &v)
{
value = v;
is_declared = true;
}
void override(const option<T> &o)
{
if (!is_declared && o.is_declared) {
if (inheritance_mode == option_inherit_mode::append)
append_value(value, o.value);
else
value = o.value;
is_declared = true;
}
}
T &operator()() { return value; }
const T &operator()() const { return value; }
std::string name;
T value;
bool is_declared{false};
option_inherit_mode inheritance_mode;
};
struct inheritable_diagram_options {
option<std::vector<std::string>> glob{"glob"};
option<std::vector<std::string>> using_namespace{"using_namespace"};

69
src/config/option.h Normal file
View File

@ -0,0 +1,69 @@
/**
* src/config/option.h
*
* Copyright (c) 2021-2022 Bartek Kryza <bkryza@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
namespace clanguml {
namespace config {
template <typename T> void append_value(T &l, const T &r) { l = r; }
enum class option_inherit_mode { override, append };
template <typename T> struct option {
option(const std::string &name_,
option_inherit_mode im = option_inherit_mode::override)
: name{name_}
, value{{}}
{
}
option(const std::string &name_, const T &initial_value,
option_inherit_mode im = option_inherit_mode::override)
: name{name_}
, value{initial_value}
{
}
void set(const T &v)
{
value = v;
is_declared = true;
}
void override(const option<T> &o)
{
if (!is_declared && o.is_declared) {
if (inheritance_mode == option_inherit_mode::append)
append_value(value, o.value);
else
value = o.value;
is_declared = true;
}
}
T &operator()() { return value; }
const T &operator()() const { return value; }
std::string name;
T value;
bool is_declared{false};
option_inherit_mode inheritance_mode;
};
}
}

View File

@ -1,6 +1,7 @@
type: class
include_relations_also_as_members: false
glob:
- src/config/option.h
- src/config/config.h
- src/config/config.cc
include: