The Battle for Wesnoth  1.17.0-dev
advanced.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2020 - 2021
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "preferences/advanced.hpp"
16 
17 #include "game_config_view.hpp"
18 #include "gettext.hpp"
19 #include "log.hpp"
20 
21 #include <cassert>
22 
23 static lg::log_domain advanced_preferences("advanced_preferences");
24 #define ERR_ADV LOG_STREAM(err, advanced_preferences)
25 
26 namespace preferences
27 {
28 static std::unique_ptr<preferences::advanced_manager> singleton = nullptr;
29 
31  : prefs()
32 {
33  for(const config& pref : gc.child_range("advanced_preference")) {
34  try {
35  prefs.emplace_back(pref);
36  } catch(const std::invalid_argument& e) {
37  ERR_ADV << e.what() << std::endl;
38  continue;
39  }
40  }
41 
42  std::sort(prefs.begin(), prefs.end(),
43  [](const auto& lhs, const auto& rhs) { return translation::icompare(lhs.name, rhs.name) < 0; });
44 }
45 
47 {
48 }
49 
51  : type()
52  , name(pref["name"].t_str())
53  , description(pref["description"].t_str())
54  , field(pref["field"].str())
55  , cfg(pref)
56 {
57  const std::string p_type = cfg["type"];
58 
59  if(p_type == "boolean") {
61  } else if(p_type == "int") {
63  } else if(p_type == "combo") {
65  } else if(p_type == "custom") {
67  } else {
68  throw std::invalid_argument("Unknown type '" + p_type + "' for advanced preference " + name);
69  }
70 }
71 
73 {
74  singleton = std::make_unique<advanced_manager>(gc);
75 }
76 
78 {
79  assert(singleton && "Advanced preference manager singleton is null!");
80  return singleton->get_preferences();
81 }
82 
83 } // namespace preferences
const advanced_pref_list & get_advanced_preferences()
Gets a list of the available advanced preferences.
Definition: advanced.cpp:77
t_string name
Displayed name.
Definition: advanced.hpp:45
#define ERR_ADV
Definition: advanced.cpp:24
config_array_view child_range(config_key_type key) const
static void field(LexState *ls, ConsControl *cc)
Definition: lparser.cpp:891
std::vector< advanced_manager::option > advanced_pref_list
Definition: advanced.hpp:69
config cfg
The full config, including type-specific options.
Definition: advanced.hpp:54
const std::vector< option > & get_preferences() const
Definition: advanced.hpp:57
avd_type type
The preference type.
Definition: advanced.hpp:42
Modify, read and display user preferences.
static std::unique_ptr< preferences::advanced_manager > singleton
Definition: advanced.cpp:28
static int sort(lua_State *L)
Definition: ltablib.cpp:397
int icompare(const std::string &s1, const std::string &s2)
Case-insensitive lexicographical comparison.
Definition: gettext.cpp:491
Standard logging facilities (interface).
advanced_manager(const game_config_view &gc)
Definition: advanced.cpp:30
#define e
static lg::log_domain advanced_preferences("advanced_preferences")
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
void init_advanced_manager(const game_config_view &gc)
Initializes the manager singleton.
Definition: advanced.cpp:72