Loading...
Searching...
No Matches
CBot::CBotClass Class Reference

A CBot class definition. More...

`#include <src/CBot/CBotClass.h>`

## Public Member Functions

| CBotClass (const std::string &name, CBotClass *parent, bool bIntrinsic=false)
| CBotClass Constructor. Once a class is created, it is known around CBot intrinsic mode gives a class that is not managed by pointers.
| ~CBotClass ()
| CBotClass Destructor.
bool | AddFunction (const std::string &name, bool rExec(CBotVar *pThis, CBotVar *pVar, CBotVar *pResult, int &Exception, void *user), CBotTypResult rCompile(CBotVar *pThis, CBotVar *&pVar))
| Add a function that can be called from CBot.
bool | SetUpdateFunc (void rUpdate(CBotVar *thisVar, void *user))
| SetUpdateFunc Defines routine to be called to update the elements of the class.
bool | AddItem (std::string name, CBotTypResult type, CBotVar::ProtectionLevel mPrivate=CBotVar::ProtectionLevel::Public)
| AddItem Adds an element to the class.
bool | AddItem (CBotVar *pVar)
| AddItem Adds an item by passing the pointer to an instance of a variable the object is taken as is, so do not destroyed.
const std::string & | GetName ()
| GetName Gives the name of the class.
CBotClass * | GetParent ()
| GetParent Gives the parent class (or nullptr).
bool | IsChildOf (CBotClass *pClass)
| IsChildOf True if a class is derived (Extends) of another.
CBotVar * | GetVar ()
| GetVar Return the list of variables.
CBotVar * | GetItem (const std::string &name)
| GetItem One of the variables according to its name.
CBotVar * | GetItemRef (int nIdent)
| GetItemRef.
bool | CheckVar (const std::string &name)
| Check whether a variable is already defined in a class.
CBotTypResult | CompileMethode (CBotToken *name, CBotVar *pThis, CBotVar **ppParams, CBotCStack *pStack, long &nIdent)
| CompileMethode Compiles a method associated with an instance of class the method can be declared by the user or AddFunction.
bool | ExecuteMethode (long &nIdent, CBotVar *pThis, CBotVar **ppParams, CBotTypResult pResultType, CBotStack *&pStack, CBotToken *pToken)
| ExecuteMethode Executes a method.
void | RestoreMethode (long &nIdent, CBotToken *name, CBotVar *pThis, CBotVar **ppParams, CBotStack *&pStack)
| RestoreMethode Restored the execution stack.
const std::list< CBotFunction * > & | GetFunctions () const
| Get the list of user-defined methods in this class.
bool | CompileDefItem (CBotToken *&p, CBotCStack *pStack, bool bSecond)
| CompileDefItem.
bool | IsIntrinsic ()
| IsIntrinsic.
void | Purge ()
| Purge.
bool | Lock (CBotProgram *prog)
| Request a lock on this class (for "synchronized" keyword).
void | Unlock ()
| Release the lock acquired in Lock() If you call Lock() multiple times for the same program, you have to call Unlock() multiple times too.
bool | CheckCall (CBotProgram *program, CBotDefParam *pParam, CBotToken *&pToken)
| CheckCall Test if a procedure name is already defined somewhere.
void | Update (CBotVar *var, void *user)

## Static Public Member Functions

static CBotClass * | Create (const std::string &name, CBotClass *parent, bool intrinsic=false)
| Create.
static CBotClass * | Find (CBotToken *&pToken)
| Find Trouve une classe d'après son nom.
static CBotClass * | Find (const std::string &name)
| Find.
static CBotClass * | Compile (CBotToken *&p, CBotCStack *pStack)
| Compile Compiles a class declared by the user.
static CBotClass * | Compile1 (CBotToken *&p, CBotCStack *pStack)
| Pre-compile a new class.
static void | DefineClasses (std::list< CBotClass * > pClassList, CBotCStack *pStack)
| DefineClasses Calls CompileDefItem for each class in a list of classes, defining fields and pre-compiling methods.
static void | ClearPublic ()
| Free.
static bool | SaveStaticState (std::ostream &ostr)
| Save all static variables from each public class.
static bool | RestoreStaticState (std::istream &istr)
| Restore all static variables in each public class.
static void | FreeLock (CBotProgram *prog)
| Release all locks in all classes held by this program.

