mirror of
https://github.com/Relintai/sfw.git
synced 2024-11-08 07:52:09 +01:00
Added Variant from Pandemonium.
This commit is contained in:
parent
b0dfb4ef5e
commit
dae144ed0d
479
sfw/object/array.cpp
Normal file
479
sfw/object/array.cpp
Normal file
@ -0,0 +1,479 @@
|
||||
/*************************************************************************/
|
||||
/* array.cpp */
|
||||
/* From https://github.com/Relintai/pandemonium_engine (MIT) */
|
||||
/*************************************************************************/
|
||||
|
||||
#include "array.h"
|
||||
|
||||
#include "core/containers/hashfuncs.h"
|
||||
#include "core/containers/vector.h"
|
||||
#include "core/object/object.h"
|
||||
#include "core/variant/variant.h"
|
||||
|
||||
class ArrayPrivate {
|
||||
public:
|
||||
SafeRefCount refcount;
|
||||
Vector<Variant> array;
|
||||
};
|
||||
|
||||
void Array::_ref(const Array &p_from) const {
|
||||
ArrayPrivate *_fp = p_from._p;
|
||||
|
||||
ERR_FAIL_COND(!_fp); // should NOT happen.
|
||||
|
||||
if (_fp == _p) {
|
||||
return; // whatever it is, nothing to do here move along
|
||||
}
|
||||
|
||||
bool success = _fp->refcount.ref();
|
||||
|
||||
ERR_FAIL_COND(!success); // should really not happen either
|
||||
|
||||
_unref();
|
||||
|
||||
_p = p_from._p;
|
||||
}
|
||||
|
||||
void Array::_unref() const {
|
||||
if (!_p) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (_p->refcount.unref()) {
|
||||
memdelete(_p);
|
||||
}
|
||||
_p = nullptr;
|
||||
}
|
||||
|
||||
Variant &Array::operator[](int p_idx) {
|
||||
return _p->array.write[p_idx];
|
||||
}
|
||||
|
||||
const Variant &Array::operator[](int p_idx) const {
|
||||
return _p->array[p_idx];
|
||||
}
|
||||
|
||||
int Array::size() const {
|
||||
return _p->array.size();
|
||||
}
|
||||
bool Array::empty() const {
|
||||
return _p->array.empty();
|
||||
}
|
||||
void Array::clear() {
|
||||
_p->array.clear();
|
||||
}
|
||||
|
||||
bool Array::deep_equal(const Array &p_array, int p_recursion_count) const {
|
||||
// Cheap checks
|
||||
ERR_FAIL_COND_V_MSG(p_recursion_count > MAX_RECURSION, true, "Max recursion reached");
|
||||
if (_p == p_array._p) {
|
||||
return true;
|
||||
}
|
||||
const Vector<Variant> &a1 = _p->array;
|
||||
const Vector<Variant> &a2 = p_array._p->array;
|
||||
const int size = a1.size();
|
||||
if (size != a2.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Heavy O(n) check
|
||||
p_recursion_count++;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (!a1[i].deep_equal(a2[i], p_recursion_count)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Array::operator==(const Array &p_array) const {
|
||||
return _p == p_array._p;
|
||||
}
|
||||
|
||||
uint32_t Array::hash() const {
|
||||
return recursive_hash(0);
|
||||
}
|
||||
|
||||
uint32_t Array::recursive_hash(int p_recursion_count) const {
|
||||
ERR_FAIL_COND_V_MSG(p_recursion_count > MAX_RECURSION, 0, "Max recursion reached");
|
||||
p_recursion_count++;
|
||||
|
||||
uint32_t h = hash_murmur3_one_32(0);
|
||||
|
||||
for (int i = 0; i < _p->array.size(); i++) {
|
||||
h = hash_murmur3_one_32(_p->array[i].recursive_hash(p_recursion_count), h);
|
||||
}
|
||||
return hash_fmix32(h);
|
||||
}
|
||||
|
||||
void Array::operator=(const Array &p_array) {
|
||||
_ref(p_array);
|
||||
}
|
||||
|
||||
void Array::push_back(const Variant &p_value) {
|
||||
_p->array.push_back(p_value);
|
||||
}
|
||||
|
||||
void Array::append_array(const Array &p_array) {
|
||||
_p->array.append_array(p_array._p->array);
|
||||
}
|
||||
|
||||
Error Array::resize(int p_new_size) {
|
||||
return _p->array.resize(p_new_size);
|
||||
}
|
||||
|
||||
void Array::insert(int p_pos, const Variant &p_value) {
|
||||
_p->array.insert(p_pos, p_value);
|
||||
}
|
||||
|
||||
void Array::fill(const Variant &p_value) {
|
||||
_p->array.fill(p_value);
|
||||
}
|
||||
|
||||
void Array::erase(const Variant &p_value) {
|
||||
_p->array.erase(p_value);
|
||||
}
|
||||
|
||||
Variant Array::front() const {
|
||||
ERR_FAIL_COND_V_MSG(_p->array.size() == 0, Variant(), "Can't take value from empty array.");
|
||||
return operator[](0);
|
||||
}
|
||||
|
||||
Variant Array::back() const {
|
||||
ERR_FAIL_COND_V_MSG(_p->array.size() == 0, Variant(), "Can't take value from empty array.");
|
||||
return operator[](_p->array.size() - 1);
|
||||
}
|
||||
|
||||
int Array::find(const Variant &p_value, int p_from) const {
|
||||
return _p->array.find(p_value, p_from);
|
||||
}
|
||||
|
||||
int Array::rfind(const Variant &p_value, int p_from) const {
|
||||
if (_p->array.size() == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (p_from < 0) {
|
||||
// Relative offset from the end
|
||||
p_from = _p->array.size() + p_from;
|
||||
}
|
||||
if (p_from < 0 || p_from >= _p->array.size()) {
|
||||
// Limit to array boundaries
|
||||
p_from = _p->array.size() - 1;
|
||||
}
|
||||
|
||||
for (int i = p_from; i >= 0; i--) {
|
||||
if (_p->array[i] == p_value) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int Array::find_last(const Variant &p_value) const {
|
||||
return rfind(p_value);
|
||||
}
|
||||
|
||||
int Array::count(const Variant &p_value) const {
|
||||
if (_p->array.size() == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int amount = 0;
|
||||
for (int i = 0; i < _p->array.size(); i++) {
|
||||
if (_p->array[i] == p_value) {
|
||||
amount++;
|
||||
}
|
||||
}
|
||||
|
||||
return amount;
|
||||
}
|
||||
|
||||
bool Array::has(const Variant &p_value) const {
|
||||
return _p->array.find(p_value, 0) != -1;
|
||||
}
|
||||
|
||||
void Array::remove(int p_pos) {
|
||||
_p->array.remove(p_pos);
|
||||
}
|
||||
|
||||
void Array::set(int p_idx, const Variant &p_value) {
|
||||
operator[](p_idx) = p_value;
|
||||
}
|
||||
|
||||
const Variant &Array::get(int p_idx) const {
|
||||
return operator[](p_idx);
|
||||
}
|
||||
|
||||
Array Array::duplicate(bool p_deep) const {
|
||||
Array new_arr;
|
||||
int element_count = size();
|
||||
new_arr.resize(element_count);
|
||||
for (int i = 0; i < element_count; i++) {
|
||||
new_arr[i] = p_deep ? get(i).duplicate(p_deep) : get(i);
|
||||
}
|
||||
|
||||
return new_arr;
|
||||
}
|
||||
|
||||
int Array::_clamp_slice_index(int p_index) const {
|
||||
int arr_size = size();
|
||||
int fixed_index = CLAMP(p_index, -arr_size, arr_size - 1);
|
||||
if (fixed_index < 0) {
|
||||
fixed_index = arr_size + fixed_index;
|
||||
}
|
||||
return fixed_index;
|
||||
}
|
||||
|
||||
Array Array::slice(int p_begin, int p_end, int p_step, bool p_deep) const { // like python, but inclusive on upper bound
|
||||
|
||||
Array new_arr;
|
||||
|
||||
ERR_FAIL_COND_V_MSG(p_step == 0, new_arr, "Array slice step size cannot be zero.");
|
||||
|
||||
if (empty()) { // Don't try to slice empty arrays.
|
||||
return new_arr;
|
||||
}
|
||||
if (p_step > 0) {
|
||||
if (p_begin >= size() || p_end < -size()) {
|
||||
return new_arr;
|
||||
}
|
||||
} else { // p_step < 0
|
||||
if (p_begin < -size() || p_end >= size()) {
|
||||
return new_arr;
|
||||
}
|
||||
}
|
||||
|
||||
int begin = _clamp_slice_index(p_begin);
|
||||
int end = _clamp_slice_index(p_end);
|
||||
|
||||
int new_arr_size = MAX(((end - begin + p_step) / p_step), 0);
|
||||
new_arr.resize(new_arr_size);
|
||||
|
||||
if (p_step > 0) {
|
||||
int dest_idx = 0;
|
||||
for (int idx = begin; idx <= end; idx += p_step) {
|
||||
ERR_FAIL_COND_V_MSG(dest_idx < 0 || dest_idx >= new_arr_size, Array(), "Bug in Array slice()");
|
||||
new_arr[dest_idx++] = p_deep ? get(idx).duplicate(p_deep) : get(idx);
|
||||
}
|
||||
} else { // p_step < 0
|
||||
int dest_idx = 0;
|
||||
for (int idx = begin; idx >= end; idx += p_step) {
|
||||
ERR_FAIL_COND_V_MSG(dest_idx < 0 || dest_idx >= new_arr_size, Array(), "Bug in Array slice()");
|
||||
new_arr[dest_idx++] = p_deep ? get(idx).duplicate(p_deep) : get(idx);
|
||||
}
|
||||
}
|
||||
|
||||
return new_arr;
|
||||
}
|
||||
|
||||
struct _ArrayVariantSort {
|
||||
_FORCE_INLINE_ bool operator()(const Variant &p_l, const Variant &p_r) const {
|
||||
bool valid = false;
|
||||
Variant res;
|
||||
Variant::evaluate(Variant::OP_LESS, p_l, p_r, res, valid);
|
||||
if (!valid) {
|
||||
res = false;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
Array &Array::sort() {
|
||||
_p->array.sort_custom<_ArrayVariantSort>();
|
||||
return *this;
|
||||
}
|
||||
|
||||
struct _ArrayVariantSortCustom {
|
||||
Object *obj;
|
||||
StringName func;
|
||||
|
||||
_FORCE_INLINE_ bool operator()(const Variant &p_l, const Variant &p_r) const {
|
||||
const Variant *args[2] = { &p_l, &p_r };
|
||||
Variant::CallError err;
|
||||
bool res = obj->call(func, args, 2, err);
|
||||
if (err.error != Variant::CallError::CALL_OK) {
|
||||
res = false;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
Array &Array::sort_custom(Object *p_obj, const StringName &p_function) {
|
||||
ERR_FAIL_NULL_V(p_obj, *this);
|
||||
|
||||
SortArray<Variant, _ArrayVariantSortCustom, true> avs;
|
||||
avs.compare.obj = p_obj;
|
||||
avs.compare.func = p_function;
|
||||
avs.sort(_p->array.ptrw(), _p->array.size());
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Array::shuffle() {
|
||||
const int n = _p->array.size();
|
||||
if (n < 2) {
|
||||
return;
|
||||
}
|
||||
Variant *data = _p->array.ptrw();
|
||||
for (int i = n - 1; i >= 1; i--) {
|
||||
const int j = Math::rand() % (i + 1);
|
||||
const Variant tmp = data[j];
|
||||
data[j] = data[i];
|
||||
data[i] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Less>
|
||||
_FORCE_INLINE_ int bisect(const Vector<Variant> &p_array, const Variant &p_value, bool p_before, const Less &p_less) {
|
||||
int lo = 0;
|
||||
int hi = p_array.size();
|
||||
if (p_before) {
|
||||
while (lo < hi) {
|
||||
const int mid = (lo + hi) / 2;
|
||||
if (p_less(p_array.get(mid), p_value)) {
|
||||
lo = mid + 1;
|
||||
} else {
|
||||
hi = mid;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
while (lo < hi) {
|
||||
const int mid = (lo + hi) / 2;
|
||||
if (p_less(p_value, p_array.get(mid))) {
|
||||
hi = mid;
|
||||
} else {
|
||||
lo = mid + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return lo;
|
||||
}
|
||||
|
||||
int Array::bsearch(const Variant &p_value, bool p_before) {
|
||||
return bisect(_p->array, p_value, p_before, _ArrayVariantSort());
|
||||
}
|
||||
|
||||
int Array::bsearch_custom(const Variant &p_value, Object *p_obj, const StringName &p_function, bool p_before) {
|
||||
ERR_FAIL_NULL_V(p_obj, 0);
|
||||
|
||||
_ArrayVariantSortCustom less;
|
||||
less.obj = p_obj;
|
||||
less.func = p_function;
|
||||
|
||||
return bisect(_p->array, p_value, p_before, less);
|
||||
}
|
||||
|
||||
Array &Array::invert() {
|
||||
_p->array.invert();
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Array::push_front(const Variant &p_value) {
|
||||
_p->array.insert(0, p_value);
|
||||
}
|
||||
|
||||
Variant Array::pop_back() {
|
||||
if (!_p->array.empty()) {
|
||||
const int n = _p->array.size() - 1;
|
||||
const Variant ret = _p->array.get(n);
|
||||
_p->array.resize(n);
|
||||
return ret;
|
||||
}
|
||||
return Variant();
|
||||
}
|
||||
|
||||
Variant Array::pop_front() {
|
||||
if (!_p->array.empty()) {
|
||||
const Variant ret = _p->array.get(0);
|
||||
_p->array.remove(0);
|
||||
return ret;
|
||||
}
|
||||
return Variant();
|
||||
}
|
||||
|
||||
Variant Array::pop_at(int p_pos) {
|
||||
if (_p->array.empty()) {
|
||||
// Return `null` without printing an error to mimic `pop_back()` and `pop_front()` behavior.
|
||||
return Variant();
|
||||
}
|
||||
|
||||
if (p_pos < 0) {
|
||||
// Relative offset from the end
|
||||
p_pos = _p->array.size() + p_pos;
|
||||
}
|
||||
|
||||
ERR_FAIL_INDEX_V_MSG(
|
||||
p_pos,
|
||||
_p->array.size(),
|
||||
Variant(),
|
||||
vformat(
|
||||
"The calculated index %s is out of bounds (the array has %s elements). Leaving the array untouched and returning `null`.",
|
||||
p_pos,
|
||||
_p->array.size()));
|
||||
|
||||
const Variant ret = _p->array.get(p_pos);
|
||||
_p->array.remove(p_pos);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Variant Array::min() const {
|
||||
Variant minval;
|
||||
for (int i = 0; i < size(); i++) {
|
||||
if (i == 0) {
|
||||
minval = get(i);
|
||||
} else {
|
||||
bool valid;
|
||||
Variant ret;
|
||||
Variant test = get(i);
|
||||
Variant::evaluate(Variant::OP_LESS, test, minval, ret, valid);
|
||||
if (!valid) {
|
||||
return Variant(); //not a valid comparison
|
||||
}
|
||||
if (bool(ret)) {
|
||||
//is less
|
||||
minval = test;
|
||||
}
|
||||
}
|
||||
}
|
||||
return minval;
|
||||
}
|
||||
|
||||
Variant Array::max() const {
|
||||
Variant maxval;
|
||||
for (int i = 0; i < size(); i++) {
|
||||
if (i == 0) {
|
||||
maxval = get(i);
|
||||
} else {
|
||||
bool valid;
|
||||
Variant ret;
|
||||
Variant test = get(i);
|
||||
Variant::evaluate(Variant::OP_GREATER, test, maxval, ret, valid);
|
||||
if (!valid) {
|
||||
return Variant(); //not a valid comparison
|
||||
}
|
||||
if (bool(ret)) {
|
||||
//is less
|
||||
maxval = test;
|
||||
}
|
||||
}
|
||||
}
|
||||
return maxval;
|
||||
}
|
||||
|
||||
const void *Array::id() const {
|
||||
return _p;
|
||||
}
|
||||
|
||||
Array::Array(const Array &p_from) {
|
||||
_p = nullptr;
|
||||
_ref(p_from);
|
||||
}
|
||||
|
||||
Array::Array() {
|
||||
_p = memnew(ArrayPrivate);
|
||||
_p->refcount.init();
|
||||
}
|
||||
Array::~Array() {
|
||||
_unref();
|
||||
}
|
87
sfw/object/array.h
Normal file
87
sfw/object/array.h
Normal file
@ -0,0 +1,87 @@
|
||||
#ifndef ARRAY_H
|
||||
#define ARRAY_H
|
||||
|
||||
/*************************************************************************/
|
||||
/* array.h */
|
||||
/* From https://github.com/Relintai/pandemonium_engine (MIT) */
|
||||
/*************************************************************************/
|
||||
|
||||
#include "core/typedefs.h"
|
||||
|
||||
class Variant;
|
||||
class ArrayPrivate;
|
||||
class Object;
|
||||
class StringName;
|
||||
|
||||
class Array {
|
||||
mutable ArrayPrivate *_p;
|
||||
void _ref(const Array &p_from) const;
|
||||
void _unref() const;
|
||||
|
||||
inline int _clamp_slice_index(int p_index) const;
|
||||
|
||||
public:
|
||||
Variant &operator[](int p_idx);
|
||||
const Variant &operator[](int p_idx) const;
|
||||
|
||||
void set(int p_idx, const Variant &p_value);
|
||||
const Variant &get(int p_idx) const;
|
||||
|
||||
int size() const;
|
||||
bool empty() const;
|
||||
void clear();
|
||||
|
||||
bool deep_equal(const Array &p_array, int p_recursion_count = 0) const;
|
||||
bool operator==(const Array &p_array) const;
|
||||
|
||||
uint32_t hash() const;
|
||||
uint32_t recursive_hash(int p_recursion_count) const;
|
||||
void operator=(const Array &p_array);
|
||||
|
||||
void push_back(const Variant &p_value);
|
||||
_FORCE_INLINE_ void append(const Variant &p_value) { push_back(p_value); } //for python compatibility
|
||||
void append_array(const Array &p_array);
|
||||
Error resize(int p_new_size);
|
||||
|
||||
void insert(int p_pos, const Variant &p_value);
|
||||
void remove(int p_pos);
|
||||
void fill(const Variant &p_value);
|
||||
|
||||
Variant front() const;
|
||||
Variant back() const;
|
||||
|
||||
Array &sort();
|
||||
Array &sort_custom(Object *p_obj, const StringName &p_function);
|
||||
void shuffle();
|
||||
int bsearch(const Variant &p_value, bool p_before = true);
|
||||
int bsearch_custom(const Variant &p_value, Object *p_obj, const StringName &p_function, bool p_before = true);
|
||||
Array &invert();
|
||||
|
||||
int find(const Variant &p_value, int p_from = 0) const;
|
||||
int rfind(const Variant &p_value, int p_from = -1) const;
|
||||
int find_last(const Variant &p_value) const;
|
||||
int count(const Variant &p_value) const;
|
||||
bool has(const Variant &p_value) const;
|
||||
|
||||
void erase(const Variant &p_value);
|
||||
|
||||
void push_front(const Variant &p_value);
|
||||
Variant pop_back();
|
||||
Variant pop_front();
|
||||
Variant pop_at(int p_pos);
|
||||
|
||||
Array duplicate(bool p_deep = false) const;
|
||||
|
||||
Array slice(int p_begin, int p_end, int p_step = 1, bool p_deep = false) const;
|
||||
|
||||
Variant min() const;
|
||||
Variant max() const;
|
||||
|
||||
const void *id() const;
|
||||
|
||||
Array(const Array &p_from);
|
||||
Array();
|
||||
~Array();
|
||||
};
|
||||
|
||||
#endif // ARRAY_H
|
297
sfw/object/dictionary.cpp
Normal file
297
sfw/object/dictionary.cpp
Normal file
@ -0,0 +1,297 @@
|
||||
/*************************************************************************/
|
||||
/* dictionary.cpp */
|
||||
/* From https://github.com/Relintai/pandemonium_engine (MIT) */
|
||||
/*************************************************************************/
|
||||
|
||||
#include "dictionary.h"
|
||||
|
||||
#include "core/containers/ordered_hash_map.h"
|
||||
#include "core/os/safe_refcount.h"
|
||||
#include "core/variant/variant.h"
|
||||
|
||||
struct DictionaryPrivate {
|
||||
SafeRefCount refcount;
|
||||
OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator> variant_map;
|
||||
};
|
||||
|
||||
void Dictionary::get_key_list(List<Variant> *p_keys) const {
|
||||
if (_p->variant_map.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
|
||||
p_keys->push_back(E.key());
|
||||
}
|
||||
}
|
||||
|
||||
Variant Dictionary::get_key_at_index(int p_index) const {
|
||||
int index = 0;
|
||||
for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
|
||||
if (index == p_index) {
|
||||
return E.key();
|
||||
}
|
||||
index++;
|
||||
}
|
||||
|
||||
return Variant();
|
||||
}
|
||||
|
||||
Variant Dictionary::get_value_at_index(int p_index) const {
|
||||
int index = 0;
|
||||
for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
|
||||
if (index == p_index) {
|
||||
return E.value();
|
||||
}
|
||||
index++;
|
||||
}
|
||||
|
||||
return Variant();
|
||||
}
|
||||
|
||||
Variant &Dictionary::operator[](const Variant &p_key) {
|
||||
return _p->variant_map[p_key];
|
||||
}
|
||||
|
||||
const Variant &Dictionary::operator[](const Variant &p_key) const {
|
||||
return _p->variant_map[p_key];
|
||||
}
|
||||
const Variant *Dictionary::getptr(const Variant &p_key) const {
|
||||
OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::ConstElement E = ((const OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator> *)&_p->variant_map)->find(p_key);
|
||||
|
||||
if (!E) {
|
||||
return nullptr;
|
||||
}
|
||||
return &E.get();
|
||||
}
|
||||
|
||||
Variant *Dictionary::getptr(const Variant &p_key) {
|
||||
OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.find(p_key);
|
||||
|
||||
if (!E) {
|
||||
return nullptr;
|
||||
}
|
||||
return &E.get();
|
||||
}
|
||||
|
||||
Variant Dictionary::get_valid(const Variant &p_key) const {
|
||||
OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::ConstElement E = ((const OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator> *)&_p->variant_map)->find(p_key);
|
||||
|
||||
if (!E) {
|
||||
return Variant();
|
||||
}
|
||||
return E.get();
|
||||
}
|
||||
|
||||
Variant Dictionary::get(const Variant &p_key, const Variant &p_default) const {
|
||||
const Variant *result = getptr(p_key);
|
||||
if (!result) {
|
||||
return p_default;
|
||||
}
|
||||
|
||||
return *result;
|
||||
}
|
||||
|
||||
int Dictionary::size() const {
|
||||
return _p->variant_map.size();
|
||||
}
|
||||
bool Dictionary::empty() const {
|
||||
return !_p->variant_map.size();
|
||||
}
|
||||
|
||||
bool Dictionary::has(const Variant &p_key) const {
|
||||
return _p->variant_map.has(p_key);
|
||||
}
|
||||
|
||||
bool Dictionary::has_all(const Array &p_keys) const {
|
||||
for (int i = 0; i < p_keys.size(); i++) {
|
||||
if (!has(p_keys[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Variant Dictionary::find_key(const Variant &p_value) const {
|
||||
for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
|
||||
if (E.value() == p_value) {
|
||||
return E.key();
|
||||
}
|
||||
}
|
||||
return Variant();
|
||||
}
|
||||
|
||||
bool Dictionary::erase(const Variant &p_key) {
|
||||
return _p->variant_map.erase(p_key);
|
||||
}
|
||||
|
||||
bool Dictionary::deep_equal(const Dictionary &p_dictionary, int p_recursion_count) const {
|
||||
// Cheap checks
|
||||
ERR_FAIL_COND_V_MSG(p_recursion_count > MAX_RECURSION, 0, "Max recursion reached");
|
||||
if (_p == p_dictionary._p) {
|
||||
return true;
|
||||
}
|
||||
if (_p->variant_map.size() != p_dictionary._p->variant_map.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Heavy O(n) check
|
||||
OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element this_E = _p->variant_map.front();
|
||||
OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element other_E = p_dictionary._p->variant_map.front();
|
||||
p_recursion_count++;
|
||||
while (this_E && other_E) {
|
||||
if (
|
||||
!this_E.key().deep_equal(other_E.key(), p_recursion_count) ||
|
||||
!this_E.value().deep_equal(other_E.value(), p_recursion_count)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this_E = this_E.next();
|
||||
other_E = other_E.next();
|
||||
}
|
||||
|
||||
return !this_E && !other_E;
|
||||
}
|
||||
|
||||
bool Dictionary::operator==(const Dictionary &p_dictionary) const {
|
||||
return _p == p_dictionary._p;
|
||||
}
|
||||
|
||||
bool Dictionary::operator!=(const Dictionary &p_dictionary) const {
|
||||
return _p != p_dictionary._p;
|
||||
}
|
||||
|
||||
void Dictionary::_ref(const Dictionary &p_from) const {
|
||||
//make a copy first (thread safe)
|
||||
if (!p_from._p->refcount.ref()) {
|
||||
return; // couldn't copy
|
||||
}
|
||||
|
||||
//if this is the same, unreference the other one
|
||||
if (p_from._p == _p) {
|
||||
_p->refcount.unref();
|
||||
return;
|
||||
}
|
||||
if (_p) {
|
||||
_unref();
|
||||
}
|
||||
_p = p_from._p;
|
||||
}
|
||||
|
||||
void Dictionary::clear() {
|
||||
_p->variant_map.clear();
|
||||
}
|
||||
|
||||
void Dictionary::merge(const Dictionary &p_dictionary, bool p_overwrite) {
|
||||
for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = p_dictionary._p->variant_map.front(); E; E = E.next()) {
|
||||
if (p_overwrite || !has(E.key())) {
|
||||
this->operator[](E.key()) = E.value();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Dictionary::_unref() const {
|
||||
ERR_FAIL_COND(!_p);
|
||||
if (_p->refcount.unref()) {
|
||||
memdelete(_p);
|
||||
}
|
||||
_p = nullptr;
|
||||
}
|
||||
|
||||
uint32_t Dictionary::hash() const {
|
||||
return recursive_hash(0);
|
||||
}
|
||||
|
||||
uint32_t Dictionary::recursive_hash(int p_recursion_count) const {
|
||||
ERR_FAIL_COND_V_MSG(p_recursion_count > MAX_RECURSION, 0, "Max recursion reached");
|
||||
p_recursion_count++;
|
||||
|
||||
uint32_t h = hash_murmur3_one_32(Variant::DICTIONARY);
|
||||
|
||||
for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
|
||||
h = hash_murmur3_one_32(E.key().recursive_hash(p_recursion_count), h);
|
||||
h = hash_murmur3_one_32(E.value().recursive_hash(p_recursion_count), h);
|
||||
}
|
||||
|
||||
return hash_fmix32(h);
|
||||
}
|
||||
|
||||
Array Dictionary::keys() const {
|
||||
Array varr;
|
||||
if (_p->variant_map.empty()) {
|
||||
return varr;
|
||||
}
|
||||
|
||||
varr.resize(size());
|
||||
|
||||
int i = 0;
|
||||
for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
|
||||
varr[i] = E.key();
|
||||
i++;
|
||||
}
|
||||
|
||||
return varr;
|
||||
}
|
||||
|
||||
Array Dictionary::values() const {
|
||||
Array varr;
|
||||
if (_p->variant_map.empty()) {
|
||||
return varr;
|
||||
}
|
||||
|
||||
varr.resize(size());
|
||||
|
||||
int i = 0;
|
||||
for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
|
||||
varr[i] = E.get();
|
||||
i++;
|
||||
}
|
||||
|
||||
return varr;
|
||||
}
|
||||
|
||||
const Variant *Dictionary::next(const Variant *p_key) const {
|
||||
if (p_key == nullptr) {
|
||||
// caller wants to get the first element
|
||||
if (_p->variant_map.front()) {
|
||||
return &_p->variant_map.front().key();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.find(*p_key);
|
||||
|
||||
if (E && E.next()) {
|
||||
return &E.next().key();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Dictionary Dictionary::duplicate(bool p_deep) const {
|
||||
Dictionary n;
|
||||
|
||||
for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
|
||||
n[E.key()] = p_deep ? E.value().duplicate(true) : E.value();
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
void Dictionary::operator=(const Dictionary &p_dictionary) {
|
||||
_ref(p_dictionary);
|
||||
}
|
||||
|
||||
const void *Dictionary::id() const {
|
||||
return _p;
|
||||
}
|
||||
|
||||
Dictionary::Dictionary(const Dictionary &p_from) {
|
||||
_p = nullptr;
|
||||
_ref(p_from);
|
||||
}
|
||||
|
||||
Dictionary::Dictionary() {
|
||||
_p = memnew(DictionaryPrivate);
|
||||
_p->refcount.init();
|
||||
}
|
||||
Dictionary::~Dictionary() {
|
||||
_unref();
|
||||
}
|
70
sfw/object/dictionary.h
Normal file
70
sfw/object/dictionary.h
Normal file
@ -0,0 +1,70 @@
|
||||
#ifndef DICTIONARY_H
|
||||
#define DICTIONARY_H
|
||||
|
||||
/*************************************************************************/
|
||||
/* dictionary.h */
|
||||
/* From https://github.com/Relintai/pandemonium_engine (MIT) */
|
||||
/*************************************************************************/
|
||||
|
||||
#include "core/containers/list.h"
|
||||
#include "core/string/ustring.h"
|
||||
#include "core/variant/array.h"
|
||||
|
||||
class Variant;
|
||||
|
||||
struct DictionaryPrivate;
|
||||
|
||||
class Dictionary {
|
||||
mutable DictionaryPrivate *_p;
|
||||
|
||||
void _ref(const Dictionary &p_from) const;
|
||||
void _unref() const;
|
||||
|
||||
public:
|
||||
void get_key_list(List<Variant> *p_keys) const;
|
||||
Variant get_key_at_index(int p_index) const;
|
||||
Variant get_value_at_index(int p_index) const;
|
||||
|
||||
Variant &operator[](const Variant &p_key);
|
||||
const Variant &operator[](const Variant &p_key) const;
|
||||
|
||||
const Variant *getptr(const Variant &p_key) const;
|
||||
Variant *getptr(const Variant &p_key);
|
||||
|
||||
Variant get_valid(const Variant &p_key) const;
|
||||
Variant get(const Variant &p_key, const Variant &p_default) const;
|
||||
|
||||
int size() const;
|
||||
bool empty() const;
|
||||
void clear();
|
||||
void merge(const Dictionary &p_dictionary, bool p_overwrite = false);
|
||||
|
||||
bool has(const Variant &p_key) const;
|
||||
bool has_all(const Array &p_keys) const;
|
||||
Variant find_key(const Variant &p_value) const;
|
||||
|
||||
bool erase(const Variant &p_key);
|
||||
|
||||
bool deep_equal(const Dictionary &p_dictionary, int p_recursion_count = 0) const;
|
||||
bool operator==(const Dictionary &p_dictionary) const;
|
||||
bool operator!=(const Dictionary &p_dictionary) const;
|
||||
|
||||
uint32_t hash() const;
|
||||
uint32_t recursive_hash(int p_recursion_count) const;
|
||||
void operator=(const Dictionary &p_dictionary);
|
||||
|
||||
const Variant *next(const Variant *p_key = nullptr) const;
|
||||
|
||||
Array keys() const;
|
||||
Array values() const;
|
||||
|
||||
Dictionary duplicate(bool p_deep = false) const;
|
||||
|
||||
const void *id() const;
|
||||
|
||||
Dictionary(const Dictionary &p_from);
|
||||
Dictionary();
|
||||
~Dictionary();
|
||||
};
|
||||
|
||||
#endif // DICTIONARY_H
|
624
sfw/object/old/variant.cpp
Normal file
624
sfw/object/old/variant.cpp
Normal file
@ -0,0 +1,624 @@
|
||||
#include "variant.h"
|
||||
|
||||
#include "math.h"
|
||||
#include "core/reference.h"
|
||||
|
||||
Variant::Type Variant::get_type() const {
|
||||
return _type;
|
||||
}
|
||||
|
||||
void Variant::clear() {
|
||||
switch (_type) {
|
||||
case TYPE_NULL:
|
||||
break;
|
||||
case TYPE_BOOL:
|
||||
_bool = false;
|
||||
break;
|
||||
case TYPE_INT:
|
||||
_int = 0;
|
||||
break;
|
||||
case TYPE_UINT:
|
||||
_uint = 0;
|
||||
break;
|
||||
case TYPE_FLOAT:
|
||||
_float = 0;
|
||||
break;
|
||||
case TYPE_STRING:
|
||||
if (_string->owner) {
|
||||
delete _string->string;
|
||||
}
|
||||
|
||||
delete _string;
|
||||
_string = nullptr;
|
||||
|
||||
break;
|
||||
case TYPE_OBJECT:
|
||||
delete _object;
|
||||
_object = nullptr;
|
||||
|
||||
break;
|
||||
case TYPE_POINTER:
|
||||
_pointer = nullptr;
|
||||
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
_type = TYPE_NULL;
|
||||
}
|
||||
void Variant::zero() {
|
||||
switch (_type) {
|
||||
case TYPE_NULL:
|
||||
break;
|
||||
case TYPE_BOOL:
|
||||
_bool = false;
|
||||
break;
|
||||
case TYPE_INT:
|
||||
_int = 0;
|
||||
break;
|
||||
case TYPE_UINT:
|
||||
_uint = 0;
|
||||
break;
|
||||
case TYPE_FLOAT:
|
||||
_float = 0;
|
||||
break;
|
||||
case TYPE_STRING:
|
||||
_string->string->resize(0);
|
||||
break;
|
||||
case TYPE_OBJECT:
|
||||
_object->object = nullptr;
|
||||
_object->reference.unref();
|
||||
break;
|
||||
case TYPE_POINTER:
|
||||
_pointer = nullptr;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Variant::parse(const String &str) {
|
||||
if (str.is_int()) {
|
||||
set_int(str.to_int());
|
||||
return;
|
||||
}
|
||||
|
||||
if (str.is_uint()) {
|
||||
set_uint(str.to_uint());
|
||||
return;
|
||||
}
|
||||
|
||||
if (str.is_numeric()) {
|
||||
set_float(str.to_float());
|
||||
return;
|
||||
}
|
||||
|
||||
if (str.is_bool()) {
|
||||
set_bool(str.to_bool());
|
||||
return;
|
||||
}
|
||||
|
||||
set_string(str);
|
||||
}
|
||||
Variant Variant::parse_string(const String &str) {
|
||||
Variant v = Variant();
|
||||
|
||||
v.parse(str);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
bool Variant::is_null() const {
|
||||
return _type == TYPE_NULL;
|
||||
}
|
||||
bool Variant::is_bool() const {
|
||||
return _type == TYPE_BOOL;
|
||||
}
|
||||
bool Variant::is_int() const {
|
||||
return _type == TYPE_INT;
|
||||
}
|
||||
bool Variant::is_uint() const {
|
||||
return _type == TYPE_UINT;
|
||||
}
|
||||
bool Variant::is_float() const {
|
||||
return _type == TYPE_FLOAT;
|
||||
}
|
||||
bool Variant::is_numeric() const {
|
||||
return _type == TYPE_INT || _type == TYPE_UINT || _type == TYPE_FLOAT;
|
||||
}
|
||||
bool Variant::is_string() const {
|
||||
return _type == TYPE_STRING;
|
||||
}
|
||||
bool Variant::is_object() const {
|
||||
return _type == TYPE_OBJECT;
|
||||
}
|
||||
bool Variant::is_pointer() const {
|
||||
return _type == TYPE_POINTER;
|
||||
}
|
||||
bool Variant::is_reference() const {
|
||||
if (_type == TYPE_OBJECT) {
|
||||
return _object->reference.is_valid();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Variant::is_primitive_type() const {
|
||||
return _type == TYPE_BOOL || _type == TYPE_INT || _type == TYPE_UINT || _type == TYPE_FLOAT;
|
||||
}
|
||||
|
||||
bool Variant::is_simple_type() const {
|
||||
return _type == TYPE_BOOL || _type == TYPE_INT || _type == TYPE_UINT || _type == TYPE_FLOAT || _type == TYPE_STRING;
|
||||
}
|
||||
|
||||
bool Variant::to_bool() const {
|
||||
return _bool;
|
||||
}
|
||||
int Variant::to_int() const {
|
||||
switch (_type) {
|
||||
case TYPE_NULL:
|
||||
return 0;
|
||||
case TYPE_BOOL:
|
||||
if (_bool) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
case TYPE_INT:
|
||||
case TYPE_UINT:
|
||||
return _int;
|
||||
case TYPE_FLOAT:
|
||||
return static_cast<float>(_int);
|
||||
case TYPE_STRING:
|
||||
return _string->string->to_int();
|
||||
case TYPE_OBJECT:
|
||||
case TYPE_POINTER:
|
||||
// just read the value of the pointer as int
|
||||
// Could return 1 or 0, but this is almost the same, but hopefully it's more useful
|
||||
return _int;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
uint64_t Variant::to_uint() const {
|
||||
switch (_type) {
|
||||
case TYPE_NULL:
|
||||
return 0;
|
||||
case TYPE_BOOL:
|
||||
if (_bool) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
case TYPE_INT:
|
||||
case TYPE_UINT:
|
||||
return _uint;
|
||||
case TYPE_FLOAT:
|
||||
return static_cast<uint64_t>(_float);
|
||||
case TYPE_STRING:
|
||||
return _string->string->to_uint();
|
||||
case TYPE_OBJECT:
|
||||
case TYPE_POINTER:
|
||||
// just read the value of the pointer as uint
|
||||
// Could return 1 or 0, but this is almost the same, but hopefully it's more useful
|
||||
return _uint;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
float Variant::to_float() const {
|
||||
switch (_type) {
|
||||
case TYPE_NULL:
|
||||
return 0;
|
||||
case TYPE_BOOL:
|
||||
if (_bool) {
|
||||
return 0;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
case TYPE_INT:
|
||||
return static_cast<float>(_int);
|
||||
case TYPE_UINT:
|
||||
return static_cast<float>(_uint);
|
||||
case TYPE_FLOAT:
|
||||
return _float;
|
||||
case TYPE_STRING:
|
||||
return _string->string->to_float();
|
||||
case TYPE_OBJECT:
|
||||
case TYPE_POINTER:
|
||||
if (_uint) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
String Variant::to_string() const {
|
||||
switch (_type) {
|
||||
case TYPE_NULL:
|
||||
return "NULL";
|
||||
case TYPE_BOOL:
|
||||
if (Math::is_zero_approx(_float)) {
|
||||
return "false";
|
||||
} else {
|
||||
return "true";
|
||||
}
|
||||
case TYPE_INT:
|
||||
return String::num(_int);
|
||||
case TYPE_UINT:
|
||||
return String::num(_uint);
|
||||
case TYPE_FLOAT:
|
||||
return String::num(_float);
|
||||
case TYPE_STRING:
|
||||
return *(_string->string);
|
||||
case TYPE_OBJECT:
|
||||
case TYPE_POINTER:
|
||||
if (_uint) {
|
||||
return "[ Object ]";
|
||||
} else {
|
||||
return "[ Object (NULL) ]";
|
||||
}
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
Object *Variant::to_object() const {
|
||||
switch (_type) {
|
||||
case TYPE_NULL:
|
||||
return nullptr;
|
||||
case TYPE_BOOL:
|
||||
return nullptr;
|
||||
case TYPE_INT:
|
||||
return nullptr;
|
||||
case TYPE_UINT:
|
||||
return nullptr;
|
||||
case TYPE_FLOAT:
|
||||
return nullptr;
|
||||
case TYPE_STRING:
|
||||
return nullptr;
|
||||
case TYPE_OBJECT:
|
||||
return _object->object;
|
||||
case TYPE_POINTER:
|
||||
return nullptr;
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
Reference *Variant::to_reference() const {
|
||||
switch (_type) {
|
||||
case TYPE_NULL:
|
||||
return nullptr;
|
||||
case TYPE_BOOL:
|
||||
return nullptr;
|
||||
case TYPE_INT:
|
||||
return nullptr;
|
||||
case TYPE_UINT:
|
||||
return nullptr;
|
||||
case TYPE_FLOAT:
|
||||
return nullptr;
|
||||
case TYPE_STRING:
|
||||
return nullptr;
|
||||
case TYPE_OBJECT:
|
||||
return Object::cast_to<Reference>(_object->object);
|
||||
case TYPE_POINTER:
|
||||
return nullptr;
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
void *Variant::to_pointer() const {
|
||||
switch (_type) {
|
||||
case TYPE_NULL:
|
||||
return nullptr;
|
||||
case TYPE_BOOL:
|
||||
return nullptr;
|
||||
case TYPE_INT:
|
||||
return nullptr;
|
||||
case TYPE_UINT:
|
||||
return nullptr;
|
||||
case TYPE_FLOAT:
|
||||
return nullptr;
|
||||
case TYPE_STRING:
|
||||
return nullptr;
|
||||
case TYPE_OBJECT:
|
||||
return nullptr;
|
||||
case TYPE_POINTER:
|
||||
return _pointer;
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
String *Variant::get_string_ptr() const {
|
||||
if (_type == TYPE_STRING) {
|
||||
return _string->string;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
bool Variant::is_string_owned() const {
|
||||
if (_type == TYPE_STRING) {
|
||||
return _string->owner;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void Variant::set_null() {
|
||||
clear();
|
||||
|
||||
_type = TYPE_NULL;
|
||||
}
|
||||
void Variant::set_bool(const bool value) {
|
||||
clear();
|
||||
|
||||
_type = TYPE_BOOL;
|
||||
_bool = value;
|
||||
}
|
||||
void Variant::set_int(const int value) {
|
||||
clear();
|
||||
|
||||
_type = TYPE_INT;
|
||||
_int = value;
|
||||
}
|
||||
void Variant::set_uint(const uint64_t value) {
|
||||
clear();
|
||||
|
||||
_type = TYPE_UINT;
|
||||
_uint = value;
|
||||
}
|
||||
void Variant::set_float(const float value) {
|
||||
clear();
|
||||
|
||||
_type = TYPE_FLOAT;
|
||||
_float = value;
|
||||
}
|
||||
void Variant::set_float(const double value) {
|
||||
clear();
|
||||
|
||||
_type = TYPE_FLOAT;
|
||||
_float = value;
|
||||
}
|
||||
void Variant::set_string(String *value, const bool copy) {
|
||||
clear();
|
||||
|
||||
if (!value) {
|
||||
return;
|
||||
}
|
||||
|
||||
_type = TYPE_STRING;
|
||||
|
||||
_string = new StringData();
|
||||
|
||||
if (copy) {
|
||||
_string->string = new String(*value);
|
||||
_string->owner = true;
|
||||
} else {
|
||||
_string->string = value;
|
||||
_string->owner = false;
|
||||
}
|
||||
}
|
||||
void Variant::set_string(const String &value, const bool copy) {
|
||||
clear();
|
||||
|
||||
_type = TYPE_STRING;
|
||||
|
||||
_string = new StringData();
|
||||
|
||||
if (copy) {
|
||||
_string->string = new String(value);
|
||||
_string->owner = true;
|
||||
} else {
|
||||
_string->string = &const_cast<String &>(value);
|
||||
_string->owner = false;
|
||||
}
|
||||
}
|
||||
void Variant::set_object(Object *value) {
|
||||
clear();
|
||||
|
||||
if (!value) {
|
||||
return;
|
||||
}
|
||||
|
||||
_object = new ObjectData();
|
||||
_object->object = value;
|
||||
|
||||
if (value->is_class_ptr(Reference::get_class_ptr_static())) {
|
||||
_object->reference = Ref<Reference>(Object::cast_to<Reference>(value));
|
||||
}
|
||||
}
|
||||
void Variant::set_pointer(void *value) {
|
||||
clear();
|
||||
|
||||
_type = TYPE_POINTER;
|
||||
_pointer = value;
|
||||
}
|
||||
void Variant::set_variant(const Variant &value) {
|
||||
clear();
|
||||
|
||||
switch (value._type) {
|
||||
case TYPE_NULL:
|
||||
break;
|
||||
case TYPE_BOOL:
|
||||
_bool = value._bool;
|
||||
break;
|
||||
case TYPE_INT:
|
||||
_int = value._int;
|
||||
break;
|
||||
case TYPE_UINT:
|
||||
_uint = value._uint;
|
||||
break;
|
||||
case TYPE_FLOAT:
|
||||
_float = value._float;
|
||||
break;
|
||||
case TYPE_STRING:
|
||||
set_string(value._string->string, true);
|
||||
break;
|
||||
case TYPE_OBJECT:
|
||||
set_object(value._object->object);
|
||||
break;
|
||||
case TYPE_POINTER:
|
||||
_pointer = value._pointer;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
_type = value._type;
|
||||
}
|
||||
|
||||
Variant::operator bool() const {
|
||||
return to_bool();
|
||||
}
|
||||
Variant::operator int() const {
|
||||
return to_int();
|
||||
}
|
||||
Variant::operator uint64_t() const {
|
||||
return to_uint();
|
||||
}
|
||||
Variant::operator float() const {
|
||||
return to_float();
|
||||
}
|
||||
Variant::operator double() const {
|
||||
return to_float();
|
||||
}
|
||||
Variant::operator String() const {
|
||||
return to_string();
|
||||
}
|
||||
Variant::operator Object *() const {
|
||||
return to_object();
|
||||
}
|
||||
Variant::operator Reference *() const {
|
||||
return to_reference();
|
||||
}
|
||||
Variant::operator void *() const {
|
||||
return to_pointer();
|
||||
}
|
||||
|
||||
void Variant::operator=(const Variant &other) {
|
||||
set_variant(other);
|
||||
}
|
||||
bool Variant::operator==(const Variant &other) const {
|
||||
if (_type != other._type) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (_type) {
|
||||
case TYPE_NULL:
|
||||
return true;
|
||||
case TYPE_BOOL:
|
||||
return _bool == other._bool;
|
||||
case TYPE_INT:
|
||||
return _int == other._int;
|
||||
case TYPE_UINT:
|
||||
return _uint == other._uint;
|
||||
case TYPE_FLOAT:
|
||||
return _float == other._float;
|
||||
case TYPE_STRING:
|
||||
return (*_string->string) == (*other._string->string);
|
||||
case TYPE_OBJECT:
|
||||
return (_object->object) == (other._object->object);
|
||||
case TYPE_POINTER:
|
||||
return _pointer == other._pointer;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
bool Variant::operator!=(const Variant &other) const {
|
||||
return !(operator==(other));
|
||||
}
|
||||
bool Variant::operator<(const Variant &other) const {
|
||||
switch (_type) {
|
||||
case TYPE_NULL: {
|
||||
if (other.is_null()) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
case TYPE_BOOL: {
|
||||
return _bool < other.to_bool();
|
||||
}
|
||||
case TYPE_INT:
|
||||
return _int < other.to_int();
|
||||
return _int == other._int;
|
||||
case TYPE_UINT:
|
||||
return _uint < other.to_uint();
|
||||
return _uint == other._uint;
|
||||
case TYPE_FLOAT:
|
||||
return _float < other.to_float();
|
||||
return _float == other._float;
|
||||
case TYPE_STRING:
|
||||
return (*_string->string) < other.to_string();
|
||||
case TYPE_OBJECT:
|
||||
return (_object->object) < other.to_object();
|
||||
case TYPE_POINTER:
|
||||
return _pointer < other.to_pointer();
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Variant::Variant(const bool value) {
|
||||
_type = TYPE_BOOL;
|
||||
|
||||
_bool = value;
|
||||
}
|
||||
Variant::Variant(const int value) {
|
||||
_type = TYPE_INT;
|
||||
|
||||
_int = value;
|
||||
}
|
||||
Variant::Variant(const uint64_t value) {
|
||||
_type = TYPE_UINT;
|
||||
|
||||
_uint = value;
|
||||
}
|
||||
Variant::Variant(const float value) {
|
||||
_type = TYPE_FLOAT;
|
||||
|
||||
_float = value;
|
||||
}
|
||||
Variant::Variant(const double value) {
|
||||
_type = TYPE_FLOAT;
|
||||
|
||||
_float = value;
|
||||
}
|
||||
Variant::Variant(String *value, bool copy) {
|
||||
_type = TYPE_NULL;
|
||||
|
||||
set_string(value, copy);
|
||||
}
|
||||
Variant::Variant(const String &value, bool copy) {
|
||||
_type = TYPE_NULL;
|
||||
|
||||
set_string(value, copy);
|
||||
}
|
||||
Variant::Variant(Object *value) {
|
||||
_type = TYPE_NULL;
|
||||
|
||||
set_object(value);
|
||||
}
|
||||
Variant::Variant(void *value) {
|
||||
_type = TYPE_NULL;
|
||||
|
||||
set_pointer(value);
|
||||
}
|
||||
Variant::Variant(const Variant &value) {
|
||||
_type = TYPE_NULL;
|
||||
|
||||
set_variant(value);
|
||||
}
|
||||
|
||||
Variant::Variant() {
|
||||
_type = TYPE_NULL;
|
||||
}
|
||||
|
||||
Variant::~Variant() {
|
||||
clear();
|
||||
}
|
120
sfw/object/old/variant.h
Normal file
120
sfw/object/old/variant.h
Normal file
@ -0,0 +1,120 @@
|
||||
#ifndef VARIANT_H
|
||||
#define VARIANT_H
|
||||
|
||||
#include "core/object.h"
|
||||
#include "core/reference.h"
|
||||
#include "core/string.h"
|
||||
|
||||
class Variant {
|
||||
|
||||
public:
|
||||
enum Type {
|
||||
TYPE_NULL = 0,
|
||||
TYPE_BOOL,
|
||||
TYPE_INT,
|
||||
TYPE_UINT,
|
||||
TYPE_FLOAT,
|
||||
TYPE_STRING,
|
||||
TYPE_OBJECT,
|
||||
TYPE_POINTER,
|
||||
};
|
||||
|
||||
Type get_type() const;
|
||||
|
||||
void clear();
|
||||
void zero();
|
||||
|
||||
void parse(const String &str);
|
||||
static Variant parse_string(const String &str);
|
||||
|
||||
bool is_null() const;
|
||||
bool is_bool() const;
|
||||
bool is_int() const;
|
||||
bool is_uint() const;
|
||||
bool is_float() const;
|
||||
bool is_numeric() const;
|
||||
bool is_string() const;
|
||||
bool is_object() const;
|
||||
bool is_pointer() const;
|
||||
bool is_reference() const;
|
||||
|
||||
bool is_primitive_type() const;
|
||||
bool is_simple_type() const;
|
||||
|
||||
bool to_bool() const;
|
||||
int to_int() const;
|
||||
uint64_t to_uint() const;
|
||||
float to_float() const;
|
||||
String to_string() const;
|
||||
Object *to_object() const;
|
||||
Reference *to_reference() const;
|
||||
void *to_pointer() const;
|
||||
String *get_string_ptr() const;
|
||||
bool is_string_owned() const;
|
||||
|
||||
void set_null();
|
||||
void set_bool(const bool value);
|
||||
void set_int(const int value);
|
||||
void set_uint(const uint64_t value);
|
||||
void set_float(const float value);
|
||||
void set_float(const double value);
|
||||
void set_string(String *value, bool copy = false);
|
||||
void set_string(const String &value, bool copy = true);
|
||||
void set_object(Object *value);
|
||||
void set_pointer(void *value);
|
||||
void set_variant(const Variant &value);
|
||||
|
||||
operator bool() const;
|
||||
operator int() const;
|
||||
operator uint64_t() const;
|
||||
operator float() const;
|
||||
operator double() const;
|
||||
operator String() const;
|
||||
operator Object *() const;
|
||||
operator Reference *() const;
|
||||
operator void *() const;
|
||||
|
||||
void operator=(const Variant &other);
|
||||
bool operator==(const Variant &other) const;
|
||||
bool operator!=(const Variant &other) const;
|
||||
bool operator<(const Variant &other) const;
|
||||
|
||||
Variant(const bool value);
|
||||
Variant(const int value);
|
||||
Variant(const uint64_t value);
|
||||
Variant(const float value);
|
||||
Variant(const double value);
|
||||
Variant(String *value, const bool copy = false);
|
||||
Variant(const String &value, const bool copy = true);
|
||||
Variant(Object *value);
|
||||
Variant(void *value);
|
||||
Variant(const Variant &value);
|
||||
|
||||
Variant();
|
||||
~Variant();
|
||||
|
||||
private:
|
||||
struct StringData {
|
||||
bool owner;
|
||||
String *string;
|
||||
};
|
||||
|
||||
struct ObjectData {
|
||||
Object *object;
|
||||
Ref<Reference> reference;
|
||||
};
|
||||
|
||||
union {
|
||||
bool _bool;
|
||||
int _int;
|
||||
uint64_t _uint;
|
||||
float _float;
|
||||
StringData *_string;
|
||||
ObjectData *_object;
|
||||
void *_pointer;
|
||||
};
|
||||
|
||||
Type _type;
|
||||
};
|
||||
|
||||
#endif
|
File diff suppressed because it is too large
Load Diff
@ -1,120 +1,500 @@
|
||||
#ifndef VARIANT_H
|
||||
#define VARIANT_H
|
||||
|
||||
#include "core/object.h"
|
||||
#include "core/reference.h"
|
||||
#include "core/string.h"
|
||||
/*************************************************************************/
|
||||
/* variant.h */
|
||||
/* From https://github.com/Relintai/pandemonium_engine (MIT) */
|
||||
/*************************************************************************/
|
||||
|
||||
#include "core/containers/pool_vector.h"
|
||||
#include "core/containers/rid.h"
|
||||
#include "core/io/ip_address.h"
|
||||
#include "core/math/aabb.h"
|
||||
#include "core/math/basis.h"
|
||||
#include "core/math/color.h"
|
||||
#include "core/math/face3.h"
|
||||
#include "core/math/plane.h"
|
||||
#include "core/math/projection.h"
|
||||
#include "core/math/quaternion.h"
|
||||
#include "core/math/transform.h"
|
||||
#include "core/math/transform_2d.h"
|
||||
#include "core/math/vector3.h"
|
||||
#include "core/math/vector3i.h"
|
||||
#include "core/math/vector4.h"
|
||||
#include "core/math/vector4i.h"
|
||||
#include "core/object/object_id.h"
|
||||
#include "core/object/ref_ptr.h"
|
||||
#include "core/string/node_path.h"
|
||||
#include "core/string/ustring.h"
|
||||
#include "core/variant/array.h"
|
||||
#include "core/variant/dictionary.h"
|
||||
|
||||
class Object;
|
||||
class ObjectRC;
|
||||
class Node; // helper
|
||||
class Control; // helper
|
||||
|
||||
struct PropertyInfo;
|
||||
struct MethodInfo;
|
||||
|
||||
typedef PoolVector<uint8_t> PoolByteArray;
|
||||
typedef PoolVector<int> PoolIntArray;
|
||||
typedef PoolVector<real_t> PoolRealArray;
|
||||
typedef PoolVector<String> PoolStringArray;
|
||||
typedef PoolVector<Vector2> PoolVector2Array;
|
||||
typedef PoolVector<Vector2i> PoolVector2iArray;
|
||||
typedef PoolVector<Vector3> PoolVector3Array;
|
||||
typedef PoolVector<Vector3i> PoolVector3iArray;
|
||||
typedef PoolVector<Vector4> PoolVector4Array;
|
||||
typedef PoolVector<Vector4i> PoolVector4iArray;
|
||||
typedef PoolVector<Color> PoolColorArray;
|
||||
|
||||
// Temporary workaround until c++11 alignas()
|
||||
#ifdef __GNUC__
|
||||
#define GCC_ALIGNED_8 __attribute__((aligned(8)))
|
||||
#else
|
||||
#define GCC_ALIGNED_8
|
||||
#endif
|
||||
|
||||
#define _REF_OBJ_PTR(m_variant) (reinterpret_cast<Ref<Reference> *>((m_variant)._get_obj().ref.get_data())->ptr())
|
||||
#define _OBJ_PTR(m_variant) ((m_variant)._get_obj().rc ? (m_variant)._get_obj().rc->get_ptr() : _REF_OBJ_PTR(m_variant))
|
||||
// _UNSAFE_OBJ_PROXY_PTR is needed for comparing an object Variant against NIL or compare two object Variants.
|
||||
// It's guaranteed to be unique per object, in contrast to the pointer stored in the RC structure,
|
||||
// which is set to null when the object is destroyed.
|
||||
#define _UNSAFE_OBJ_PROXY_PTR(m_variant) ((m_variant)._get_obj().rc ? reinterpret_cast<uint8_t *>((m_variant)._get_obj().rc) : reinterpret_cast<uint8_t *>(_REF_OBJ_PTR(m_variant)))
|
||||
|
||||
class Variant {
|
||||
|
||||
public:
|
||||
// If this changes the table in variant_op must be updated
|
||||
enum Type {
|
||||
TYPE_NULL = 0,
|
||||
TYPE_BOOL,
|
||||
TYPE_INT,
|
||||
TYPE_UINT,
|
||||
TYPE_FLOAT,
|
||||
TYPE_STRING,
|
||||
TYPE_OBJECT,
|
||||
TYPE_POINTER,
|
||||
|
||||
NIL,
|
||||
|
||||
// atomic types
|
||||
BOOL,
|
||||
INT,
|
||||
REAL,
|
||||
STRING,
|
||||
|
||||
// math types
|
||||
RECT2, // 5
|
||||
RECT2I,
|
||||
VECTOR2,
|
||||
VECTOR2I,
|
||||
VECTOR3,
|
||||
VECTOR3I, // 10
|
||||
VECTOR4,
|
||||
VECTOR4I,
|
||||
|
||||
PLANE,
|
||||
QUATERNION,
|
||||
AABB, // 15
|
||||
BASIS,
|
||||
TRANSFORM,
|
||||
TRANSFORM2D,
|
||||
PROJECTION,
|
||||
|
||||
// misc types
|
||||
COLOR, // 20
|
||||
NODE_PATH,
|
||||
RID,
|
||||
OBJECT,
|
||||
STRING_NAME,
|
||||
DICTIONARY, // 25
|
||||
ARRAY,
|
||||
|
||||
// arrays
|
||||
POOL_BYTE_ARRAY,
|
||||
POOL_INT_ARRAY,
|
||||
POOL_REAL_ARRAY,
|
||||
POOL_STRING_ARRAY, //30
|
||||
POOL_VECTOR2_ARRAY,
|
||||
POOL_VECTOR2I_ARRAY,
|
||||
POOL_VECTOR3_ARRAY,
|
||||
POOL_VECTOR3I_ARRAY,
|
||||
POOL_VECTOR4_ARRAY, //35
|
||||
POOL_VECTOR4I_ARRAY,
|
||||
POOL_COLOR_ARRAY,
|
||||
|
||||
VARIANT_MAX // 38
|
||||
|
||||
};
|
||||
|
||||
Type get_type() const;
|
||||
|
||||
void clear();
|
||||
void zero();
|
||||
|
||||
void parse(const String &str);
|
||||
static Variant parse_string(const String &str);
|
||||
|
||||
bool is_null() const;
|
||||
bool is_bool() const;
|
||||
bool is_int() const;
|
||||
bool is_uint() const;
|
||||
bool is_float() const;
|
||||
bool is_numeric() const;
|
||||
bool is_string() const;
|
||||
bool is_object() const;
|
||||
bool is_pointer() const;
|
||||
bool is_reference() const;
|
||||
|
||||
bool is_primitive_type() const;
|
||||
bool is_simple_type() const;
|
||||
|
||||
bool to_bool() const;
|
||||
int to_int() const;
|
||||
uint64_t to_uint() const;
|
||||
float to_float() const;
|
||||
String to_string() const;
|
||||
Object *to_object() const;
|
||||
Reference *to_reference() const;
|
||||
void *to_pointer() const;
|
||||
String *get_string_ptr() const;
|
||||
bool is_string_owned() const;
|
||||
|
||||
void set_null();
|
||||
void set_bool(const bool value);
|
||||
void set_int(const int value);
|
||||
void set_uint(const uint64_t value);
|
||||
void set_float(const float value);
|
||||
void set_float(const double value);
|
||||
void set_string(String *value, bool copy = false);
|
||||
void set_string(const String &value, bool copy = true);
|
||||
void set_object(Object *value);
|
||||
void set_pointer(void *value);
|
||||
void set_variant(const Variant &value);
|
||||
|
||||
operator bool() const;
|
||||
operator int() const;
|
||||
operator uint64_t() const;
|
||||
operator float() const;
|
||||
operator double() const;
|
||||
operator String() const;
|
||||
operator Object *() const;
|
||||
operator Reference *() const;
|
||||
operator void *() const;
|
||||
|
||||
void operator=(const Variant &other);
|
||||
bool operator==(const Variant &other) const;
|
||||
bool operator!=(const Variant &other) const;
|
||||
bool operator<(const Variant &other) const;
|
||||
|
||||
Variant(const bool value);
|
||||
Variant(const int value);
|
||||
Variant(const uint64_t value);
|
||||
Variant(const float value);
|
||||
Variant(const double value);
|
||||
Variant(String *value, const bool copy = false);
|
||||
Variant(const String &value, const bool copy = true);
|
||||
Variant(Object *value);
|
||||
Variant(void *value);
|
||||
Variant(const Variant &value);
|
||||
|
||||
Variant();
|
||||
~Variant();
|
||||
enum {
|
||||
// Maximum recursion depth allowed when serializing variants.
|
||||
MAX_RECURSION_DEPTH = 1024,
|
||||
};
|
||||
|
||||
private:
|
||||
struct StringData {
|
||||
bool owner;
|
||||
String *string;
|
||||
friend struct _VariantCall;
|
||||
// Variant takes 20 bytes when real_t is float, and 36 if double
|
||||
// it only allocates extra memory for aabb/matrix.
|
||||
|
||||
Type type;
|
||||
|
||||
struct ObjData {
|
||||
// Will be null for every type deriving from Reference as they have their
|
||||
// own reference count mechanism
|
||||
ObjectRC *rc;
|
||||
// Always initialized, but will be null if the Ref<> assigned was null
|
||||
// or this Variant is not even holding a Reference-derived object
|
||||
RefPtr ref;
|
||||
};
|
||||
|
||||
struct ObjectData {
|
||||
Object *object;
|
||||
Ref<Reference> reference;
|
||||
};
|
||||
_FORCE_INLINE_ ObjData &_get_obj();
|
||||
_FORCE_INLINE_ const ObjData &_get_obj() const;
|
||||
|
||||
union {
|
||||
bool _bool;
|
||||
int _int;
|
||||
uint64_t _uint;
|
||||
float _float;
|
||||
StringData *_string;
|
||||
ObjectData *_object;
|
||||
void *_pointer;
|
||||
int64_t _int;
|
||||
double _real;
|
||||
Transform2D *_transform2d;
|
||||
::AABB *_aabb;
|
||||
Basis *_basis;
|
||||
Transform *_transform;
|
||||
Projection *_projection;
|
||||
void *_ptr; //generic pointer
|
||||
uint8_t _mem[sizeof(ObjData) > (sizeof(real_t) * 4) ? sizeof(ObjData) : (sizeof(real_t) * 4)];
|
||||
} _data GCC_ALIGNED_8;
|
||||
|
||||
void reference(const Variant &p_variant);
|
||||
void clear();
|
||||
|
||||
public:
|
||||
_FORCE_INLINE_ Type get_type() const { return type; }
|
||||
static String get_type_name(Variant::Type p_type);
|
||||
static bool can_convert(Type p_type_from, Type p_type_to);
|
||||
static bool can_convert_strict(Type p_type_from, Type p_type_to);
|
||||
|
||||
bool is_ref() const;
|
||||
_FORCE_INLINE_ bool is_num() const { return type == INT || type == REAL; };
|
||||
_FORCE_INLINE_ bool is_array() const { return type >= ARRAY; };
|
||||
_FORCE_INLINE_ bool is_null() const { return type == NIL; };
|
||||
bool is_shared() const;
|
||||
bool is_zero() const;
|
||||
bool is_one() const;
|
||||
|
||||
ObjectID get_object_instance_id() const;
|
||||
bool is_invalid_object() const;
|
||||
|
||||
operator bool() const;
|
||||
operator signed int() const;
|
||||
operator unsigned int() const; // this is the real one
|
||||
operator signed short() const;
|
||||
operator unsigned short() const;
|
||||
operator signed char() const;
|
||||
operator unsigned char() const;
|
||||
//operator long unsigned int() const;
|
||||
operator int64_t() const;
|
||||
operator uint64_t() const;
|
||||
#ifdef NEED_LONG_INT
|
||||
operator signed long() const;
|
||||
operator unsigned long() const;
|
||||
#endif
|
||||
|
||||
operator CharType() const;
|
||||
operator float() const;
|
||||
operator double() const;
|
||||
operator String() const;
|
||||
operator StringName() const;
|
||||
operator Rect2() const;
|
||||
operator Rect2i() const;
|
||||
operator Vector2() const;
|
||||
operator Vector2i() const;
|
||||
operator Vector3() const;
|
||||
operator Vector3i() const;
|
||||
operator Vector4() const;
|
||||
operator Vector4i() const;
|
||||
operator Plane() const;
|
||||
operator ::AABB() const;
|
||||
operator Quaternion() const;
|
||||
operator Basis() const;
|
||||
operator Transform() const;
|
||||
operator Transform2D() const;
|
||||
operator Projection() const;
|
||||
|
||||
operator Color() const;
|
||||
operator NodePath() const;
|
||||
operator RefPtr() const;
|
||||
operator ::RID() const;
|
||||
|
||||
operator Object *() const;
|
||||
operator Node *() const;
|
||||
operator Control *() const;
|
||||
|
||||
operator Dictionary() const;
|
||||
operator Array() const;
|
||||
|
||||
operator PoolVector<uint8_t>() const;
|
||||
operator PoolVector<int>() const;
|
||||
operator PoolVector<real_t>() const;
|
||||
operator PoolVector<String>() const;
|
||||
operator PoolVector<Vector2>() const;
|
||||
operator PoolVector<Vector2i>() const;
|
||||
operator PoolVector<Vector3>() const;
|
||||
operator PoolVector<Vector3i>() const;
|
||||
operator PoolVector<Vector4>() const;
|
||||
operator PoolVector<Vector4i>() const;
|
||||
operator PoolVector<Color>() const;
|
||||
operator PoolVector<Plane>() const;
|
||||
operator PoolVector<Face3>() const;
|
||||
|
||||
operator Vector<Variant>() const;
|
||||
operator Vector<uint8_t>() const;
|
||||
operator Vector<int>() const;
|
||||
operator Vector<real_t>() const;
|
||||
operator Vector<String>() const;
|
||||
operator Vector<StringName>() const;
|
||||
operator Vector<Vector3>() const;
|
||||
operator Vector<Vector3i>() const;
|
||||
operator Vector<Vector4>() const;
|
||||
operator Vector<Vector4i>() const;
|
||||
operator Vector<Color>() const;
|
||||
operator Vector<::RID>() const;
|
||||
operator Vector<Vector2>() const;
|
||||
operator Vector<Vector2i>() const;
|
||||
|
||||
operator Vector<Plane>() const;
|
||||
|
||||
// some core type enums to convert to
|
||||
operator Margin() const;
|
||||
operator Side() const;
|
||||
operator Orientation() const;
|
||||
|
||||
operator IP_Address() const;
|
||||
|
||||
Variant(bool p_bool);
|
||||
Variant(signed int p_int); // real one
|
||||
Variant(unsigned int p_int);
|
||||
#ifdef NEED_LONG_INT
|
||||
Variant(signed long p_long); // real one
|
||||
Variant(unsigned long p_long);
|
||||
//Variant(long unsigned int p_long);
|
||||
#endif
|
||||
Variant(signed short p_short); // real one
|
||||
Variant(unsigned short p_short);
|
||||
Variant(signed char p_char); // real one
|
||||
Variant(unsigned char p_char);
|
||||
Variant(int64_t p_int); // real one
|
||||
Variant(uint64_t p_int);
|
||||
Variant(float p_float);
|
||||
Variant(double p_double);
|
||||
Variant(const String &p_string);
|
||||
Variant(const StringName &p_string);
|
||||
Variant(const char *const p_cstring);
|
||||
Variant(const CharType *p_wstring);
|
||||
Variant(const Vector2 &p_vector2);
|
||||
Variant(const Vector2i &p_vector2);
|
||||
Variant(const Rect2 &p_rect2);
|
||||
Variant(const Rect2i &p_rect2);
|
||||
Variant(const Vector3 &p_vector3);
|
||||
Variant(const Vector3i &p_vector3);
|
||||
Variant(const Vector4 &p_vector4);
|
||||
Variant(const Vector4i &p_vector4);
|
||||
Variant(const Projection &p_projection);
|
||||
Variant(const Plane &p_plane);
|
||||
Variant(const ::AABB &p_aabb);
|
||||
Variant(const Quaternion &p_quat);
|
||||
Variant(const Basis &p_matrix);
|
||||
Variant(const Transform2D &p_transform);
|
||||
Variant(const Transform &p_transform);
|
||||
Variant(const Color &p_color);
|
||||
Variant(const NodePath &p_node_path);
|
||||
Variant(const RefPtr &p_resource);
|
||||
Variant(const ::RID &p_rid);
|
||||
Variant(const Object *p_object);
|
||||
Variant(const Dictionary &p_dictionary);
|
||||
|
||||
Variant(const Array &p_array);
|
||||
Variant(const PoolVector<Plane> &p_array);
|
||||
Variant(const PoolVector<uint8_t> &p_raw_array);
|
||||
Variant(const PoolVector<int> &p_int_array);
|
||||
Variant(const PoolVector<real_t> &p_real_array);
|
||||
Variant(const PoolVector<String> &p_string_array);
|
||||
Variant(const PoolVector<Vector3> &p_vector3_array);
|
||||
Variant(const PoolVector<Vector3i> &p_vector3_array);
|
||||
Variant(const PoolVector<Color> &p_color_array);
|
||||
Variant(const PoolVector<Face3> &p_face_array);
|
||||
Variant(const PoolVector<Vector2> &p_vector2_array);
|
||||
Variant(const PoolVector<Vector2i> &p_vector2_array);
|
||||
Variant(const PoolVector<Vector4> &p_vector4_array);
|
||||
Variant(const PoolVector<Vector4i> &p_vector4_array);
|
||||
|
||||
Variant(const Vector<Variant> &p_array);
|
||||
Variant(const Vector<uint8_t> &p_array);
|
||||
Variant(const Vector<int> &p_array);
|
||||
Variant(const Vector<real_t> &p_array);
|
||||
Variant(const Vector<String> &p_array);
|
||||
Variant(const Vector<StringName> &p_array);
|
||||
Variant(const Vector<Vector3> &p_array);
|
||||
Variant(const Vector<Vector3i> &p_array);
|
||||
Variant(const Vector<Color> &p_array);
|
||||
Variant(const Vector<Plane> &p_array);
|
||||
Variant(const Vector<::RID> &p_array);
|
||||
Variant(const Vector<Vector2> &p_array);
|
||||
Variant(const Vector<Vector2i> &p_array);
|
||||
Variant(const Vector<Vector4> &p_array);
|
||||
Variant(const Vector<Vector4i> &p_array);
|
||||
|
||||
Variant(const IP_Address &p_address);
|
||||
|
||||
// If this changes the table in variant_op must be updated
|
||||
enum Operator {
|
||||
|
||||
//comparison
|
||||
OP_EQUAL,
|
||||
OP_NOT_EQUAL,
|
||||
OP_LESS,
|
||||
OP_LESS_EQUAL,
|
||||
OP_GREATER,
|
||||
OP_GREATER_EQUAL,
|
||||
//mathematic
|
||||
OP_ADD,
|
||||
OP_SUBTRACT,
|
||||
OP_MULTIPLY,
|
||||
OP_DIVIDE,
|
||||
OP_NEGATE,
|
||||
OP_POSITIVE,
|
||||
OP_MODULE,
|
||||
OP_STRING_CONCAT,
|
||||
//bitwise
|
||||
OP_SHIFT_LEFT,
|
||||
OP_SHIFT_RIGHT,
|
||||
OP_BIT_AND,
|
||||
OP_BIT_OR,
|
||||
OP_BIT_XOR,
|
||||
OP_BIT_NEGATE,
|
||||
//logic
|
||||
OP_AND,
|
||||
OP_OR,
|
||||
OP_XOR,
|
||||
OP_NOT,
|
||||
//containment
|
||||
OP_IN,
|
||||
OP_MAX
|
||||
|
||||
};
|
||||
|
||||
Type _type;
|
||||
static String get_operator_name(Operator p_op);
|
||||
static void evaluate(const Operator &p_op, const Variant &p_a, const Variant &p_b, Variant &r_ret, bool &r_valid);
|
||||
static _FORCE_INLINE_ Variant evaluate(const Operator &p_op, const Variant &p_a, const Variant &p_b) {
|
||||
bool valid = true;
|
||||
Variant res;
|
||||
evaluate(p_op, p_a, p_b, res, valid);
|
||||
return res;
|
||||
}
|
||||
|
||||
void zero();
|
||||
Variant duplicate(bool deep = false) const;
|
||||
static void blend(const Variant &a, const Variant &b, float c, Variant &r_dst);
|
||||
static void interpolate(const Variant &a, const Variant &b, float c, Variant &r_dst);
|
||||
static void sub(const Variant &a, const Variant &b, Variant &r_dst);
|
||||
|
||||
struct CallError {
|
||||
enum Error {
|
||||
CALL_OK,
|
||||
CALL_ERROR_INVALID_METHOD,
|
||||
CALL_ERROR_INVALID_ARGUMENT,
|
||||
CALL_ERROR_TOO_MANY_ARGUMENTS,
|
||||
CALL_ERROR_TOO_FEW_ARGUMENTS,
|
||||
CALL_ERROR_INSTANCE_IS_NULL,
|
||||
};
|
||||
Error error;
|
||||
int argument;
|
||||
Type expected;
|
||||
};
|
||||
|
||||
void call_ptr(const StringName &p_method, const Variant **p_args, int p_argcount, Variant *r_ret, CallError &r_error);
|
||||
Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, CallError &r_error);
|
||||
Variant call(const StringName &p_method, const Variant &p_arg1 = Variant(), const Variant &p_arg2 = Variant(), const Variant &p_arg3 = Variant(), const Variant &p_arg4 = Variant(), const Variant &p_arg5 = Variant(), const Variant &p_arg6 = Variant(), const Variant &p_arg7 = Variant(), const Variant &p_arg8 = Variant());
|
||||
|
||||
static String get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Variant::CallError &ce);
|
||||
|
||||
static Variant construct(const Variant::Type, const Variant **p_args, int p_argcount, CallError &r_error, bool p_strict = true);
|
||||
|
||||
void get_method_list(List<MethodInfo> *p_list) const;
|
||||
bool has_method(const StringName &p_method) const;
|
||||
static Vector<Variant::Type> get_method_argument_types(Variant::Type p_type, const StringName &p_method);
|
||||
static Vector<Variant> get_method_default_arguments(Variant::Type p_type, const StringName &p_method);
|
||||
static Variant::Type get_method_return_type(Variant::Type p_type, const StringName &p_method, bool *r_has_return = nullptr);
|
||||
static Vector<StringName> get_method_argument_names(Variant::Type p_type, const StringName &p_method);
|
||||
static bool is_method_const(Variant::Type p_type, const StringName &p_method);
|
||||
|
||||
void set_named(const StringName &p_index, const Variant &p_value, bool *r_valid = nullptr);
|
||||
Variant get_named(const StringName &p_index, bool *r_valid = nullptr) const;
|
||||
|
||||
void set(const Variant &p_index, const Variant &p_value, bool *r_valid = nullptr);
|
||||
Variant get(const Variant &p_index, bool *r_valid = nullptr) const;
|
||||
bool in(const Variant &p_index, bool *r_valid = nullptr) const;
|
||||
|
||||
bool iter_init(Variant &r_iter, bool &r_valid) const;
|
||||
bool iter_next(Variant &r_iter, bool &r_valid) const;
|
||||
Variant iter_get(const Variant &r_iter, bool &r_valid) const;
|
||||
|
||||
void get_property_list(List<PropertyInfo> *p_list) const;
|
||||
|
||||
//argsVariant call()
|
||||
|
||||
bool deep_equal(const Variant &p_variant, int p_recursion_count = 0) const;
|
||||
bool operator==(const Variant &p_variant) const;
|
||||
bool operator!=(const Variant &p_variant) const;
|
||||
bool operator<(const Variant &p_variant) const;
|
||||
uint32_t hash() const;
|
||||
uint32_t recursive_hash(int p_recursion_count) const;
|
||||
|
||||
bool hash_compare(const Variant &p_variant) const;
|
||||
bool booleanize() const;
|
||||
String stringify(List<const void *> &stack) const;
|
||||
|
||||
void static_assign(const Variant &p_variant);
|
||||
static void get_constructor_list(Variant::Type p_type, List<MethodInfo> *p_list);
|
||||
static void get_constants_for_type(Variant::Type p_type, List<StringName> *p_constants);
|
||||
static bool has_constant(Variant::Type p_type, const StringName &p_value);
|
||||
static Variant get_constant_value(Variant::Type p_type, const StringName &p_value, bool *r_valid = nullptr);
|
||||
|
||||
typedef String (*ObjectDeConstruct)(const Variant &p_object, void *ud);
|
||||
typedef void (*ObjectConstruct)(const String &p_text, void *ud, Variant &r_value);
|
||||
|
||||
String get_construct_string() const;
|
||||
static void construct_from_string(const String &p_string, Variant &r_value, ObjectConstruct p_obj_construct = nullptr, void *p_construct_ud = nullptr);
|
||||
|
||||
void operator=(const Variant &p_variant); // only this is enough for all the other types
|
||||
Variant(const Variant &p_variant);
|
||||
_FORCE_INLINE_ Variant() {
|
||||
type = NIL;
|
||||
}
|
||||
_FORCE_INLINE_ ~Variant() {
|
||||
if (type != Variant::NIL) {
|
||||
clear();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
//typedef Dictionary Dictionary; no
|
||||
//typedef Array Array;
|
||||
|
||||
Vector<Variant> varray();
|
||||
Vector<Variant> varray(const Variant &p_arg1);
|
||||
Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2);
|
||||
Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3);
|
||||
Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4);
|
||||
Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5);
|
||||
|
||||
struct VariantHasher {
|
||||
static _FORCE_INLINE_ uint32_t hash(const Variant &p_variant) { return p_variant.hash(); }
|
||||
};
|
||||
|
||||
struct VariantComparator {
|
||||
static _FORCE_INLINE_ bool compare(const Variant &p_lhs, const Variant &p_rhs) { return p_lhs.hash_compare(p_rhs); }
|
||||
};
|
||||
|
||||
Variant::ObjData &Variant::_get_obj() {
|
||||
return *reinterpret_cast<ObjData *>(&_data._mem[0]);
|
||||
}
|
||||
|
||||
const Variant::ObjData &Variant::_get_obj() const {
|
||||
return *reinterpret_cast<const ObjData *>(&_data._mem[0]);
|
||||
}
|
||||
|
||||
String vformat(const String &p_text, const Variant &p1 = Variant(), const Variant &p2 = Variant(), const Variant &p3 = Variant(), const Variant &p4 = Variant(), const Variant &p5 = Variant());
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user