AuboCaps  0.5.0
input_validator.h
Go to the documentation of this file.
1 #ifndef AUBO_SCOPE_KEYBOARD_INPUT_VALIDATOR_H
2 #define AUBO_SCOPE_KEYBOARD_INPUT_VALIDATOR_H
3 
4 #include <string>
5 #include <QObject>
6 
8 
9 namespace arcs {
10 namespace aubo_scope {
11 
12 /**
13  * <p>
14  * Interface representing the input validators created by {@link
15  * InputValidationFactory}. This factory provides a set of standard input
16  * validators that can be used for validating the input entered by the user
17  * using the virtual keyboard/keypad.
18  * </p>
19  *
20  * If the standard validators available in the {@link InputValidationFactory}
21  * does not satisfy your needs, you are free to implement your own custom
22  * validator.
23  *
24  * @param <T> the (generic) type parameter for the interface representing the
25  * type of input data being validated (e.g. Integer or Double).
26  */
27 template <typename T>
29 {
30  /**
31  * @param value to be validated.
32  * @return <code>true</code> if value is valid.
33  */
34  virtual bool isValid(T value) = 0;
35 
36  /**
37  * Returns a meaningful message in case the value is not valid.
38  *
39  * @param value the invalid value. Can be included in the message if it
40  * makes sense.
41  * @return message.
42  */
43  virtual std::string getMessage(T value) = 0;
44 };
45 
46 template <typename T>
48 {
49  RangeValidator(T minValue, T maxValue)
50  : min_value_(minValue), max_value_(maxValue)
51  {
52  }
53 
54  bool isValid(T value) override
55  {
56  return value >= min_value_ && value <= max_value_;
57  }
58 
59  std::string getMessage(T value) override
60  {
61  if (value < min_value_ || value > max_value_) {
62  if constexpr (std::is_same<T, double>::value) {
63  return QObject::tr("Error: The value is between %1 and %2.")
64  .arg(min_value_, 0, 'g', 15)
65  .arg(max_value_, 0, 'g', 15)
66  .toStdString();
67  } else {
68  return QObject::tr("Error: The value is between %1 and %2.")
69  .arg(min_value_, 0)
70  .arg(max_value_, 0)
71  .toStdString();
72  }
73  }
74  return "";
75  }
76 
79 };
80 
84 
86 {
87  StringLengthValidator(int minLength, int maxLength)
88  : min_length_(minLength), max_length_(maxLength)
89  {
90  }
91 
92  bool isValid(std::string value) override
93  {
94  return (int)value.length() >= min_length_ &&
95  (int)value.length() <= max_length_;
96  }
97 
98  std::string getMessage(std::string value) override
99  {
100  if ((int)value.length() < min_length_ ||
101  (int)value.length() > max_length_) {
102  return QObject::tr(
103  "Error: The value is between %1 and %2 in length.")
104  .arg(min_length_)
105  .arg(min_length_)
106  .toStdString();
107  }
108  return "";
109  }
112 };
113 } // namespace aubo_scope
114 } // namespace arcs
115 
116 #endif
bool isValid(std::string value) override
virtual std::string getMessage(T value)=0
Returns a meaningful message in case the value is not valid.
std::string getMessage(T value) override
Returns a meaningful message in case the value is not valid.
RangeValidator(T minValue, T maxValue)
StringLengthValidator(int minLength, int maxLength)
std::string getMessage(std::string value) override
Returns a meaningful message in case the value is not valid.
bool isValid(T value) override
virtual bool isValid(T value)=0