The Battle for Wesnoth  1.19.7+dev
name_generator_factory.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2024
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 "log.hpp"
17 #include "utils/name_generator.hpp"
20 #include "formula/string_utils.hpp"
21 
22 static lg::log_domain log_wml("wml");
23 #define ERR_WML LOG_STREAM(err, log_wml)
24 
25 std::string name_generator::generate(const std::map<std::string,std::string>& variables) const {
27 }
28 
29 name_generator_factory::name_generator_factory(const config& config, std::vector<std::string> ids) : name_generators_() {
31 
32  for (std::vector<std::string>::iterator it = std::begin(ids); it!=std::end(ids); ++it) {
33  std::string id = *it;
34  add_name_generator_from_config(config, id, (id + "_"));
35  }
36 }
37 
38 void name_generator_factory::add_name_generator_from_config(const config& config, const std::string& id, const std::string& prefix) {
39  std::string cfg_name = prefix + "name_generator";
40  std::string markov_name = prefix + "names";
41 
42  if(config.has_attribute(cfg_name)) {
43  try {
45  return;
46  }
47  catch (const name_generator_invalid_exception& ex) {
48  lg::log_to_chat() << ex.what() << '\n';
49  ERR_WML << ex.what();
50  }
51  }
52 
53  if(config.has_attribute(markov_name)) {
54  config::attribute_value markov_name_list = config[markov_name];
55 
56  if(!markov_name_list.blank()) {
57  name_generators_[id].reset(new markov_generator(utils::split(markov_name_list), config["markov_chain_size"].to_int(2), 12));
58  }
59  }
60 }
61 
62 std::shared_ptr<name_generator> name_generator_factory::get_name_generator() {
63  std::map<std::string, std::shared_ptr<name_generator>>::const_iterator it = name_generators_.find("");
64  if(it == name_generators_.end()) {
65  //create a dummy instance, which always returns the empty string
66  return std::make_shared<name_generator>();
67  }
68 
69  return it->second;
70 }
71 
72 std::shared_ptr<name_generator> name_generator_factory::get_name_generator(const std::string& id) {
73  std::map<std::string, std::shared_ptr<name_generator>>::const_iterator it = name_generators_.find(id);
74  if(it == name_generators_.end()) {
75  return get_name_generator();
76  }
77 
78  return it->second;
79 }
Variant for storing WML attributes.
bool blank() const
Tests for an attribute that was never set.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
bool has_attribute(config_key_type key) const
Definition: config.cpp:157
std::shared_ptr< name_generator > get_name_generator()
Gets the default name generator.
void add_name_generator_from_config(const config &config, const std::string &id, const std::string &prefix)
Determines a name generator from WML data.
std::map< std::string, std::shared_ptr< name_generator > > name_generators_
name_generator_factory(const config &config, std::vector< std::string > ids)
Creates a new name generator factory.
const char * what() const noexcept
virtual std::string generate() const
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:198
Standard logging facilities (interface).
#define ERR_WML
static lg::log_domain log_wml("wml")
std::stringstream & log_to_chat()
Use this to show WML errors in the ingame chat.
Definition: log.cpp:520
std::string interpolate_variables_into_string(const std::string &str, const string_map *const symbols)
Function which will interpolate variables, starting with '$' in the string 'str' with the equivalent ...
std::vector< std::string > split(const config_attribute_value &val)
std::string::const_iterator iterator
Definition: tokenizer.hpp:25