1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
/* ConditionEntry.cpp
Copyright (c) 2024-2025 by Peter van der Meer
Endless Sky is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
Endless Sky is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "ConditionEntry.h"
using namespace std;
ConditionEntry::ConditionEntry(const string &name)
: name(name), value(0), providingEntry(nullptr)
{
}
const string &ConditionEntry::Name() const
{
return name;
}
const string ConditionEntry::NameWithoutPrefix() const
{
// If we have a provider, and that provider has a main-entry, then we have prefix.
if(providingEntry)
return name.substr(providingEntry->name.length());
// If we have no prefix, then return the name without prefix.
return name;
}
ConditionEntry::operator int64_t() const
{
// Prefixed provider; use the other function to get the value.
if(providingEntry)
return providingEntry->getFunction(*this);
// Named provider; use the local getFunction to get the value.
if(getFunction)
return getFunction(*this);
// This is not a provider, just return the value.
return value;
}
ConditionEntry &ConditionEntry::operator=(int64_t val)
{
// Set through prefixed provider, notify if the function is read/write.
if(providingEntry)
{
if(providingEntry->setFunction)
{
providingEntry->setFunction(*this, val);
NotifyUpdate(val);
}
}
// Set through named provider, notify if the function is read/write.
else if(getFunction)
{
if(setFunction)
{
setFunction(*this, val);
NotifyUpdate(val);
}
}
// Set value directly.
else
{
value = val;
NotifyUpdate(val);
}
return *this;
}
ConditionEntry &ConditionEntry::operator++()
{
// Reuse assignment and int64_t conversion operators.
return (*this) = (*this) + 1;
}
ConditionEntry &ConditionEntry::operator--()
{
// Reuse assignment and int64_t conversion operators.
return (*this) = (*this) - 1;
}
ConditionEntry &ConditionEntry::operator+=(int64_t val)
{
// Reuse assignment and int64_t conversion operators.
return (*this) = (*this) + val;
}
ConditionEntry &ConditionEntry::operator-=(int64_t val)
{
// Reuse assignment and int64_t conversion operators.
return (*this) = (*this) - val;
}
void ConditionEntry::ProvidePrefixed(function<int64_t(const ConditionEntry &)> getFunction)
{
this->getFunction = std::move(getFunction);
this->providingEntry = this;
}
void ConditionEntry::ProvidePrefixed(function<int64_t(const ConditionEntry &)> getFunction,
function<void(ConditionEntry &, int64_t)> setFunction)
{
ProvidePrefixed(getFunction);
this->setFunction = std::move(setFunction);
}
void ConditionEntry::ProvideNamed(function<int64_t(const ConditionEntry &)> getFunction)
{
this->getFunction = std::move(getFunction);
this->providingEntry = nullptr;
}
void ConditionEntry::ProvideNamed(function<int64_t(const ConditionEntry &)> getFunction,
function<void(ConditionEntry &, int64_t)> setFunction)
{
ProvideNamed(getFunction);
this->setFunction = std::move(setFunction);
}
void ConditionEntry::NotifyUpdate(uint64_t value)
{
// Subscribing is not implemented yet.
return;
}
|