File indexing completed on 2024-04-28 05:37:01
0001 /*************************************************************************** 0002 * Copyright (C) 2014 by Renaud Guezennec * 0003 * https://rolisteam.org/contact * 0004 * * 0005 * This file is part of DiceParser * 0006 * * 0007 * DiceParser is free software; you can redistribute it and/or modify * 0008 * it under the terms of the GNU General Public License as published by * 0009 * the Free Software Foundation; either version 2 of the License, or * 0010 * (at your option) any later version. * 0011 * * 0012 * This program is distributed in the hope that it will be useful, * 0013 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0015 * GNU General Public License for more details. * 0016 * * 0017 * You should have received a copy of the GNU General Public License * 0018 * along with this program; if not, write to the * 0019 * Free Software Foundation, Inc., * 0020 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0021 ***************************************************************************/ 0022 #include "validator.h" 0023 0024 Validator::Validator() {} 0025 Validator::~Validator() {} 0026 0027 template <typename Functor> 0028 qint64 Validator::onEach(const std::vector<Die*>& b, bool recursive, bool unlight, Functor functor) const 0029 { 0030 qint64 result= 0; 0031 std::for_each(b.begin(), b.end(), [this, recursive, unlight, functor, &result](Die* die) { 0032 if(hasValid(die, recursive, unlight)) 0033 { 0034 ++result; 0035 functor(die, recursive, unlight); 0036 } 0037 }); 0038 return result; 0039 } 0040 0041 template <typename Functor> 0042 qint64 Validator::onEachValue(const std::vector<Die*>& b, bool recursive, bool unlight, Functor functor) const 0043 { 0044 qint64 result= 0; 0045 std::for_each(b.begin(), b.end(), [this, recursive, unlight, functor, &result](Die* die) { 0046 if(hasValid(die, recursive, unlight)) 0047 { 0048 ++result; 0049 functor(die, recursive, unlight); 0050 } 0051 }); 0052 return result; 0053 } 0054 0055 template <typename Functor> 0056 qint64 Validator::oneOfThem(const std::vector<Die*>& b, bool recursive, bool unlight, Functor functor) const 0057 { 0058 auto oneOfThem= std::any_of(b.begin(), b.end(), 0059 [this, recursive, unlight](Die* die) { return hasValid(die, recursive, unlight); }); 0060 if(oneOfThem) 0061 functor(recursive, unlight); 0062 return oneOfThem ? 1 : 0; 0063 } 0064 0065 template <typename Functor> 0066 qint64 Validator::allOfThem(const std::vector<Die*>& b, bool recursive, bool unlight, Functor functor) const 0067 { 0068 auto all= std::all_of(b.begin(), b.end(), 0069 [this, recursive, unlight](Die* die) { return hasValid(die, recursive, unlight); }); 0070 if(all) 0071 functor(recursive, unlight); 0072 return all ? 1 : 0; 0073 } 0074 0075 template <typename Functor> 0076 qint64 Validator::onScalar(const std::vector<Die*>& b, bool recursive, bool unlight, Functor functor) const 0077 { 0078 qint64 result= 0; 0079 for(const auto& die : b) 0080 { 0081 result+= die->getValue(); 0082 } 0083 Die die; 0084 die.setValue(result); 0085 if(hasValid(&die, recursive, unlight)) 0086 { 0087 functor(recursive, unlight); 0088 return 1; 0089 } 0090 return 0; 0091 } 0092 0093 const std::set<qint64>& Validator::getPossibleValues(const std::pair<qint64, qint64>&) 0094 { 0095 return m_values; 0096 } 0097 0098 template <typename Functor> 0099 qint64 Validator::validResult(const std::vector<Die*>& b, bool recursive, bool unlight, Functor functor) const 0100 { 0101 qint64 result; 0102 switch(m_conditionType) 0103 { 0104 case Dice::OnEach: 0105 result= onEach(b, recursive, unlight, functor); 0106 break; 0107 case Dice::OnEachValue: 0108 result= onEachValue(b, recursive, unlight, functor); 0109 break; 0110 case Dice::OneOfThem: 0111 result= oneOfThem(b, recursive, unlight, functor); 0112 break; 0113 case Dice::AllOfThem: 0114 result= allOfThem(b, recursive, unlight, functor); 0115 break; 0116 case Dice::OnScalar: 0117 result= onScalar(b, recursive, unlight, functor); 0118 break; 0119 } 0120 return result; 0121 } 0122 0123 Dice::ConditionType Validator::getConditionType() const 0124 { 0125 return m_conditionType; 0126 } 0127 0128 void Validator::setConditionType(const Dice::ConditionType& conditionType) 0129 { 0130 m_conditionType= conditionType; 0131 }