WSF
VariantValidator.hpp
Go to the documentation of this file.
1// ****************************************************************************
2// CUI
3//
4// The Advanced Framework for Simulation, Integration, and Modeling (AFSIM)
5//
6// Copyright 2019 Infoscitex, a DCS Company. All rights reserved.
7//
8// The use, dissemination or disclosure of data in this file is subject to
9// limitation or restriction. See accompanying README and LICENSE for details.
10// ****************************************************************************
11
12#ifndef VARIANTVALIDATOR_HPP
13#define VARIANTVALIDATOR_HPP
14
15#include "usmtf_export.h"
16
17#include <string>
18#include <tuple>
19#include <typeindex>
20#include <unordered_map>
21
22#include "FFIRN_Validator.hpp"
23
24// Variadic aware check to assert all Types are FFIRN_Validators
25namespace
26{
27// code from
28// https://stackoverflow.com/questions/13562823/parameter-pack-aware-stdis-base-of
29template<bool... b>
30struct static_all_of;
31
32// implementation: recurse, if the first argument is true
33template<bool... tail>
34struct static_all_of<true, tail...> : static_all_of<tail...>
35{
36};
37
38// end recursion if first argument is false -
39template<bool... tail>
40struct static_all_of<false, tail...> : std::false_type
41{
42};
43
44// - or if no more arguments
45template<>
46struct static_all_of<> : std::true_type
47{
48};
49} // namespace
50
51namespace usmtf
52{
60template<typename... Types>
62{
63 static_assert(static_all_of<std::is_base_of<FFIRN_Validator, Types>::value...>::value,
64 "Invalid Type, Must be an FFIRN_Validator");
65
66public:
67 VariantValidator() = default;
68 // By design once a passed validator is found, there is no way to copy the ptr that is to an internal
69 // data structure
70 VariantValidator(const VariantValidator& aOther) = delete;
71 VariantValidator(const VariantValidator&& aOther) = delete;
74
79 bool ParseAndValidate(const usmtf::Field& aField) override
80 {
81 bool res = ParseAndValidateP<0, sizeof...(Types)>{}(*this, aField);
83 return res;
84 }
85
90 template<typename T>
91 T* IsMatch() const
92 {
93 return dynamic_cast<T*>(mValidator);
94 }
95
97 bool HasMatch() const { return mValidator != nullptr; }
98
99private:
100 template<int N, int TypeCount>
101 struct ParseAndValidateP
102 {
103 bool operator()(VariantValidator<Types...>& aValidator, const Field& aField)
104 {
105 auto& ref = std::get<N>(aValidator.mTypeInstances);
106 if (ref.ParseAndValidate(aField))
107 {
108 aValidator.mValidator = &ref;
109 return true;
110 }
111 else
112 {
113 aValidator.AddErrors(ref.GetErrors());
114 }
115 return ParseAndValidateP<N + 1, TypeCount>{}(aValidator, aField);
116 }
117 };
118
119 template<int N>
120 struct ParseAndValidateP<N, N>
121 {
122 bool operator()(VariantValidator<Types...>& aValidator, const Field& aField) { return false; }
123 };
124
125 FFIRN_Validator* mValidator = nullptr;
126 std::tuple<Types...> mTypeInstances;
127};
128} // namespace usmtf
129
130#endif
void AddErrors(const std::vector< ValidationError > &aErrors) noexcept
Definition ErrorManager.cpp:53
void ClearErrorsIfValid(bool aHasAtLeastOnePassed) noexcept
Definition FFIRN_Validator.cpp:49
Definition Field.hpp:48
Definition VariantValidator.hpp:62
T * IsMatch() const
Definition VariantValidator.hpp:91
bool ParseAndValidate(const usmtf::Field &aField) override
Definition VariantValidator.hpp:79
VariantValidator operator=(const VariantValidator &&aOther)=delete
bool HasMatch() const
Returns false if no match was made or Parse and Validate has yet to be called.
Definition VariantValidator.hpp:97
VariantValidator(const VariantValidator &aOther)=delete
VariantValidator operator=(const VariantValidator &aOther)=delete
VariantValidator(const VariantValidator &&aOther)=delete
Definition Acmid.cpp:17
Copyrights Multiple, All Rights Reserved