## Detailed Description

A CBot class definition.

# Usage examples

Define class "point":

CBotClass* classPoint = new CBotClass("CPoint", nullptr);
classPoint->AddItem("x", CBotTypResult(CBotTypFloat)); // add ".x"
classPoint->AddItem("y", CBotTypResult(CBotTypFloat)); // add ".y"
// This class can be used in CBot like so:
// point position;
// position.x = 12;
// position.y = -13.6
CBot::CBotClass::CBotClass
CBotClass(const std::string &name, CBotClass *parent, bool bIntrinsic=false)
CBotClass Constructor. Once a class is created, it is known around CBot intrinsic mode gives a class ...
Definition CBotClass.cpp:49
CBot::CBotClass::AddItem
bool AddItem(std::string name, CBotTypResult type, CBotVar::ProtectionLevel mPrivate=CBotVar::ProtectionLevel::Public)
AddItem Adds an element to the class.
Definition CBotClass.cpp:158
CBot::CBotTypResult
Class to define the complete type of a variable.
Definition CBotTypResult.h:48
CBot::CBotTypFloat
@ CBotTypFloat
float
Definition CBotEnums.h:43

Define readonly class "object" with members of type "point" and some methods:

CBotClass* classObject = new CBotClass("object", nullptr);
classObject->AddItem("category", CBotTypResult(CBotTypInt), CBotVar::ProtectionType::ReadOnly);
classObject->AddItem("position", CBotTypResult(CBotTypClass, classPoint), CBotVar::ProtectionType::ReadOnly);
classObject->AddFunction("func", rFunc, cFunc); // TODO: Document function format for class methods (different from standard CBotProgram::AddFunction()!)
// This class can be used in CBot like so:
// object item = radar(Me);
// goto(item.position);
// item.func();
CBot::CBotClass::AddFunction
bool AddFunction(const std::string &name, bool rExec(CBotVar *pThis, CBotVar *pVar, CBotVar *pResult, int &Exception, void *user), CBotTypResult rCompile(CBotVar *pThis, CBotVar *&pVar))
Add a function that can be called from CBot.
Definition CBotClass.cpp:288
CBot::CBotTypClass
@ CBotTypClass
Class instance.
Definition CBotEnums.h:53
CBot::CBotTypInt
@ CBotTypInt
int
Definition CBotEnums.h:41

Define class "robot" derrived from "object":

CBotClass* classRobot = new CBotClass("object", classObject);
classRobot->AddItem("velocity", CBotTypResult(CBotTypClass, classPoint), CBotVar::ProtectionType::ReadOnly);
classRobot->AddFunction("func2", rFunc2, cFunc2);
// This class can be used in CBot like so:
// robot item = something();
// goto(item.position); // can still access base class
// item.func(); // can still call base class
// item.func2(); // but also the derrived class

Create instance of the "robot" class:

CBotVar* var = new CBotVar("variableName", classRobot);
CBot::CBotVar
A CBot variable.
Definition CBotVar.h:44

Access members of the "point" class:

CBotVar* varX = classInstance->GetItem("x");
float x = varX->GetValFloat();
CBotVar* varY = classInstance->GetItem("y");
float y = varX->GetValFloat();
// OR
CBotVar* var = classInstance->GetItemList();
float x = var->GetValFloat();
var->GetNext();
float y = var->GetValFloat();
CBot::CBotLinkedList::GetNext
T * GetNext()
Returns the next variable in the linked list.
Definition CBotUtils.h:110
CBot::CBotVar::GetValFloat
virtual float GetValFloat() const
Get value as float.
Definition CBotVar.cpp:617
CBot::CBotVar::GetItem
virtual CBotVar * GetItem(const std::string &name)
Returns class member by name.
Definition CBotVar.cpp:430
CBot::CBotVar::GetItemList
virtual CBotVar * GetItemList()
Return all elements of this variable as a linked list. Works for both classes and arrays.
Definition CBotVar.cpp:444

## Constructor & Destructor Documentation

## CBotClass()

