I have a series of field validators that each look something like the following:
template <typename T>
class NameValidator : public Validator<T> {
...
bool validate(const T& msg) const override { ... }
...
};
Each of these validators must be able to validate different message types, hence the template parameter T.
I would like to create a manager class that serves as a common gateway to each of these validators. Something like the following:
class ValidatorManger {
...
// Calls validate() functions for each field.
template <typename T>
bool validate(const T& msg) { ... }
...
};
Hence, I will need to store each of the validator classes (ex: NameValidator<T>) in some type of data structure and then iterate over them within ValidatorManager::validate().
Is there a way to do this so that I do not have to explicitly specialize the templates for each message type? I'm imaging something like the following
validator_map.insert(std::pair("Name", NameValidator<T>));
validator_map.insert(std::pair("Age", AgeValidator<T>());
...
though this is obviously gibberish.
Questions:
- Has anyone used this sort of pattern before and have a solution for achieving this?
- Should I be rethinking this design entirely?
Aucun commentaire:
Enregistrer un commentaire