AuboCaps  0.6.1
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 <functional>
6 #include <float.h>
7 
8 #include <QObject>
9 
11 
12 namespace arcs {
13 namespace aubo_scope {
14 
15 /**
16  * <p>
17  * Interface representing the input validators created by {@link
18  * InputValidationFactory}. This factory provides a set of standard input
19  * validators that can be used for validating the input entered by the user
20  * using the virtual keyboard/keypad.
21  * </p>
22  *
23  * If the standard validators available in the {@link InputValidationFactory}
24  * does not satisfy your needs, you are free to implement your own custom
25  * validator.
26  *
27  * @param <T> the (generic) type parameter for the interface representing the
28  * type of input data being validated (e.g. Integer or Double).
29  */
31 {
32  /**
33  * @param value to be validated.
34  * @return <code>true</code> if value is valid.
35  */
36  virtual bool isValid(std::string value) = 0;
37 
38  /**
39  * Returns a meaningful message in case the value is not valid.
40  *
41  * @param value the invalid value. Can be included in the message if it
42  * makes sense.
43  * @return message.
44  */
45  virtual std::string getMessage(std::string value) = 0;
46 };
47 
49 {
50  IntegerRangeValidator(int min_value, int max_value);
51 
52  IntegerRangeValidator(std::function<std::pair<int, int>()> range_func);
53 
54  bool isValid(std::string value) override;
55 
56  std::string getMessage(std::string value) override;
57 
58 private:
59  void updateValue();
60 
61 private:
62  int min_value_{ INT_MIN };
63  int max_value_{ INT_MAX };
64 
65  std::function<std::pair<int, int>()> range_func_{ nullptr };
66 };
67 
69 {
70  UIntegerRangeValidator(uint32_t min_value, uint32_t max_value);
71 
73  std::function<std::pair<uint32_t, uint32_t>()> range_func);
74 
75  bool isValid(std::string value) override;
76 
77  std::string getMessage(std::string value) override;
78 
79 private:
80  void updateValue();
81 
82 private:
83  uint32_t min_value_{ 0 };
84  uint32_t max_value_{ UINT_MAX };
85 
86  std::function<std::pair<uint32_t, uint32_t>()> range_func_{ nullptr };
87 };
88 
90 {
91  DoubleRangeValidator(double minValue, double maxValue,
92  bool is_min_inclusive = true,
93  bool is_max_inclusive = true);
94 
95  DoubleRangeValidator(std::function<std::pair<double, double>()> range_func);
96 
98  std::function<std::tuple<double, double, bool, bool>()> range_func);
99 
100  bool isValid(std::string value) override;
101 
102  std::string getMessage(std::string value) override;
103 
104 private:
105  void updateValue();
106 
107 private:
108  double min_value_{ DBL_MIN };
109  double max_value_{ DBL_MAX };
110  std::function<std::pair<double, double>()> range_func_{ nullptr };
111  std::function<std::tuple<double, double, bool, bool>()>
112  range_func_with_flags_{ nullptr };
113  bool is_min_inclusive_{ true }; // 最小值是否闭区间
114  bool is_max_inclusive_{ true }; // 最大值是否闭区间
115 };
116 
118 {
119  StringLengthValidator(size_t min_length, size_t max_length);
120 
122  std::function<std::pair<size_t, size_t>()> range_func);
123 
124  bool isValid(std::string value) override;
125 
126  std::string getMessage(std::string value) override;
127 
128 private:
129  void updateValue();
130 
131 private:
132  size_t min_length_{ 0 };
133  size_t max_length_{ ULONG_MAX };
134  std::function<std::pair<size_t, size_t>()> range_func_{ nullptr };
135 };
136 
137 } // namespace aubo_scope
138 } // namespace arcs
139 
140 #endif
virtual bool isValid(std::string value)=0
virtual std::string getMessage(std::string value)=0
Returns a meaningful message in case the value is not valid.