AUI Framework  master
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AValidator.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 Alex2772 and Contributors
    4 *
    5 * SPDX-License-Identifier: MPL-2.0
    6 *
    7 * This Source Code Form is subject to the terms of the Mozilla Public
    8 * License, v. 2.0. If a copy of the MPL was not distributed with this
    9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
   10 */
   11
   12#pragma once
   13
   14#include <AUI/Common/AString.h>
   15#include <type_traits>
   16
   17namespace aui::valid {
   18    struct validator {}; // flag class which marks class as a validator
   19
   20    namespace impl {
   21        template<typename Validator1, typename Validator2>
   22        struct And: validator {
   23            template<typename T>
   24            bool operator()(T t) const noexcept {
   25                return Validator1()(t) && Validator2()(t);
   26            }
   27        };
   28
   29        template<typename Validator1, typename Validator2>
   30        struct Or: validator {
   31            template<typename T>
   32            bool operator()(T t) const noexcept {
   33                return Validator1()(t) || Validator2()(t);
   34            }
   35        };
   36    }
   37
   38    template<typename T>
   39    constexpr bool is_validator = std::is_base_of_v<validator, T>;
   40
   41    template<typename Validator1, typename Validator2, std::enable_if_t<is_validator<Validator1> &&
   42                                                                        is_validator<Validator2>, bool> = true>
   43    auto operator||(Validator1 v1, Validator2 v2) noexcept {
   44
   45        return impl::Or<Validator1, Validator2>();
   46    }
   47
   48    template<typename Validator1, typename Validator2, std::enable_if_t<is_validator<Validator1> &&
   49                                                                        is_validator<Validator2>, bool> = true>
   50    auto operator&&(Validator1 v1, Validator2 v2) noexcept {
   51
   52        return impl::And<Validator1, Validator2>();
   53    }
   54
   59    template<auto lower, auto higher>
   60    struct in_range: validator {
   61        template<typename T>
   62        bool operator()(T t) const noexcept {
   63            return t >= lower && t <= higher;
   64        }
   65    };
   66
   67    namespace chars {
   71        struct latin: validator {
   72
   73            bool operator()(int c) const noexcept {
   74                return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
   75            }
   76        };
   77
   81        struct numeric: validator {
   82            bool operator()(int c) const noexcept {
   83                return (c >= '0' && c <= '9');
   84            }
   85        };
   86
   90        using latin_numeric = decltype(latin() || numeric());
   91    }
   92
   93    namespace container {
   98        template<typename Validator>
   99        struct any: validator {
  100            Validator validator;
  101            any(Validator validator = {}): validator(std::move(validator)) {}
  102
  103            template<typename Container>
  104            bool operator()(const Container& c) const noexcept {
  105                for (const auto& elem : c) {
  106                    if (validator(elem)) return true;
  107                }
  108                return false;
  109            }
  110        };
  111
  116        template<typename Validator>
  117        struct all: validator {
  118            Validator validator;
  119            all(Validator validator = {}): validator(std::move(validator)) {}
  120
  121            template<typename Container>
  122            bool operator()(const Container& c) const noexcept {
  123                for (const auto& elem : c) {
  124                    if (!Validator()(elem)) return false;
  125                }
  126                return true;
  127            }
  128        };
  129    }
  130
  131
  132    namespace string {
  133
  137        using latin = aui::valid::container::all<chars::latin>;
  138
  142        using numeric = aui::valid::container::all<chars::numeric>;
  143
  147        using latin_numeric = aui::valid::container::all<chars::latin_numeric>;
  148    }
  149}
Definition AValidator.h:71
Definition AValidator.h:81
Definition AValidator.h:117
Definition AValidator.h:22
Definition AValidator.h:30
Definition AValidator.h:60
Definition AValidator.h:18