CBot::CBotClass::CBotClass | ( | const std::string & | name,
|  | CBotClass * | parent,
|  | bool | bIntrinsic = false )

CBotClass Constructor. Once a class is created, it is known around CBot intrinsic mode gives a class that is not managed by pointers.

Parameters

  name |
  parent |
  bIntrinsic |

## ~CBotClass()

CBot::CBotClass::~CBotClass | ( |  | ) |

CBotClass Destructor.

## Member Function Documentation

## Create()

CBotClass * CBot::CBotClass::Create | ( | const std::string & | name,
|  | CBotClass * | parent,
|  | bool | intrinsic = false )

static

Create.

Parameters

  name |
  parent |
  intrinsic |

Returns

## AddFunction()

bool CBot::CBotClass::AddFunction | ( | const std::string & | name,
|  | bool | rExecCBotVar *pThis, CBotVar *pVar, CBotVar *pResult, int &Exception, void *user,
|  | CBotTypResult | rCompileCBotVar *pThis, CBotVar *&pVar )

Add a function that can be called from CBot.

See also
  CBotProgram::AddFunction

## SetUpdateFunc()

bool CBot::CBotClass::SetUpdateFunc | ( | void | rUpdateCBotVar *thisVar, void *user | ) |

SetUpdateFunc Defines routine to be called to update the elements of the class.

Parameters

  rUpdate |

Returns

## AddItem() [1/2]

bool CBot::CBotClass::AddItem | ( | std::string | name,
|  | CBotTypResult | type,
|  | CBotVar::ProtectionLevel | mPrivate = CBotVar::ProtectionLevel::Public )

AddItem Adds an element to the class.

Parameters

  name |
  type |
  mPrivate |

Returns

pVar->SetUniqNum(CBotVar::NextUniqNum());

## AddItem() [2/2]

bool CBot::CBotClass::AddItem | ( | CBotVar * | pVar | ) |

AddItem Adds an item by passing the pointer to an instance of a variable the object is taken as is, so do not destroyed.

Parameters

  pVar |

Returns

## GetName()

const std::string & CBot::CBotClass::GetName | ( |  | ) |

GetName Gives the name of the class.

Returns

## GetParent()

CBotClass * CBot::CBotClass::GetParent | ( |  | ) |

GetParent Gives the parent class (or nullptr).

Returns

## IsChildOf()

bool CBot::CBotClass::IsChildOf | ( | CBotClass * | pClass | ) |

IsChildOf True if a class is derived (Extends) of another.

Parameters

  pClass |

Returns
  true also if the classes are identical

## Find() [1/2]

CBotClass * CBot::CBotClass::Find | ( | CBotToken *& | pToken | ) |

static

Find Trouve une classe d'après son nom.

Parameters

  pToken |

Returns
  A class by it's its name.

## Find() [2/2]

CBotClass * CBot::CBotClass::Find | ( | const std::string & | name | ) |

static

Find.

Parameters

  name |

Returns

## GetVar()

CBotVar * CBot::CBotClass::GetVar | ( |  | ) |

GetVar Return the list of variables.

Returns

## GetItem()

CBotVar * CBot::CBotClass::GetItem | ( | const std::string & | name | ) |

GetItem One of the variables according to its name.

Parameters

  name |

Returns

## GetItemRef()

CBotVar * CBot::CBotClass::GetItemRef | ( | int | nIdent | ) |

GetItemRef.

Parameters

  nIdent |

Returns

## CheckVar()

bool CBot::CBotClass::CheckVar | ( | const std::string & | name | ) |

Check whether a variable is already defined in a class.

Parameters

  name | Name of the variable

Returns
  True if a variable is defined in the class

## CompileMethode()

CBotTypResult CBot::CBotClass::CompileMethode | ( | CBotToken * | name,
|  | CBotVar * | pThis,
|  | CBotVar ** | ppParams,
|  | CBotCStack * | pStack,
|  | long & | nIdent )

CompileMethode Compiles a method associated with an instance of class the method can be declared by the user or AddFunction.

Parameters

  name |
  pThis |
  ppParams |
  pStack |
  nIdent |

Returns

## ExecuteMethode()

