Colobot
CBotVarValue.h
1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2016, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but 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 this program. If not, see http://gnu.org/licenses
18  */
19 
20 #pragma once
21 
22 #include "CBot/CBotVar/CBotVar.h"
23 
24 #include "CBot/CBotEnums.h"
25 #include "CBot/CBotToken.h"
26 
27 #include <sstream>
28 #include <cmath>
29 
30 
31 namespace CBot
32 {
33 
37 template <typename T, CBotType type>
38 class CBotVarValue : public CBotVar
39 {
40 public:
44  CBotVarValue(const CBotToken& name) : CBotVar(name)
45  {
46  m_type = type;
47  }
48 
49  void Copy(CBotVar* pSrc, bool bName = true) override
50  {
51  CBotVar::Copy(pSrc, bName);
52 
53  CBotVarValue* p = static_cast<CBotVarValue*>(pSrc);
54  m_val = p->m_val;
55  }
56 
57 
58  void SetValString(const std::string& val) override
59  {
60  std::istringstream s(val);
61  s >> m_val;
63  }
64 
65  std::string GetValString() override
66  {
68  return LoadString(TX_UNDEF);
70  return LoadString(TX_NAN);
71 
72  std::ostringstream s;
73  s << m_val;
74  return s.str();
75  }
76 
77 protected:
79  T m_val;
80 };
81 
85 template <typename T, CBotType type>
86 class CBotVarNumberBase : public CBotVarValue<T, type>
87 {
88 public:
89  CBotVarNumberBase(const CBotToken &name) : CBotVarValue<T, type>(name) {}
90 
91  void SetValInt(int val, const std::string &s = "") override
92  {
93  this->m_val = static_cast<T>(val);
95  }
96 
97  void SetValFloat(float val) override
98  {
99  this->m_val = static_cast<T>(val);
101  }
102 
103  int GetValInt() override
104  {
105  return static_cast<int>(this->m_val);
106  }
107 
108  float GetValFloat() override
109  {
110  return static_cast<float>(this->m_val);
111  }
112 
113 
114  bool Eq(CBotVar* left, CBotVar* right) override
115  {
116  return left->GetValFloat() == right->GetValFloat();
117  }
118  bool Ne(CBotVar* left, CBotVar* right) override
119  {
120  return left->GetValFloat() != right->GetValFloat();
121  }
122 };
123 
127 template <typename T, CBotType type>
128 class CBotVarNumber : public CBotVarNumberBase<T, type>
129 {
130 public:
131  CBotVarNumber(const CBotToken &name) : CBotVarNumberBase<T, type>(name) {}
132 
133  void Mul(CBotVar* left, CBotVar* right) override
134  {
135  this->SetValFloat(left->GetValFloat() * right->GetValFloat());
136  }
137  void Power(CBotVar* left, CBotVar* right) override
138  {
139  this->SetValFloat(pow(left->GetValFloat(), right->GetValFloat()));
140  }
141  CBotError Div(CBotVar* left, CBotVar* right) override
142  {
143  float r = right->GetValFloat();
144  if (r == 0) return CBotErrZeroDiv;
145  this->SetValFloat(left->GetValFloat() / r);
146  return CBotNoErr;
147  }
148  CBotError Modulo(CBotVar* left, CBotVar* right) override
149  {
150  float r = right->GetValFloat();
151  if (r == 0) return CBotErrZeroDiv;
152  this->SetValFloat(fmod(left->GetValFloat(), r));
153  return CBotNoErr;
154  }
155  void Add(CBotVar* left, CBotVar* right) override
156  {
157  this->SetValFloat(left->GetValFloat() + right->GetValFloat());
158  }
159  void Sub(CBotVar* left, CBotVar* right) override
160  {
161  this->SetValFloat(left->GetValFloat() - right->GetValFloat());
162  }
163 
164  void Neg() override
165  {
166  this->m_val = - this->m_val;
167  }
168  void Inc() override
169  {
170  this->m_val++;
171  }
172  void Dec() override
173  {
174  this->m_val--;
175  }
176 
177  bool Lo(CBotVar* left, CBotVar* right) override
178  {
179  return left->GetValFloat() < right->GetValFloat();
180  }
181  bool Hi(CBotVar* left, CBotVar* right) override
182  {
183  return left->GetValFloat() > right->GetValFloat();
184  }
185  bool Ls(CBotVar* left, CBotVar* right) override
186  {
187  return left->GetValFloat() <= right->GetValFloat();
188  }
189  bool Hs(CBotVar* left, CBotVar* right) override
190  {
191  return left->GetValFloat() >= right->GetValFloat();
192  }
193 };
194 
195 }
196 
virtual void SetValFloat(float val)
Set value as float.
Definition: CBotVar.cpp:558
int GetValInt() override
Get value as integer.
Definition: CBotVarValue.h:103
CBotError Modulo(CBotVar *left, CBotVar *right) override
Modulo (remainder of division)
Definition: CBotVarValue.h:148
InitType m_binit
Initialization status.
Definition: CBotVar.h:657
void SetValInt(int val, const std::string &s="") override
Set value as an integer.
Definition: CBotVarValue.h:91
A number variable (int, float)
Definition: CBotVarValue.h:128
bool Eq(CBotVar *left, CBotVar *right) override
left == right
Definition: CBotVarValue.h:114
CBotVarValue(const CBotToken &name)
Constructor. Do not call directly, use CBotVar::Create()
Definition: CBotVarValue.h:44
const std::string & LoadString(TokenId id)
Maps given ID to its string equivalent.
Definition: CBotToken.cpp:124
bool Ls(CBotVar *left, CBotVar *right) override
left <= right
Definition: CBotVarValue.h:185
bool Hi(CBotVar *left, CBotVar *right) override
left > right
Definition: CBotVarValue.h:181
void Inc() override
++this
Definition: CBotVarValue.h:168
A CBot variable.
Definition: CBotVar.h:42
Some enum values used across the CBot engine.
A number based variable (bool, int, float)
Definition: CBotVarValue.h:86
bool Hs(CBotVar *left, CBotVar *right) override
left >= right
Definition: CBotVarValue.h:189
the variable value is currently not defined
virtual float GetValFloat()
Get value as float.
Definition: CBotVar.cpp:545
CBotError
This enum contains possible CBot error values. Values in range 5000-5999 are compile errors...
Definition: CBotEnums.h:190
A variable holding a simple value (bool, int, float, string)
Definition: CBotVarValue.h:38
CBotError Div(CBotVar *left, CBotVar *right) override
Division.
Definition: CBotVarValue.h:141
CBotTypResult m_type
Type of value.
Definition: CBotVar.h:655
T m_val
The value.
Definition: CBotVarValue.h:79
void Sub(CBotVar *left, CBotVar *right) override
Subtraction.
Definition: CBotVarValue.h:159
virtual void Copy(CBotVar *pSrc, bool bName=true)
Copy from another variable.
Definition: CBotVar.cpp:704
void SetValString(const std::string &val) override
Set value as string.
Definition: CBotVarValue.h:58
division by zero
Definition: CBotEnums.h:241
bool Lo(CBotVar *left, CBotVar *right) override
left < right
Definition: CBotVarValue.h:177
void Dec() override
–this
Definition: CBotVarValue.h:172
void Copy(CBotVar *pSrc, bool bName=true) override
Copy from another variable.
Definition: CBotVarValue.h:49
float GetValFloat() override
Get value as float.
Definition: CBotVarValue.h:108
the variable value is defined
void Add(CBotVar *left, CBotVar *right) override
Addition.
Definition: CBotVarValue.h:155
CBot engine.
Definition: CBotCallMethode.cpp:28
the variable value is NAN
std::string GetValString() override
Get value as string.
Definition: CBotVarValue.h:65
void SetValFloat(float val) override
Set value as float.
Definition: CBotVarValue.h:97
Class representing one token of a program.
Definition: CBotToken.h:80
void Neg() override
-this
Definition: CBotVarValue.h:164
void Mul(CBotVar *left, CBotVar *right) override
Multiplication.
Definition: CBotVarValue.h:133
bool Ne(CBotVar *left, CBotVar *right) override
left != right
Definition: CBotVarValue.h:118
void Power(CBotVar *left, CBotVar *right) override
Power.
Definition: CBotVarValue.h:137