The Battle for Wesnoth  1.17.0-dev
name_generator_factory.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017-2018 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 "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_() {
30  add_name_generator_from_config(config, "", "");
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 {
44  name_generators_[id].reset(new context_free_grammar_generator(config[cfg_name]));
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 }
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:210
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:214
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:59
std::map< std::string, std::shared_ptr< name_generator > > name_generators_
std::string::const_iterator iterator
Definition: tokenizer.hpp:24
const char * what() const noexcept
std::stringstream & log_to_chat()
Use this to show WML errors in the ingame chat.
Definition: log.cpp:288