The Battle for Wesnoth  1.15.1+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 start_line Line in file
49  * @param file Name of file
50  */
51  virtual void open_tag(const std::string & name,
52  const config& parent,
53  int start_line,
54  const std::string &file,
55  bool addittion = false) = 0;
56  /**
57  * As far as parser is built on stack, some realizations can store stack
58  * too. So they need to know if tag was closed.
59  */
60  virtual void close_tag() = 0;
61  /**
62  * Validates config. Checks if all mandatory elements are present.
63  * What exactly is validated depends on validator realization
64  * @param cfg Config to be validated.
65  * @param name Name of tag
66  * @param start_line Line in file
67  * @param file Name of file
68  */
69  virtual void validate(const config & cfg,
70  const std::string & name,
71  int start_line,
72  const std::string &file) = 0;
73  /**
74  * Checks if key is allowed and if its value is valid
75  * What exactly is validated depends on validator realization
76  * @param cfg Config to be validated.
77  * @param name Name of tag
78  * @param start_line Line in file
79  * @param file Name of file
80  */
81 
82  virtual void validate_key(const config & cfg,
83  const std::string & name,
84  const std::string & value,
85  int start_line,
86  const std::string &file) = 0;
87  /**
88  * @struct error
89  * Used to manage with not initialized validators
90  * Supposed to be thrown from the constructor
91  */
92  struct error : public game::error {
93  error(const std::string& message) : game::error(message) {}
94  };
95 
96  const std::string name_;
97 };
virtual void open_tag(const std::string &name, const config &parent, int start_line, const std::string &file, bool addittion=false)=0
Is called when parser opens tag.
bool strict_validation_enabled
Definition: validator.cpp:19
const std::string name_
Definition: validator.hpp:96
virtual void close_tag()=0
As far as parser is built on stack, some realizations can store stack too.
static const char * name(const std::vector< SDL_Joystick *> &joysticks, const std::size_t index)
Definition: joystick.cpp:48
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:92
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:29
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:31
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
error(const std::string &message)
Definition: validator.hpp:93