The Battle for Wesnoth  1.19.5+dev
validator.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2024
3  by Sytyi Nick <nsytyi@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * @file validator.hpp
18  * This file contains information about validation abstract level interface.
19  */
20 
21 #pragma once
22 
23 #include "exceptions.hpp"
24 
25 #include <string>
26 
27 class config;
29 extern bool strict_validation_enabled;
30 
31 /**
32  * @class abstract_validator
33  * Used in parsing config file. @ref parser.cpp
34  * Contains virtual methods, which are called by parser
35  * and take information about config to be validated
36  */
38 {
39 public:
40  /**
41  * Constructor of validator can throw validator::error
42  * @throws abstract_validator::error
43  */
44  abstract_validator(const std::string& name) : name_(name) {}
45 
46  virtual ~abstract_validator(){}
47  /**
48  * Is called when parser opens tag.
49  * @param name Name of tag
50  * @param parent The parent config
51  * @param start_line Line in file
52  * @param file Name of file
53  * @param addition
54  */
55  virtual void open_tag(const std::string & name,
56  const config& parent,
57  int start_line,
58  const std::string &file,
59  bool addition = false) = 0;
60  /**
61  * As far as parser is built on stack, some realizations can store stack
62  * too. So they need to know if tag was closed.
63  */
64  virtual void close_tag() = 0;
65  /**
66  * Validates config. Checks if all mandatory elements are present.
67  * What exactly is validated depends on validator realization
68  * @param cfg Config to be validated.
69  * @param name Name of tag
70  * @param start_line Line in file
71  * @param file Name of file
72  */
73  virtual void validate(const config & cfg,
74  const std::string & name,
75  int start_line,
76  const std::string &file) = 0;
77  /**
78  * Checks if key is allowed and if its value is valid
79  * What exactly is validated depends on validator realization
80  * @param cfg Config to be validated.
81  * @param name Name of tag
82  * @param value The key's value
83  * @param start_line Line in file
84  * @param file Name of file
85  */
86 
87  virtual void validate_key(const config & cfg,
88  const std::string & name,
89  const config_attribute_value & value,
90  int start_line,
91  const std::string &file) = 0;
92  /**
93  * @struct error
94  * Used to manage with not initialized validators
95  * Supposed to be thrown from the constructor
96  */
97  struct error : public game::error {
98  error(const std::string& message) : game::error(message) {}
99  };
100 
101  const std::string name_;
102 };
Used in parsing config file.
Definition: validator.hpp:38
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...
virtual void validate(const config &cfg, const std::string &name, int start_line, const std::string &file)=0
Validates config.
virtual ~abstract_validator()
Definition: validator.hpp:46
abstract_validator(const std::string &name)
Constructor of validator can throw validator::error.
Definition: validator.hpp:44
virtual void close_tag()=0
As far as parser is built on stack, some realizations can store stack too.
const std::string name_
Definition: validator.hpp:101
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.
Variant for storing WML attributes.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
Used to manage with not initialized validators Supposed to be thrown from the constructor.
Definition: validator.hpp:97
error(const std::string &message)
Definition: validator.hpp:98
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:29
std::string message
Definition: exceptions.hpp:30
bool strict_validation_enabled
Definition: validator.cpp:21