Colobot
CBotVarString.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/CBotVarValue.h"
23 
24 namespace CBot
25 {
26 
30 class CBotVarString : public CBotVarValue<std::string, CBotTypString>
31 {
32 public:
33  CBotVarString(const CBotToken &name) : CBotVarValue(name) {}
34 
35  void SetValString(const std::string& val) override
36  {
37  m_val = val;
39  }
40 
41  void SetValInt(int val, const std::string& s = "") override
42  {
43  SetValString(ToString(val));
44  }
45 
46  void SetValFloat(float val) override
47  {
48  SetValString(ToString(val));
49  }
50 
51  int GetValInt()
52  {
53  return FromString<int>(GetValString());
54  }
55 
56  float GetValFloat()
57  {
58  return FromString<float>(GetValString());
59  }
60 
61  void Add(CBotVar* left, CBotVar* right) override;
62 
63  bool Eq(CBotVar* left, CBotVar* right) override;
64  bool Ne(CBotVar* left, CBotVar* right) override;
65 
66  bool Save1State(FILE* pf) override;
67 
68 private:
69  template<typename T>
70  static std::string ToString(T val)
71  {
72  std::ostringstream ss;
73  ss << val;
74  return ss.str();
75  }
76 
77  template<typename T>
78  static T FromString(std::string val)
79  {
80  std::istringstream ss(val);
81  T v;
82  ss >> v;
83  return v;
84  }
85 };
86 
87 } // namespace CBot
InitType m_binit
Initialization status.
Definition: CBotVar.h:657
void Add(CBotVar *left, CBotVar *right) override
Addition.
Definition: CBotVarString.cpp:25
void SetValInt(int val, const std::string &s="") override
Set value as an integer.
Definition: CBotVarString.h:41
CBotVarValue(const CBotToken &name)
Constructor. Do not call directly, use CBotVar::Create()
Definition: CBotVarValue.h:44
bool Save1State(FILE *pf) override
Save variable data.
Definition: CBotVarString.cpp:40
void SetValString(const std::string &val) override
Set value as string.
Definition: CBotVarString.h:35
bool Eq(CBotVar *left, CBotVar *right) override
left == right
Definition: CBotVarString.cpp:30
void SetValFloat(float val) override
Set value as float.
Definition: CBotVarString.h:46
A CBot variable.
Definition: CBotVar.h:42
A variable holding a simple value (bool, int, float, string)
Definition: CBotVarValue.h:38
float GetValFloat()
Get value as float.
Definition: CBotVarString.h:56
std::string m_val
The value.
Definition: CBotVarValue.h:79
int GetValInt()
Get value as integer.
Definition: CBotVarString.h:51
CBotVar subclass for managing string values (CBotTypString)
Definition: CBotVarString.h:30
the variable value is defined
CBot engine.
Definition: CBotCallMethode.cpp:28
bool Ne(CBotVar *left, CBotVar *right) override
left != right
Definition: CBotVarString.cpp:35
std::string GetValString() override
Definition: CBotVarValue.h:65
Class representing one token of a program.
Definition: CBotToken.h:80