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