Colobot
CBotUtils.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/CBotTypResult.h"
23 
24 #include <cstdio>
25 #include <string>
26 #include <cassert>
27 
28 namespace CBot
29 {
30 
31 class CBotVar;
32 class CBotToken;
33 class CBotCStack;
34 
42 CBotVar* MakeListVars(CBotVar** ppVars, bool bSetVal=false);
43 
50 CBotTypResult TypeParam(CBotToken* &p, CBotCStack* pile);
51 
59 CBotTypResult ArrayType(CBotToken* &p, CBotCStack* pile, CBotTypResult type);
60 
67 bool WriteWord(FILE* pf, unsigned short w);
68 
75 bool WriteString(FILE* pf, std::string s);
76 
83 bool WriteFloat(FILE* pf, float w);
84 
90 long GetNumInt(const std::string& str);
91 
97 float GetNumFloat(const std::string& str);
98 
99 template<typename T> class CBotLinkedList
100 {
101 public:
105  virtual ~CBotLinkedList()
106  {
107  if (m_next != nullptr)
108  {
109  delete m_next;
110  m_next = nullptr;
111  }
112  }
113 
118  T* GetNext()
119  {
120  return m_next;
121  }
122 
127  void AddNext(T* elem)
128  {
129  CBotLinkedList<T>* p = this;
130  while (p->m_next != nullptr) p = p->m_next;
131  p->m_next = elem;
132  }
133 
134 protected:
135  T* m_next = nullptr;
136 };
137 
138 template<typename T> class CBotDoublyLinkedList
139 {
140 public:
145  {
146  assert(m_prev == nullptr);
147 
148  if (m_next != nullptr)
149  {
150  m_next->m_prev = nullptr;
151  delete m_next;
152  m_next = nullptr;
153  }
154  }
155 
160  T* GetNext()
161  {
162  return m_next;
163  }
164 
169  T* GetPrev()
170  {
171  return m_prev;
172  }
173 
178  void AddNext(T* elem)
179  {
180  CBotDoublyLinkedList<T>* p = this;
181  while (p->m_next != nullptr) p = p->m_next;
182  p->m_next = elem;
183  elem->m_prev = p;
184  }
185 
186 protected:
187  T* m_next = nullptr;
188  T* m_prev = nullptr;
189 };
190 
191 } // namespace CBot
void AddNext(T *elem)
Appends a new element at the end of the linked list.
Definition: CBotUtils.h:178
virtual ~CBotLinkedList()
Destructor. Be careful, destroys the whole linked list!
Definition: CBotUtils.h:105
T * GetPrev()
Returns the previous variable in the linked list.
Definition: CBotUtils.h:169
bool WriteWord(FILE *pf, unsigned short w)
WriteWord.
Definition: CBotUtils.cpp:112
float GetNumFloat(const std::string &str)
GetNumFloat Converts a string into a float number.
Definition: CBotUtils.cpp:179
Definition: CBotUtils.h:138
T * GetNext()
Returns the next variable in the linked list.
Definition: CBotUtils.h:160
virtual ~CBotDoublyLinkedList()
Destructor. Be careful, destroys the whole linked list!
Definition: CBotUtils.h:144
CBotTypResult TypeParam(CBotToken *&p, CBotCStack *pile)
TypeParam.
Definition: CBotUtils.cpp:59
long GetNumInt(const std::string &str)
GetNumInt Converts a string into integer may be of the form 0xabc123.
Definition: CBotUtils.cpp:144
bool WriteString(FILE *pf, std::string s)
WriteString.
Definition: CBotUtils.cpp:122
CBotVar * MakeListVars(CBotVar **ppVars, bool bSetVal)
MakeListVars Transforms the array of pointers to variables in a chained list of variables.
Definition: CBotUtils.cpp:35
T * GetNext()
Returns the next variable in the linked list.
Definition: CBotUtils.h:118
Definition: CBotUtils.h:99
bool WriteFloat(FILE *pf, float w)
WriteFloat.
Definition: CBotUtils.cpp:134
CBotTypResult ArrayType(CBotToken *&p, CBotCStack *pile, CBotTypResult type)
ArrayType.
Definition: CBotUtils.cpp:97
CBot engine.
Definition: CBotCallMethode.cpp:28
void AddNext(T *elem)
Appends a new element at the end of the linked list.
Definition: CBotUtils.h:127