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