The Battle for Wesnoth  1.15.1+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 std::string name_generator::generate(const std::map<std::string,std::string>& variables) const {
24 }
25 
26 name_generator_factory::name_generator_factory(const config& config, std::vector<std::string> ids) : name_generators_() {
27  add_name_generator_from_config(config, "", "");
28 
29  for (std::vector<std::string>::iterator it = std::begin(ids); it!=std::end(ids); ++it) {
30  std::string id = *it;
31  add_name_generator_from_config(config, id, (id + "_"));
32  }
33 }
34 
35 void name_generator_factory::add_name_generator_from_config(const config& config, const std::string id, const std::string prefix) {
36  std::string cfg_name = prefix + "name_generator";
37  std::string markov_name = prefix + "names";
38 
39  if(config.has_attribute(cfg_name)) {
40  try {
41  name_generators_[id].reset(new context_free_grammar_generator(config[cfg_name]));
42  return;
43  }
44  catch (const name_generator_invalid_exception& ex) {
45  lg::wml_error() << ex.what() << '\n';
46  }
47  }
48 
49  if(config.has_attribute(markov_name)) {
50  config::attribute_value markov_name_list = config[markov_name];
51 
52  if(!markov_name_list.blank()) {
53  name_generators_[id].reset(new markov_generator(utils::split(markov_name_list), config["markov_chain_size"].to_int(2), 12));
54  }
55  }
56 }
57 
58 std::shared_ptr<name_generator> name_generator_factory::get_name_generator() {
59  std::map<std::string, std::shared_ptr<name_generator>>::const_iterator it = name_generators_.find("");
60  if(it == name_generators_.end()) {
61  //create a dummy instance, which always returns the empty string
62  return std::make_shared<name_generator>();
63  }
64 
65  return it->second;
66 }
67 
68 std::shared_ptr<name_generator> name_generator_factory::get_name_generator(const std::string id) {
69  std::map<std::string, std::shared_ptr<name_generator>>::const_iterator it = name_generators_.find(id);
70  if(it == name_generators_.end()) {
71  return get_name_generator();
72  }
73 
74  return it->second;
75 }
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:213
virtual std::string generate() const
std::shared_ptr< name_generator > get_name_generator()
Gets the default name generator.
std::vector< std::string > split(const std::string &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
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::stringstream & wml_error()
Use this logger to send errors due to deprecated WML.
Definition: log.cpp:269
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).
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
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