The Battle for Wesnoth  1.17.0-dev
validator.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 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 /**
16  * @file validator.hpp
17  * This file contains information about validation abstract level interface.
18  */
19 
20 #pragma once
21 
22 #include "exceptions.hpp"
23 
24 #include <string>
25 
26 class config;
28 extern bool strict_validation_enabled;
29 
30 /**
31  * @class abstract_validator
32  * Used in parsing config file. @ref parser.cpp
33  * Contains virtual methods, which are called by parser
34  * and take information about config to be validated
35  */
37 {
38 public:
39  /**
40  * Constructor of validator can throw validator::error
41  * @throws abstract_validator::error
42  */
43  abstract_validator(const std::string& name) : name_(name) {}
44 
45  virtual ~abstract_validator(){}
46  /**
47  * Is called when parser opens tag.
48  * @param name Name of tag
49  * @param parent The parent config
50  * @param start_line Line in file
51  * @param file Name of file
52  * @param addition
53  */
54  virtual void open_tag(const std::string & name,
55  const config& parent,
56  int start_line,
57  const std::string &file,
58  bool addition = false) = 0;
59  /**
60  * As far as parser is built on stack, some realizations can store stack
61  * too. So they need to know if tag was closed.
62  */
63  virtual void close_tag() = 0;
64  /**
65  * Validates config. Checks if all mandatory elements are present.
66  * What exactly is validated depends on validator realization
67  * @param cfg Config to be validated.
68  * @param name Name of tag
69  * @param start_line Line in file
70  * @param file Name of file
71  */
72  virtual void validate(const config & cfg,
73  const std::string & name,
74  int start_line,
75  const std::string &file) = 0;
76  /**
77  * Checks if key is allowed and if its value is valid
78  * What exactly is validated depends on validator realization
79  * @param cfg Config to be validated.
80  * @param name Name of tag
81  * @param value The key's value
82  * @param start_line Line in file
83  * @param file Name of file
84  */
85 
86  virtual void validate_key(const config & cfg,
87  const std::string & name,
88  const config_attribute_value & value,
89  int start_line,
90  const std::string &file) = 0;
91  /**
92  * @struct error
93  * Used to manage with not initialized validators
94  * Supposed to be thrown from the constructor
95  */
96  struct error : public game::error {
97  error(const std::string& message) : game::error(message) {}
98  };
99 
100  const std::string name_;
101 };
Variant for storing WML attributes.
bool strict_validation_enabled
Definition: validator.cpp:20
const std::string name_
Definition: validator.hpp:100
virtual void close_tag()=0
As far as parser is built on stack, some realizations can store stack too.
Used in parsing config file.
Definition: validator.hpp:36
virtual ~abstract_validator()
Definition: validator.hpp:45
Used to manage with not initialized validators Supposed to be thrown from the constructor.
Definition: validator.hpp:96
virtual void open_tag(const std::string &name, const config &parent, int start_line, const std::string &file, bool addition=false)=0
Is called when parser opens tag.
abstract_validator(const std::string &name)
Constructor of validator can throw validator::error.
Definition: validator.hpp:43
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:27
virtual void validate(const config &cfg, const std::string &name, int start_line, const std::string &file)=0
Validates config.
std::string message
Definition: exceptions.hpp:29
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
virtual void validate_key(const config &cfg, const std::string &name, const config_attribute_value &value, int start_line, const std::string &file)=0
Checks if key is allowed and if its value is valid What exactly is validated depends on validator rea...
error(const std::string &message)
Definition: validator.hpp:97