event_value.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2015 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28 
29 
30 #pragma once
31 
32 #include "../../Core/System/databuffer.h"
33 
34 namespace clan
35 {
38 
41 {
42 public:
43  enum Type
44  {
45  null,
54  binary
55  };
56 
58 
62  NetGameEventValue(int value);
63 
67  NetGameEventValue(unsigned int value);
68 
72  NetGameEventValue(char value);
73 
77  NetGameEventValue(unsigned char value);
78 
82  NetGameEventValue(float value);
83 
87  NetGameEventValue(const std::string &value);
88 
92  NetGameEventValue(const char *str);
93 
97  NetGameEventValue(const wchar_t *str);
98 
102  explicit NetGameEventValue(bool value);
103 
108 
113 
117  Type get_type() const;
118 
122  bool is_null() const;
123 
127  bool is_uinteger() const;
128 
132  bool is_integer() const;
133 
137  bool is_ucharacter() const;
138 
142  bool is_character() const;
143 
147  bool is_number() const;
148 
152  bool is_string() const;
153 
157  bool is_boolean() const;
158 
162  bool is_binary() const;
163 
167  bool is_complex() const;
168 
169  unsigned int get_member_count() const;
170  const NetGameEventValue &get_member(unsigned int index) const;
171 
175  void add_member(const NetGameEventValue &value);
176 
181  void set_member(unsigned int index, const NetGameEventValue &value);
182 
186  unsigned int get_uinteger() const;
187 
191  int get_integer() const;
192 
196  unsigned int get_ucharacter() const;
197 
201  int get_character() const;
202 
206  float get_number() const;
207 
211  std::string get_string() const;
212 
216  bool get_boolean() const;
217 
222 
223  inline operator unsigned int() const { return get_uinteger(); }
224  inline operator int() const { return get_integer(); }
225  inline operator unsigned char() const { return get_ucharacter(); }
226  inline operator float() const { return get_number(); }
227  inline operator std::string() const { return get_string(); }
228  inline operator bool() const { return get_boolean(); }
229  inline operator DataBuffer() const { return get_binary(); }
230 
232  static std::string to_string(const NetGameEventValue &);
233 
234 private:
236  void throw_if_not_complex() const;
237 
238  Type type;
239  union
240  {
242  unsigned int value_uint;
244  unsigned char value_uchar;
245  float value_float;
247  };
248  std::string value_string;
249  DataBuffer value_binary;
250  std::vector<NetGameEventValue> value_complex;
251 };
252 
253 }
254 
256 
bool is_ucharacter() const
Is Ucharacter.
NetGameEventValue(char value)
Constructs a NetGameEventValue.
int get_integer() const
To integer.
Type get_type() const
Get Type.
NetGameEventValue(float value)
Constructs a NetGameEventValue.
NetGameEventValue(const char *str)
Constructs a NetGameEventValue.
NetGameEventValue(const wchar_t *str)
Constructs a NetGameEventValue.
NetGameEventValue.
Definition: event_value.h:41
void add_member(const NetGameEventValue &value)
Add member.
General purpose data buffer.
Definition: databuffer.h:43
unsigned char value_uchar
Definition: event_value.h:244
bool is_boolean() const
Is Boolean.
bool is_number() const
Is Number.
@ string
Definition: event_value.h:50
bool is_complex() const
Is Complex.
NetGameEventValue(unsigned char value)
Constructs a NetGameEventValue.
bool is_integer() const
Is Integer.
bool value_bool
Definition: event_value.h:246
const NetGameEventValue & get_member(unsigned int index) const
void set_member(unsigned int index, const NetGameEventValue &value)
Set member.
bool is_uinteger() const
Is Uinteger.
@ ucharacter
Definition: event_value.h:49
unsigned int value_uint
Definition: event_value.h:242
NetGameEventValue(int value)
Constructs a NetGameEventValue.
bool is_string() const
Is String.
float get_number() const
To number.
@ integer
Definition: event_value.h:46
bool is_null() const
Is Null.
DataBuffer get_binary() const
To binary.
int get_character() const
To character.
bool is_character() const
Is Character.
int value_int
Definition: event_value.h:241
NetGameEventValue(const std::string &value)
Constructs a NetGameEventValue.
NetGameEventValue(unsigned int value)
Constructs a NetGameEventValue.
NetGameEventValue(bool value)
Constructs a NetGameEventValue.
@ binary
Definition: event_value.h:54
unsigned int get_ucharacter() const
To unsigned character.
@ character
Definition: event_value.h:48
Definition: clanapp.h:36
NetGameEventValue(Type type)
Constructs a NetGameEventValue.
@ uinteger
Definition: event_value.h:47
NetGameEventValue(const DataBuffer &value)
Constructs a NetGameEventValue.
float value_float
Definition: event_value.h:245
bool get_boolean() const
To boolean.
unsigned int get_member_count() const
std::string get_string() const
To string.
@ boolean
Definition: event_value.h:51
unsigned int get_uinteger() const
To unsigned integer.
bool is_binary() const
Is Binary.
@ complex
Definition: event_value.h:53
@ number
Definition: event_value.h:52
static std::string to_string(const NetGameEventValue &)
Helper function to obtain a string representation of an EventValue object.
char value_char
Definition: event_value.h:243
Type
Definition: event_value.h:44