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