Colobot
CBotVar.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/CBotDefines.h"
23 #include "CBot/CBotTypResult.h"
24 #include "CBot/CBotEnums.h"
25 #include "CBot/CBotUtils.h"
26 
27 #include <string>
28 
29 namespace CBot
30 {
31 
32 class CBotVarClass;
33 class CBotInstr;
34 class CBotClass;
35 class CBotToken;
36 
42 class CBotVar : public CBotLinkedList<CBotVar>
43 {
44 public:
46 
47 
51  CBotVar();
52 
56  CBotVar(const CBotToken& name);
57 
61  virtual ~CBotVar();
62 
68  static CBotVar* Create(const std::string& name, CBotTypResult type);
69 
81  static CBotVar* Create(const std::string& name, CBotClass* pClass);
82 
94  static CBotVar* Create(const CBotToken& name, CBotType type);
95 
101  static CBotVar* Create(const CBotToken& name, CBotTypResult type);
102 
116  static CBotVar* Create(const std::string& name, CBotType type, CBotClass* pClass);
117 
125  static CBotVar* Create(CBotVar* pVar);
126 
131  static void Destroy(CBotVar* var);
132 
134 
136 
145  virtual void SetIdent(long UniqId);
146 
149 
150 
155  void SetUserPtr(void* pUser);
156 
161  void* GetUserPtr();
162 
164 
167 
168 
173  std::string GetName();
174 
179  void SetName(const std::string& name);
180 
186  CBotToken* GetToken();
187 
191  enum class GetTypeMode
192  {
193  NORMAL = 0,
194  CLASS_AS_POINTER = 1,
195  CLASS_AS_INTRINSIC = 2,
196  };
197 
202  CBotType GetType(GetTypeMode mode = GetTypeMode::NORMAL);
203 
208  CBotTypResult GetTypResult(GetTypeMode mode = GetTypeMode::NORMAL);
209 
214  void SetType(CBotTypResult& type);
215 
221  virtual void SetClass(CBotClass* pClass);
222 
228  virtual CBotClass* GetClass();
229 
231 
234 
235 
241  enum class InitType : int
242  {
243  UNDEF = 0,
244  DEF = 1,
245  IS_POINTER = 2,
246  IS_NAN = 999
247  };
248 
253  void SetInit(InitType initType);
254 
258  InitType GetInit() const;
259 
264  bool IsUndefined() const { return GetInit() == InitType::UNDEF; }
265 
270  bool IsDefined() const { return GetInit() == InitType::DEF; }
271 
276  bool IsNAN() const { return GetInit() == InitType::IS_NAN; }
277 
279 
282 
283 
291  void SetStatic(bool bStatic);
292 
300  bool IsStatic();
301 
306  enum class ProtectionLevel
307  {
308  Public = 0,
309  ReadOnly = 1,
310  Protected = 2,
311  Private = 3
312  };
313 
321  void SetPrivate(ProtectionLevel mPrivate);
322 
331 
337 
346  bool IsElemOfClass(const std::string& name);
347 
349 
351 
359  virtual void ConstructorSet();
360 
366 
373  virtual void Update(void* pUser);
374 
380  void SetUniqNum(long n);
381 
388  long GetUniqNum();
389 
395  static long NextUniqNum();
396 
399 
400 
406  virtual CBotVar* GetItem(const std::string& name);
407 
414  virtual CBotVar* GetItemRef(int nIdent);
415 
425  virtual CBotVar* GetItem(int index, bool grow = false);
426 
431  virtual CBotVar* GetItemList();
432 
434 
436 
446 
447  operator int();
448  operator float();
449  operator std::string();
450  void operator=(const CBotVar& var);
451  void operator=(int x);
452  void operator=(float x);
453  void operator=(const std::string &x);
454 
459  void SetVal(CBotVar* var);
460 
466  virtual void Copy(CBotVar* pSrc, bool bName = true);
467 
476  virtual void SetValInt(int val, const std::string& name = "");
477 
482  virtual void SetValFloat(float val);
483 
488  virtual void SetValString(const std::string& val);
489 
494  virtual int GetValInt();
495 
500  virtual float GetValFloat();
501 
511  virtual std::string GetValString();
512 
517  virtual void SetPointer(CBotVar* p);
518 
523  virtual CBotVarClass* GetPointer();
524 
526 
528 
536 
538  virtual void Add(CBotVar* left, CBotVar* right);
540  virtual void Sub(CBotVar* left, CBotVar* right);
542  virtual void Mul(CBotVar* left, CBotVar* right);
544  virtual CBotError Div(CBotVar* left, CBotVar* right);
546  virtual CBotError Modulo(CBotVar* left, CBotVar* right);
548  virtual void Power(CBotVar* left, CBotVar* right);
549 
551 
554 
555 
557  virtual bool Lo(CBotVar* left, CBotVar* right);
559  virtual bool Hi(CBotVar* left, CBotVar* right);
561  virtual bool Ls(CBotVar* left, CBotVar* right);
563  virtual bool Hs(CBotVar* left, CBotVar* right);
565  virtual bool Eq(CBotVar* left, CBotVar* right);
567  virtual bool Ne(CBotVar* left, CBotVar* right);
568 
570 
572 
580  virtual void And(CBotVar* left, CBotVar* right);
583  virtual void Or(CBotVar* left, CBotVar* right);
585  virtual void XOr(CBotVar* left, CBotVar* right);
587  virtual void Not();
588 
590 
593 
594 
596  virtual void ASR(CBotVar* left, CBotVar* right);
598 
601  virtual void SR(CBotVar* left, CBotVar* right);
603  virtual void SL(CBotVar* left, CBotVar* right);
604 
606 
609 
610 
612  virtual void Neg();
614  virtual void Inc();
616  virtual void Dec();
617 
619 
622 
623 
629  virtual bool Save0State(FILE* pf);
630 
639  virtual bool Save1State(FILE* pf);
640 
647  static bool RestoreState(FILE* pf, CBotVar* &pVar);
648 
650 
651 protected:
661 
665  void* m_pUserPtr;
667  bool m_bStatic;
675 
679  long m_ident;
680 
682  static long m_identcpt;
683 
684  friend class CBotStack;
685  friend class CBotCStack;
686  friend class CBotInstrCall;
687  friend class CBotProgram;
688  friend class CBotClass;
689  friend class CBotVarClass;
690  friend class CBotVarPointer;
691  friend class CBotVarArray;
692 };
693 
694 } // namespace CBot
virtual void SetValFloat(float val)
Set value as float.
Definition: CBotVar.cpp:558
virtual CBotVarClass * GetPointer()
Get value for pointer types.
Definition: CBotVar.cpp:529
Class for one CBot instruction.
Definition: CBotInstr.h:86
InitType
Variable initialization status.
Definition: CBotVar.h:241
virtual void Update(void *pUser)
Call the class update function.
Definition: CBotVar.cpp:136
virtual void SL(CBotVar *left, CBotVar *right)
left << right
Definition: CBotVar.cpp:674
static long m_identcpt
TODO: ?
Definition: CBotVar.h:682
virtual CBotVar * GetItem(const std::string &name)
Returns class member by name.
Definition: CBotVar.cpp:396
virtual bool Save1State(FILE *pf)
Save variable data.
Definition: CBotVar.cpp:126
virtual void ASR(CBotVar *left, CBotVar *right)
left >> right
Definition: CBotVar.cpp:662
InitType m_binit
Initialization status.
Definition: CBotVar.h:657
virtual void Mul(CBotVar *left, CBotVar *right)
Multiplication.
Definition: CBotVar.cpp:564
Class that manages a CBot program. This is the main entry point into the CBot engine.
Definition: CBotProgram.h:83
void SetUniqNum(long n)
Set unique identifier of this variable Note: For classes, this is unique within the class only - see ...
Definition: CBotVar.cpp:99
CBotToken * m_token
The corresponding token, defines the variable name.
Definition: CBotVar.h:653
The execution stack.
Definition: CBotStack.h:44
virtual void Inc()
++this
Definition: CBotVar.cpp:692
CBotVar()
Constructor. Do not call directly, use CBotVar::Create()
Definition: CBotVar.cpp:51
GetTypeMode
Mode for GetType() and GetTypResult()
Definition: CBotVar.h:191
virtual void ConstructorSet()
Called after constructor has been called.
Definition: CBotVar.cpp:76
virtual bool Hi(CBotVar *left, CBotVar *right)
left > right
Definition: CBotVar.cpp:609
virtual int GetValInt()
Get value as integer.
Definition: CBotVar.cpp:538
virtual std::string GetValString()
Get value as string.
Definition: CBotVar.cpp:725
virtual void SetValInt(int val, const std::string &name="")
Set value as an integer.
Definition: CBotVar.cpp:552
virtual void SetValString(const std::string &val)
Set value as string.
Definition: CBotVar.cpp:719
virtual CBotClass * GetClass()
Return class this variable is instance of.
Definition: CBotVar.cpp:738
void SetStatic(bool bStatic)
Marks the variable as "static".
Definition: CBotVar.cpp:493
bool IsDefined() const
Checks if the variable is currently "defined".
Definition: CBotVar.h:270
virtual void Add(CBotVar *left, CBotVar *right)
Addition.
Definition: CBotVar.cpp:590
A CBot class definition.
Definition: CBotClass.h:105
virtual void Sub(CBotVar *left, CBotVar *right)
Subtraction.
Definition: CBotVar.cpp:596
virtual CBotError Modulo(CBotVar *left, CBotVar *right)
Modulo (remainder of division)
Definition: CBotVar.cpp:583
virtual bool Ls(CBotVar *left, CBotVar *right)
left <= right
Definition: CBotVar.cpp:616
virtual bool Hs(CBotVar *left, CBotVar *right)
left >= right
Definition: CBotVar.cpp:623
virtual void SR(CBotVar *left, CBotVar *right)
left >>> right
Definition: CBotVar.cpp:668
void * m_pUserPtr
User pointer if specified.
Definition: CBotVar.h:665
static CBotVar * Create(const std::string &name, CBotTypResult type)
Creates a new variable from a type described by CBotTypResult.
Definition: CBotVar.cpp:209
A CBot variable.
Definition: CBotVar.h:42
Some enum values used across the CBot engine.
void SetVal(CBotVar *var)
Set the value.
Definition: CBotVar.cpp:457
virtual void Or(CBotVar *left, CBotVar *right)
left || right or left | right
Definition: CBotVar.cpp:650
virtual CBotVar * GetItemList()
Return all elements of this variable as a linked list. Works for both classes and arrays...
Definition: CBotVar.cpp:410
void * GetUserPtr()
Returns the custom pointer associated with this variable.
Definition: CBotVar.cpp:120
the variable value is currently not defined
ProtectionLevel
Class member protection level (public/protected/private)
Definition: CBotVar.h:306
virtual void XOr(CBotVar *left, CBotVar *right)
left ^ right (also for boolean!)
Definition: CBotVar.cpp:656
The CBotCStack class Management of the stack of compilation.
Definition: CBotCStack.h:35
virtual float GetValFloat()
Get value as float.
Definition: CBotVar.cpp:545
A call to a function - func()
Definition: CBotInstrCall.h:32
CBotError
This enum contains possible CBot error values. Values in range 5000-5999 are compile errors...
Definition: CBotEnums.h:190
long m_ident
Identifier.
Definition: CBotVar.h:679
CBotVar * GetStaticVar()
If this is a static class variable, return the static var from the class.
Definition: CBotVar.cpp:447
CBotVar subclass for managing arrays (CBotTypArrayPointer)
Definition: CBotVarArray.h:32
CBotInstr * m_InitExpr
Expression describing initial value.
Definition: CBotVar.h:671
virtual bool Save0State(FILE *pf)
Save common variable header (name, type, etc.)
Definition: CBotStack.cpp:758
virtual ~CBotVar()
Destructor. Do not call directly, use CBotVar::Destroy()
Definition: CBotVar.cpp:70
CBotTypResult m_type
Type of value.
Definition: CBotVar.h:655
bool IsElemOfClass(const std::string &name)
Check if a variable belongs to a class with a given name.
Definition: CBotVar.cpp:424
bool IsUndefined() const
Checks if the variable is currently "undefined".
Definition: CBotVar.h:264
virtual void Dec()
–this
Definition: CBotVar.cpp:698
virtual void Power(CBotVar *left, CBotVar *right)
Power.
Definition: CBotVar.cpp:570
ProtectionLevel GetPrivate()
Get variable protection level.
Definition: CBotVar.cpp:517
void SetName(const std::string &name)
SetName Changes the name of the variable.
Definition: CBotVar.cpp:384
bool IsNAN() const
Checks if the variable is currently NAN.
Definition: CBotVar.h:276
ProtectionLevel m_mPrivate
Element protection level - public, protected or private (for classes)
Definition: CBotVar.h:669
static void Destroy(CBotVar *var)
Destroy a variable.
Definition: CBotVar.cpp:305
virtual void SetPointer(CBotVar *p)
Set value for pointer types.
Definition: CBotVar.cpp:523
virtual void Copy(CBotVar *pSrc, bool bName=true)
Copy from another variable.
Definition: CBotVar.cpp:704
CBotType
Defines known types. This types are modeled on Java types.
Definition: CBotEnums.h:35
virtual void And(CBotVar *left, CBotVar *right)
left && right or left & right
Definition: CBotVar.cpp:644
CBotVar subclass for managing pointers to classes (CBotTypPointer)
Definition: CBotVarPointer.h:30
CBotType GetType(GetTypeMode mode=GetTypeMode::NORMAL)
GetType Returns the base type of the variable (CBotType)
Definition: CBotVar.cpp:324
bool m_bStatic
true if the variable is static (for classes)
Definition: CBotVar.h:667
virtual void Neg()
-this
Definition: CBotVar.cpp:680
long GetUniqNum()
Return unique identifier of this variable Note: For classes, this is unique within the class only - s...
Definition: CBotVar.cpp:114
CBotTypResult GetTypResult(GetTypeMode mode=GetTypeMode::NORMAL)
Returns the complete type of the variable (CBotTypResult)
Definition: CBotVar.cpp:311
std::string GetName()
Returns the name of the variable.
Definition: CBotVar.cpp:378
Definition: CBotUtils.h:99
static bool RestoreState(FILE *pf, CBotVar *&pVar)
Restore variable.
Definition: CBotStack.cpp:768
virtual void SetClass(CBotClass *pClass)
Set class this variable is instance of.
Definition: CBotVar.cpp:732
the variable value is defined
virtual void Not()
!this or ~this
Definition: CBotVar.cpp:686
static long NextUniqNum()
Generate next unique identifier.
Definition: CBotVar.cpp:107
CBotVarClass * m_pMyThis
Corresponding this element (TODO: ?)
Definition: CBotVar.h:659
bool IsStatic()
Checks if the variable is static.
Definition: CBotVar.cpp:505
Class to define the complete type of a variable.
Definition: CBotTypResult.h:47
virtual void SetIdent(long UniqId)
Associates an unique identifier to class instance.
Definition: CBotVar.cpp:91
virtual CBotError Div(CBotVar *left, CBotVar *right)
Division.
Definition: CBotVar.cpp:576
void SetType(CBotTypResult &type)
Change type of this variable.
Definition: CBotVar.cpp:334
CBot engine.
Definition: CBotCallMethode.cpp:28
the variable value is NAN
CBotToken * GetToken()
Returns the CBotToken this variable is associated with.
Definition: CBotVar.cpp:390
InitType GetInit() const
Returns the current init state of the variable.
Definition: CBotVar.cpp:340
void SetUserPtr(void *pUser)
Set a custom pointer associated with this variable.
Definition: CBotVar.cpp:82
virtual bool Ne(CBotVar *left, CBotVar *right)
left != right
Definition: CBotVar.cpp:637
void SetInit(InitType initType)
Changes variable init status.
Definition: CBotVar.cpp:348
bool IsPrivate(ProtectionLevel level=ProtectionLevel::Protected)
Checks if the variable is accessible at the given protection level.
Definition: CBotVar.cpp:511
virtual bool Eq(CBotVar *left, CBotVar *right)
left == right
Definition: CBotVar.cpp:630
virtual CBotVar * GetItemRef(int nIdent)
Returns class member by unique ID.
Definition: CBotVar.cpp:403
CBotVar subclass for managing classes (CBotTypClass, CBotTypIntrinsic)
Definition: CBotVarClass.h:34
virtual bool Lo(CBotVar *left, CBotVar *right)
left < right
Definition: CBotVar.cpp:602
Class representing one token of a program.
Definition: CBotToken.h:80
void SetPrivate(ProtectionLevel mPrivate)
Sets variable protection level.
Definition: CBotVar.cpp:499
CBotInstr * m_LimExpr
Expression describing array limit.
Definition: CBotVar.h:673