summaryrefslogtreecommitdiff
path: root/modules/gdnative/godot/godot_dictionary.cpp
blob: 16d08e58e2bd5be566ac49abbcfd83518882f6a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*************************************************************************/
/*  godot_dictionary.cpp                                                 */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                    http://www.godotengine.org                         */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md)    */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/
#include "godot_dictionary.h"

#include "core/dictionary.h"

#include "core/os/memory.h"

#include "core/io/json.h"

#ifdef __cplusplus
extern "C" {
#endif

void _dictionary_api_anchor() {
}

void GDAPI godot_dictionary_new(godot_dictionary *p_dict) {
	Dictionary *dict = (Dictionary *)p_dict;
	memnew_placement(dict, Dictionary);
}

void GDAPI godot_dictionary_clear(godot_dictionary *p_dict) {
	Dictionary *dict = (Dictionary *)p_dict;
	dict->clear();
}

godot_bool GDAPI godot_dictionary_empty(const godot_dictionary *p_dict) {
	const Dictionary *dict = (const Dictionary *)p_dict;
	return dict->empty();
}

void GDAPI godot_dictionary_erase(godot_dictionary *p_dict, const godot_variant *p_key) {
	Dictionary *dict = (Dictionary *)p_dict;
	Variant *key = (Variant *)p_key;
	dict->erase(*key);
}

godot_bool GDAPI godot_dictionary_has(const godot_dictionary *p_dict, const godot_variant *p_key) {
	const Dictionary *dict = (const Dictionary *)p_dict;
	const Variant *key = (const Variant *)p_key;
	return dict->has(*key);
}

godot_bool GDAPI godot_dictionary_has_all(const godot_dictionary *p_dict, const godot_array *p_keys) {
	const Dictionary *dict = (const Dictionary *)p_dict;
	const Array *keys = (const Array *)p_keys;
	return dict->has_all(*keys);
}

uint32_t GDAPI godot_dictionary_hash(const godot_dictionary *p_dict) {
	const Dictionary *dict = (const Dictionary *)p_dict;
	return dict->hash();
}

godot_array GDAPI godot_dictionary_keys(const godot_dictionary *p_dict) {
	godot_array a;
	godot_array_new(&a);
	const Dictionary *dict = (const Dictionary *)p_dict;
	Array *array = (Array *)&a;
	*array = dict->keys();
	return a;
}

godot_int GDAPI godot_dictionary_parse_json(godot_dictionary *p_dict, const godot_string *p_json) {
	Dictionary *dict = (Dictionary *)p_dict;
	const String *json = (const String *)p_json;
	Variant ret;
	int err_line;
	String err_str;
	int err = (int)JSON::parse(*json, ret, err_str, err_line);
	*dict = ret;
	return err;
}

godot_variant GDAPI *godot_dictionary_operator_index(godot_dictionary *p_dict, const godot_variant *p_key) {
	Dictionary *dict = (Dictionary *)p_dict;
	Variant *key = (Variant *)p_key;
	return (godot_variant *)&dict->operator[](*key);
}

godot_int GDAPI godot_dictionary_size(const godot_dictionary *p_dict) {
	const Dictionary *dict = (const Dictionary *)p_dict;
	return dict->size();
}

godot_string GDAPI godot_dictionary_to_json(const godot_dictionary *p_dict) {
	const Dictionary *dict = (const Dictionary *)p_dict;
	godot_string str;
	godot_string_new(&str);
	String *s = (String *)&str;
	*s = JSON::print(Variant(*dict));
	return str;
}

godot_array GDAPI godot_dictionary_values(const godot_dictionary *p_dict) {
	godot_array a;
	godot_array_new(&a);
	const Dictionary *dict = (const Dictionary *)p_dict;
	Array *array = (Array *)&a;
	*array = dict->values();
	return a;
}

void GDAPI godot_dictionary_destroy(godot_dictionary *p_dict) {
	((Dictionary *)p_dict)->~Dictionary();
}

#ifdef __cplusplus
}
#endif