colobot/src/CBot/CBotVar/CBotVarValue.h

296 lines
7.2 KiB
C
Raw Normal View History

2016-07-09 18:39:18 +00:00
/*
* This file is part of the Colobot: Gold Edition source code
2021-09-11 13:52:34 +00:00
* Copyright (C) 2001-2021, Daniel Roux, EPSITEC SA & TerranovaTeam
2016-07-09 18:39:18 +00:00
* http://epsitec.ch; http://colobot.info; http://github.com/colobot
*
* This program 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.
*
* This program 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 http://gnu.org/licenses
*/
2016-05-28 20:59:34 +00:00
#pragma once
#include "CBot/CBotVar/CBotVar.h"
#include "CBot/CBotEnums.h"
#include "CBot/CBotToken.h"
#include <sstream>
#include <cmath>
namespace CBot
{
/**
* \brief A variable holding a simple value (bool, int, float, string)
*/
template <typename T, CBotType type>
class CBotVarValue : public CBotVar
{
public:
/**
* \brief Constructor. Do not call directly, use CBotVar::Create()
*/
CBotVarValue(const CBotToken& name) : CBotVar(name)
{
m_type = type;
}
void Copy(CBotVar* pSrc, bool bName = true) override
{
CBotVar::Copy(pSrc, bName);
CBotVarValue* p = static_cast<CBotVarValue*>(pSrc);
m_val = p->m_val;
}
void SetValString(const std::string& val) override
{
std::istringstream s(val);
s >> m_val;
m_binit = CBotVar::InitType::DEF;
}
2022-08-14 21:47:47 +00:00
std::string GetValString() const override
2016-05-28 20:59:34 +00:00
{
if (m_binit == CBotVar::InitType::UNDEF)
return LoadString(TX_UNDEF);
std::ostringstream s;
2022-08-13 15:18:14 +00:00
s << std::boolalpha << m_val;
2016-05-28 20:59:34 +00:00
return s.str();
}
protected:
virtual void SetValue(T val)
{
this->m_val = val;
this->m_binit = CBotVar::InitType::DEF;
}
2016-05-28 20:59:34 +00:00
protected:
//! The value
T m_val;
};
/**
* \brief A number based variable (bool, int, float)
*/
template <typename T, CBotType type>
class CBotVarNumberBase : public CBotVarValue<T, type>
{
public:
CBotVarNumberBase(const CBotToken &name) : CBotVarValue<T, type>(name)
{
this->m_val = static_cast<T>(0);
}
2016-05-28 20:59:34 +00:00
void SetValByte(signed char val) override
{
this->SetValue(static_cast<T>(val));
}
void SetValShort(short val) override
{
this->SetValue(static_cast<T>(val));
}
void SetValChar(uint32_t val) override
{
this->SetValue(static_cast<T>(val));
}
2016-05-28 20:59:34 +00:00
void SetValInt(int val, const std::string &s = "") override
{
this->SetValue(static_cast<T>(val));
2016-05-28 20:59:34 +00:00
}
void SetValLong(long val) override
{
this->SetValue(static_cast<T>(val));
}
2016-05-28 20:59:34 +00:00
void SetValFloat(float val) override
{
this->SetValue(static_cast<T>(val));
2016-05-28 20:59:34 +00:00
}
void SetValDouble(double val) override
{
this->SetValue(static_cast<T>(val));
}
2022-08-14 21:47:47 +00:00
signed char GetValByte() const override
{
return static_cast<signed char>(this->m_val);
}
2022-08-14 21:47:47 +00:00
short GetValShort() const override
{
return static_cast<short>(this->m_val);
}
2022-08-14 21:47:47 +00:00
uint32_t GetValChar() const override
{
return static_cast<uint32_t>(this->m_val);
}
2022-08-14 21:47:47 +00:00
int GetValInt() const override
2016-05-28 20:59:34 +00:00
{
return static_cast<int>(this->m_val);
}
2022-08-14 21:47:47 +00:00
long GetValLong() const override
{
return static_cast<long>(this->m_val);
}
2022-08-14 21:47:47 +00:00
float GetValFloat() const override
2016-05-28 20:59:34 +00:00
{
return static_cast<float>(this->m_val);
}
2022-08-14 21:47:47 +00:00
double GetValDouble() const override
{
return static_cast<double>(this->m_val);
}
2016-05-28 20:59:34 +00:00
bool Eq(CBotVar* left, CBotVar* right) override
{
return static_cast<T>(*left) == static_cast<T>(*right);
2016-05-28 20:59:34 +00:00
}
bool Ne(CBotVar* left, CBotVar* right) override
{
return static_cast<T>(*left) != static_cast<T>(*right);
2016-05-28 20:59:34 +00:00
}
};
/**
* \brief A number variable (int, float)
*/
template <typename T, CBotType type>
class CBotVarNumber : public CBotVarNumberBase<T, type>
{
public:
CBotVarNumber(const CBotToken &name) : CBotVarNumberBase<T, type>(name) {}
void Mul(CBotVar* left, CBotVar* right) override
{
this->SetValue(static_cast<T>(*left) * static_cast<T>(*right));
2016-05-28 20:59:34 +00:00
}
void Power(CBotVar* left, CBotVar* right) override
{
this->SetValue(pow(static_cast<T>(*left), static_cast<T>(*right)));
2016-05-28 20:59:34 +00:00
}
CBotError Div(CBotVar* left, CBotVar* right) override
{
T r = static_cast<T>(*right);
if ( r == static_cast<T>(0) ) return CBotErrZeroDiv;
this->SetValue(static_cast<T>(*left) / r);
2016-05-28 20:59:34 +00:00
return CBotNoErr;
}
CBotError Modulo(CBotVar* left, CBotVar* right) override
{
T r = static_cast<T>(*right);
if ( r == static_cast<T>(0) ) return CBotErrZeroDiv;
this->SetValue(fmod(static_cast<T>(*left), r));
2016-05-28 20:59:34 +00:00
return CBotNoErr;
}
void Add(CBotVar* left, CBotVar* right) override
{
this->SetValue(static_cast<T>(*left) + static_cast<T>(*right));
2016-05-28 20:59:34 +00:00
}
void Sub(CBotVar* left, CBotVar* right) override
{
this->SetValue(static_cast<T>(*left) - static_cast<T>(*right));
2016-05-28 20:59:34 +00:00
}
void Neg() override
{
this->m_val = - this->m_val;
}
void Inc() override
{
this->m_val++;
}
void Dec() override
{
this->m_val--;
}
bool Lo(CBotVar* left, CBotVar* right) override
{
return static_cast<T>(*left) < static_cast<T>(*right);
2016-05-28 20:59:34 +00:00
}
bool Hi(CBotVar* left, CBotVar* right) override
{
return static_cast<T>(*left) > static_cast<T>(*right);
2016-05-28 20:59:34 +00:00
}
bool Ls(CBotVar* left, CBotVar* right) override
{
return static_cast<T>(*left) <= static_cast<T>(*right);
2016-05-28 20:59:34 +00:00
}
bool Hs(CBotVar* left, CBotVar* right) override
{
return static_cast<T>(*left) >= static_cast<T>(*right);
2016-05-28 20:59:34 +00:00
}
};
/**
* \brief An integer variable (byte, short, char, int, long)
*/
template <typename T, CBotType type>
class CBotVarInteger : public CBotVarNumber<T, type>
{
public:
CBotVarInteger(const CBotToken &name) : CBotVarNumber<T, type>(name) {}
CBotError Modulo(CBotVar* left, CBotVar* right) override
{
T r = static_cast<T>(*right);
if ( r == static_cast<T>(0) ) return CBotErrZeroDiv;
this->SetValue(static_cast<T>(*left) % r);
return CBotNoErr;
}
void XOr(CBotVar* left, CBotVar* right) override
{
this->SetValue(static_cast<T>(*left) ^ static_cast<T>(*right));
}
void And(CBotVar* left, CBotVar* right) override
{
this->SetValue(static_cast<T>(*left) & static_cast<T>(*right));
}
void Or(CBotVar* left, CBotVar* right) override
{
this->SetValue(static_cast<T>(*left) | static_cast<T>(*right));
}
void SL(CBotVar* left, CBotVar* right) override
{
this->SetValue(static_cast<T>(*left) << right->GetValInt());
}
void ASR(CBotVar* left, CBotVar* right) override
{
this->SetValue(static_cast<T>(*left) >> right->GetValInt());
}
void Not() override
{
this->m_val = ~(this->m_val);
}
};
2016-05-28 20:59:34 +00:00
} // namespace CBot