bool CBot::CBotClass::ExecuteMethode | ( | long & | nIdent,
|  | CBotVar * | pThis,
|  | CBotVar ** | ppParams,
|  | CBotTypResult | pResultType,
|  | CBotStack *& | pStack,
|  | CBotToken * | pToken )

ExecuteMethode Executes a method.

Parameters

  nIdent |
  name |
  pThis |
  ppParams |
  pResultType |
  pStack |
  pToken |

Returns

## RestoreMethode()

void CBot::CBotClass::RestoreMethode | ( | long & | nIdent,
|  | CBotToken * | name,
|  | CBotVar * | pThis,
|  | CBotVar ** | ppParams,
|  | CBotStack *& | pStack )

RestoreMethode Restored the execution stack.

Parameters

  nIdent |
  name |
  pThis |
  ppParams |
  pStack |

## Compile()

CBotClass * CBot::CBotClass::Compile | ( | CBotToken *& | p,
|  | CBotCStack * | pStack )

static

Compile Compiles a class declared by the user.

Parameters

  p |
  pStack |

Returns

## Compile1()

CBotClass * CBot::CBotClass::Compile1 | ( | CBotToken *& | p,
|  | CBotCStack * | pStack )

static

Pre-compile a new class.

Parameters

  p[in,out] | Pointer to first token of the class, will be updated to point to first token after the class definition
  pStack | Compile stack

This function is used to find the beginning and end of class definition.

If any errors in the code are detected, this function will set the error on compile stack and return nullptr.

Returns
  Precompiled class, or nullptr in case of error

## DefineClasses()

void CBot::CBotClass::DefineClasses | ( | std::list< CBotClass * > | pClassList,
|  | CBotCStack * | pStack )

static

DefineClasses Calls CompileDefItem for each class in a list of classes, defining fields and pre-compiling methods.

Parameters

  pClassList | List of classes
  pStack |

## GetFunctions()

const std::list< CBotFunction * > & CBot::CBotClass::GetFunctions | ( |  | ) | const

Get the list of user-defined methods in this class.

Returns
  List of methods, can be empty.

## CompileDefItem()

bool CBot::CBotClass::CompileDefItem | ( | CBotToken *& | p,
|  | CBotCStack * | pStack,
|  | bool | bSecond )

CompileDefItem.

Parameters

  p |
  pStack |
  bSecond |

Returns

## IsIntrinsic()

bool CBot::CBotClass::IsIntrinsic | ( |  | ) |

IsIntrinsic.

Returns

## Purge()

void CBot::CBotClass::Purge | ( |  | ) |

Purge.

## ClearPublic()

void CBot::CBotClass::ClearPublic | ( |  | ) |

static

Free.

## SaveStaticState()

bool CBot::CBotClass::SaveStaticState | ( | std::ostream & | ostr | ) |

static

Save all static variables from each public class.

Parameters

  ostr | Output stream

Returns
  true on success

## RestoreStaticState()

bool CBot::CBotClass::RestoreStaticState | ( | std::istream & | istr | ) |

static

Restore all static variables in each public class.

Parameters

  istr | Input stream

Returns
  true on success

## Lock()

bool CBot::CBotClass::Lock | ( | CBotProgram * | prog | ) |

Request a lock on this class (for "synchronized" keyword).

Parameters

  prog | Program that requests the lock

Returns
  true if lock was acquired, false if the lock is already taken

## Unlock()

void CBot::CBotClass::Unlock | ( |  | ) |

Release the lock acquired in Lock() If you call Lock() multiple times for the same program, you have to call Unlock() multiple times too.

## FreeLock()

void CBot::CBotClass::FreeLock | ( | CBotProgram * | prog | ) |

static

Release all locks in all classes held by this program.

Parameters

  prog | Program to release the locks from

## CheckCall()

bool CBot::CBotClass::CheckCall | ( | CBotProgram * | program,
|  | CBotDefParam * | pParam,
|  | CBotToken *& | pToken )

CheckCall Test if a procedure name is already defined somewhere.

Parameters

  program |
  pToken |
  pParam |

Returns

The documentation for this class was generated from the following files:

- src/CBot/CBotClass.h
- src/CBot/CBotClass.cpp