| •1 |
/* |
| 2 |
RailControl - Model Railway Control Software |
| 3 |
|
| 4 |
Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org |
| 5 |
|
| 6 |
RailControl is free software; you can redistribute it and/or modify it |
| 7 |
under the terms of the GNU General Public License as published by the |
| 8 |
Free Software Foundation; either version 3, or (at your option) any |
| 9 |
later version. |
| 10 |
|
| 11 |
RailControl is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 |
See the GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with RailControl; see the file LICENCE. If not see |
| 18 |
<http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "DataModel/Counter.h" |
| 22 |
#include "DataModel/LockableItem.h" |
| 23 |
#include "Manager.h" |
| 24 |
#include "Utils/Utils.h" |
| 25 |
|
| 26 |
using std::map; |
| 27 |
using std::string; |
| 28 |
using std::to_string; |
| 29 |
|
| 30 |
namespace DataModel |
| 31 |
{ |
| 32 |
std::string Counter::Serialize() const |
| 33 |
{ |
| 34 |
string str; |
| 35 |
str += "objectType=Counter;"; |
| 36 |
str += LayoutItem::Serialize(); |
| 37 |
str += ";counter="; |
| 38 |
str += to_string(counter); |
| 39 |
str += ";max="; |
| 40 |
str += to_string(max); |
| 41 |
str += ";min="; |
| 42 |
str += to_string(min); |
| 43 |
return str; |
| 44 |
} |
| 45 |
|
| 46 |
void Counter::Deserialize(const string& serialized) |
| 47 |
{ |
| 48 |
map<string,string> arguments; |
| 49 |
ParseArguments(serialized, arguments); |
| 50 |
string objectType = Utils::Utils::GetStringMapEntry(arguments, "objectType"); |
| 51 |
if (objectType.compare("Counter") != 0) |
| 52 |
{ |
| 53 |
return; |
| 54 |
} |
| 55 |
LayoutItem::Deserialize(arguments); |
| 56 |
counter = Utils::Utils::GetIntegerMapEntry(arguments, "counter", 0); |
| 57 |
max = Utils::Utils::GetIntegerMapEntry(arguments, "max", 0); |
| 58 |
min = Utils::Utils::GetIntegerMapEntry(arguments, "min", 0); |
| 59 |
} |
| 60 |
|
| 61 |
bool Counter::Check(const CounterType type) |
| 62 |
{ |
| 63 |
switch(type) |
| 64 |
{ |
| 65 |
case CounterTypeIncrement: |
| 66 |
return (counter < max); |
| 67 |
|
| 68 |
case CounterTypeDecrement: |
| 69 |
return (counter > min); |
| 70 |
|
| 71 |
default: |
| 72 |
return false; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
bool Counter::Count(const CounterType type) |
| 77 |
{ |
| 78 |
std::lock_guard<std::mutex> Guard(countMutex); |
| 79 |
switch(type) |
| 80 |
{ |
| 81 |
case CounterTypeIncrement: |
| 82 |
{ |
| 83 |
if (counter >= max) |
| 84 |
{ |
| 85 |
return false; |
| 86 |
} |
| 87 |
++counter; |
| 88 |
return true; |
| 89 |
} |
| 90 |
|
| 91 |
case CounterTypeDecrement: |
| 92 |
{ |
| 93 |
if (counter <= min) |
| 94 |
{ |
| 95 |
return false; |
| 96 |
} |
| 97 |
--counter; |
| 98 |
return true; |
| 99 |
} |
| 100 |
|
| 101 |
default: |
| 102 |
return false; |
| 103 |
} |
| 104 |
} |
| 105 |
} // namespace DataModel |