mirror of
https://github.com/Relintai/pmlpp.git
synced 2024-11-08 13:12:09 +01:00
Also added variant.
This commit is contained in:
parent
a6e965cc8e
commit
0e0d35fe7b
7
sfw/variant/SCsub
Normal file
7
sfw/variant/SCsub
Normal file
@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
Import("env")
|
||||
|
||||
env_variant = env.Clone()
|
||||
|
||||
env_variant.add_source_files(env.core_sources, "*.cpp")
|
505
sfw/variant/array.cpp
Normal file
505
sfw/variant/array.cpp
Normal file
@ -0,0 +1,505 @@
|
||||
/*************************************************************************/
|
||||
/* array.cpp */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* PANDEMONIUM ENGINE */
|
||||
/* https://github.com/Relintai/pandemonium_engine */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2022-present Péter Magyar. */
|
||||
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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 "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();
|
||||
}
|
113
sfw/variant/array.h
Normal file
113
sfw/variant/array.h
Normal file
@ -0,0 +1,113 @@
|
||||
#ifndef ARRAY_H
|
||||
#define ARRAY_H
|
||||
|
||||
/*************************************************************************/
|
||||
/* array.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* PANDEMONIUM ENGINE */
|
||||
/* https://github.com/Relintai/pandemonium_engine */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2022-present Péter Magyar. */
|
||||
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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 "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
|
323
sfw/variant/dictionary.cpp
Normal file
323
sfw/variant/dictionary.cpp
Normal file
@ -0,0 +1,323 @@
|
||||
/*************************************************************************/
|
||||
/* dictionary.cpp */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* PANDEMONIUM ENGINE */
|
||||
/* https://github.com/Relintai/pandemonium_engine */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2022-present Péter Magyar. */
|
||||
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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 "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();
|
||||
}
|
96
sfw/variant/dictionary.h
Normal file
96
sfw/variant/dictionary.h
Normal file
@ -0,0 +1,96 @@
|
||||
#ifndef DICTIONARY_H
|
||||
#define DICTIONARY_H
|
||||
|
||||
/*************************************************************************/
|
||||
/* dictionary.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* PANDEMONIUM ENGINE */
|
||||
/* https://github.com/Relintai/pandemonium_engine */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2022-present Péter Magyar. */
|
||||
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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 "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
|
470
sfw/variant/method_ptrcall.h
Normal file
470
sfw/variant/method_ptrcall.h
Normal file
@ -0,0 +1,470 @@
|
||||
#ifndef METHOD_PTRCALL_H
|
||||
#define METHOD_PTRCALL_H
|
||||
|
||||
/*************************************************************************/
|
||||
/* method_ptrcall.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* PANDEMONIUM ENGINE */
|
||||
/* https://github.com/Relintai/pandemonium_engine */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2022-present Péter Magyar. */
|
||||
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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 "core/math/transform_2d.h"
|
||||
#include "core/typedefs.h"
|
||||
#include "core/variant/variant.h"
|
||||
|
||||
#ifdef PTRCALL_ENABLED
|
||||
|
||||
template <class T>
|
||||
struct PtrToArg {
|
||||
};
|
||||
|
||||
#define MAKE_PTRARG(m_type) \
|
||||
template <> \
|
||||
struct PtrToArg<m_type> { \
|
||||
_FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
|
||||
return *reinterpret_cast<const m_type *>(p_ptr); \
|
||||
} \
|
||||
_FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \
|
||||
*((m_type *)p_ptr) = p_val; \
|
||||
} \
|
||||
}; \
|
||||
template <> \
|
||||
struct PtrToArg<const m_type &> { \
|
||||
_FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
|
||||
return *reinterpret_cast<const m_type *>(p_ptr); \
|
||||
} \
|
||||
_FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \
|
||||
*((m_type *)p_ptr) = p_val; \
|
||||
} \
|
||||
}
|
||||
|
||||
#define MAKE_PTRARGCONV(m_type, m_conv) \
|
||||
template <> \
|
||||
struct PtrToArg<m_type> { \
|
||||
_FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
|
||||
return static_cast<m_type>(*reinterpret_cast<const m_conv *>(p_ptr)); \
|
||||
} \
|
||||
_FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \
|
||||
*((m_conv *)p_ptr) = static_cast<m_conv>(p_val); \
|
||||
} \
|
||||
}; \
|
||||
template <> \
|
||||
struct PtrToArg<const m_type &> { \
|
||||
_FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
|
||||
return static_cast<m_type>(*reinterpret_cast<const m_conv *>(p_ptr)); \
|
||||
} \
|
||||
_FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \
|
||||
*((m_conv *)p_ptr) = static_cast<m_conv>(p_val); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define MAKE_PTRARG_BY_REFERENCE(m_type) \
|
||||
template <> \
|
||||
struct PtrToArg<m_type> { \
|
||||
_FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
|
||||
return *reinterpret_cast<const m_type *>(p_ptr); \
|
||||
} \
|
||||
_FORCE_INLINE_ static void encode(const m_type &p_val, void *p_ptr) { \
|
||||
*((m_type *)p_ptr) = p_val; \
|
||||
} \
|
||||
}; \
|
||||
template <> \
|
||||
struct PtrToArg<const m_type &> { \
|
||||
_FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
|
||||
return *reinterpret_cast<const m_type *>(p_ptr); \
|
||||
} \
|
||||
_FORCE_INLINE_ static void encode(const m_type &p_val, void *p_ptr) { \
|
||||
*((m_type *)p_ptr) = p_val; \
|
||||
} \
|
||||
}
|
||||
|
||||
MAKE_PTRARG(bool);
|
||||
MAKE_PTRARGCONV(uint8_t, int64_t);
|
||||
MAKE_PTRARGCONV(int8_t, int64_t);
|
||||
MAKE_PTRARGCONV(uint16_t, int64_t);
|
||||
MAKE_PTRARGCONV(int16_t, int64_t);
|
||||
MAKE_PTRARGCONV(uint32_t, int64_t);
|
||||
MAKE_PTRARGCONV(int32_t, int64_t);
|
||||
MAKE_PTRARG(int64_t);
|
||||
MAKE_PTRARG(uint64_t);
|
||||
MAKE_PTRARGCONV(float, double);
|
||||
MAKE_PTRARG(double);
|
||||
|
||||
MAKE_PTRARG(String);
|
||||
MAKE_PTRARG(StringName);
|
||||
|
||||
MAKE_PTRARG(Rect2);
|
||||
MAKE_PTRARG(Rect2i);
|
||||
MAKE_PTRARG(Vector2);
|
||||
MAKE_PTRARG(Vector2i);
|
||||
MAKE_PTRARG_BY_REFERENCE(Vector3);
|
||||
MAKE_PTRARG_BY_REFERENCE(Vector3i);
|
||||
MAKE_PTRARG_BY_REFERENCE(Vector4);
|
||||
MAKE_PTRARG_BY_REFERENCE(Vector4i);
|
||||
MAKE_PTRARG_BY_REFERENCE(Plane);
|
||||
MAKE_PTRARG(Quaternion);
|
||||
MAKE_PTRARG_BY_REFERENCE(AABB);
|
||||
MAKE_PTRARG_BY_REFERENCE(Basis);
|
||||
MAKE_PTRARG_BY_REFERENCE(Transform);
|
||||
MAKE_PTRARG(Transform2D);
|
||||
MAKE_PTRARG(Projection);
|
||||
MAKE_PTRARG_BY_REFERENCE(Color);
|
||||
MAKE_PTRARG(NodePath);
|
||||
MAKE_PTRARG(RID);
|
||||
MAKE_PTRARG(Dictionary);
|
||||
MAKE_PTRARG(Array);
|
||||
MAKE_PTRARG(PoolByteArray);
|
||||
MAKE_PTRARG(PoolIntArray);
|
||||
MAKE_PTRARG(PoolRealArray);
|
||||
MAKE_PTRARG(PoolStringArray);
|
||||
MAKE_PTRARG(PoolVector2Array);
|
||||
MAKE_PTRARG(PoolVector2iArray);
|
||||
MAKE_PTRARG(PoolVector3Array);
|
||||
MAKE_PTRARG(PoolVector3iArray);
|
||||
MAKE_PTRARG(PoolVector4Array);
|
||||
MAKE_PTRARG(PoolVector4iArray);
|
||||
MAKE_PTRARG(PoolColorArray);
|
||||
MAKE_PTRARG_BY_REFERENCE(Variant);
|
||||
|
||||
//this is for Object
|
||||
|
||||
template <class T>
|
||||
struct PtrToArg<T *> {
|
||||
_FORCE_INLINE_ static T *convert(const void *p_ptr) {
|
||||
return const_cast<T *>(reinterpret_cast<const T *>(p_ptr));
|
||||
}
|
||||
|
||||
_FORCE_INLINE_ static void encode(T *p_var, void *p_ptr) {
|
||||
*((T **)p_ptr) = p_var;
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct PtrToArg<const T *> {
|
||||
_FORCE_INLINE_ static const T *convert(const void *p_ptr) {
|
||||
return reinterpret_cast<const T *>(p_ptr);
|
||||
}
|
||||
|
||||
_FORCE_INLINE_ static void encode(T *p_var, void *p_ptr) {
|
||||
*((T **)p_ptr) = p_var;
|
||||
}
|
||||
};
|
||||
|
||||
//this is for the special cases used by Variant
|
||||
|
||||
#define MAKE_VECARG(m_type) \
|
||||
template <> \
|
||||
struct PtrToArg<Vector<m_type>> { \
|
||||
_FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
|
||||
const PoolVector<m_type> *dvs = reinterpret_cast<const PoolVector<m_type> *>(p_ptr); \
|
||||
Vector<m_type> ret; \
|
||||
int len = dvs->size(); \
|
||||
ret.resize(len); \
|
||||
{ \
|
||||
PoolVector<m_type>::Read r = dvs->read(); \
|
||||
for (int i = 0; i < len; i++) { \
|
||||
ret.write[i] = r[i]; \
|
||||
} \
|
||||
} \
|
||||
return ret; \
|
||||
} \
|
||||
_FORCE_INLINE_ static void encode(Vector<m_type> p_vec, void *p_ptr) { \
|
||||
PoolVector<m_type> *dv = reinterpret_cast<PoolVector<m_type> *>(p_ptr); \
|
||||
int len = p_vec.size(); \
|
||||
dv->resize(len); \
|
||||
{ \
|
||||
PoolVector<m_type>::Write w = dv->write(); \
|
||||
for (int i = 0; i < len; i++) { \
|
||||
w[i] = p_vec[i]; \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
}; \
|
||||
template <> \
|
||||
struct PtrToArg<const Vector<m_type> &> { \
|
||||
_FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
|
||||
const PoolVector<m_type> *dvs = reinterpret_cast<const PoolVector<m_type> *>(p_ptr); \
|
||||
Vector<m_type> ret; \
|
||||
int len = dvs->size(); \
|
||||
ret.resize(len); \
|
||||
{ \
|
||||
PoolVector<m_type>::Read r = dvs->read(); \
|
||||
for (int i = 0; i < len; i++) { \
|
||||
ret.write[i] = r[i]; \
|
||||
} \
|
||||
} \
|
||||
return ret; \
|
||||
} \
|
||||
}
|
||||
|
||||
#define MAKE_VECARG_ALT(m_type, m_type_alt) \
|
||||
template <> \
|
||||
struct PtrToArg<Vector<m_type_alt>> { \
|
||||
_FORCE_INLINE_ static Vector<m_type_alt> convert(const void *p_ptr) { \
|
||||
const PoolVector<m_type> *dvs = reinterpret_cast<const PoolVector<m_type> *>(p_ptr); \
|
||||
Vector<m_type_alt> ret; \
|
||||
int len = dvs->size(); \
|
||||
ret.resize(len); \
|
||||
{ \
|
||||
PoolVector<m_type>::Read r = dvs->read(); \
|
||||
for (int i = 0; i < len; i++) { \
|
||||
ret.write[i] = r[i]; \
|
||||
} \
|
||||
} \
|
||||
return ret; \
|
||||
} \
|
||||
_FORCE_INLINE_ static void encode(Vector<m_type_alt> p_vec, void *p_ptr) { \
|
||||
PoolVector<m_type> *dv = reinterpret_cast<PoolVector<m_type> *>(p_ptr); \
|
||||
int len = p_vec.size(); \
|
||||
dv->resize(len); \
|
||||
{ \
|
||||
PoolVector<m_type>::Write w = dv->write(); \
|
||||
for (int i = 0; i < len; i++) { \
|
||||
w[i] = p_vec[i]; \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
}; \
|
||||
template <> \
|
||||
struct PtrToArg<const Vector<m_type_alt> &> { \
|
||||
_FORCE_INLINE_ static Vector<m_type_alt> convert(const void *p_ptr) { \
|
||||
const PoolVector<m_type> *dvs = reinterpret_cast<const PoolVector<m_type> *>(p_ptr); \
|
||||
Vector<m_type_alt> ret; \
|
||||
int len = dvs->size(); \
|
||||
ret.resize(len); \
|
||||
{ \
|
||||
PoolVector<m_type>::Read r = dvs->read(); \
|
||||
for (int i = 0; i < len; i++) { \
|
||||
ret.write[i] = r[i]; \
|
||||
} \
|
||||
} \
|
||||
return ret; \
|
||||
} \
|
||||
}
|
||||
MAKE_VECARG(String);
|
||||
MAKE_VECARG(StringName);
|
||||
MAKE_VECARG(uint8_t);
|
||||
MAKE_VECARG(int);
|
||||
MAKE_VECARG(float);
|
||||
MAKE_VECARG(Vector2);
|
||||
MAKE_VECARG(Vector2i);
|
||||
MAKE_VECARG(Vector3);
|
||||
MAKE_VECARG(Vector3i);
|
||||
MAKE_VECARG(Vector4);
|
||||
MAKE_VECARG(Vector4i);
|
||||
MAKE_VECARG(Color);
|
||||
//MAKE_VECARG_ALT(String, StringName);
|
||||
|
||||
//for stuff that gets converted to Array vectors
|
||||
#define MAKE_VECARR(m_type) \
|
||||
template <> \
|
||||
struct PtrToArg<Vector<m_type>> { \
|
||||
_FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
|
||||
const Array *arr = reinterpret_cast<const Array *>(p_ptr); \
|
||||
Vector<m_type> ret; \
|
||||
int len = arr->size(); \
|
||||
ret.resize(len); \
|
||||
for (int i = 0; i < len; i++) { \
|
||||
ret.write[i] = (*arr)[i]; \
|
||||
} \
|
||||
return ret; \
|
||||
} \
|
||||
_FORCE_INLINE_ static void encode(Vector<m_type> p_vec, void *p_ptr) { \
|
||||
Array *arr = reinterpret_cast<Array *>(p_ptr); \
|
||||
int len = p_vec.size(); \
|
||||
arr->resize(len); \
|
||||
for (int i = 0; i < len; i++) { \
|
||||
(*arr)[i] = p_vec[i]; \
|
||||
} \
|
||||
} \
|
||||
}; \
|
||||
template <> \
|
||||
struct PtrToArg<const Vector<m_type> &> { \
|
||||
_FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
|
||||
const Array *arr = reinterpret_cast<const Array *>(p_ptr); \
|
||||
Vector<m_type> ret; \
|
||||
int len = arr->size(); \
|
||||
ret.resize(len); \
|
||||
for (int i = 0; i < len; i++) { \
|
||||
ret.write[i] = (*arr)[i]; \
|
||||
} \
|
||||
return ret; \
|
||||
} \
|
||||
}
|
||||
|
||||
MAKE_VECARR(Variant);
|
||||
MAKE_VECARR(RID);
|
||||
MAKE_VECARR(Plane);
|
||||
|
||||
#define MAKE_DVECARR(m_type) \
|
||||
template <> \
|
||||
struct PtrToArg<PoolVector<m_type>> { \
|
||||
_FORCE_INLINE_ static PoolVector<m_type> convert(const void *p_ptr) { \
|
||||
const Array *arr = reinterpret_cast<const Array *>(p_ptr); \
|
||||
PoolVector<m_type> ret; \
|
||||
int len = arr->size(); \
|
||||
ret.resize(len); \
|
||||
{ \
|
||||
PoolVector<m_type>::Write w = ret.write(); \
|
||||
for (int i = 0; i < len; i++) { \
|
||||
w[i] = (*arr)[i]; \
|
||||
} \
|
||||
} \
|
||||
return ret; \
|
||||
} \
|
||||
_FORCE_INLINE_ static void encode(PoolVector<m_type> p_vec, void *p_ptr) { \
|
||||
Array *arr = reinterpret_cast<Array *>(p_ptr); \
|
||||
int len = p_vec.size(); \
|
||||
arr->resize(len); \
|
||||
{ \
|
||||
PoolVector<m_type>::Read r = p_vec.read(); \
|
||||
for (int i = 0; i < len; i++) { \
|
||||
(*arr)[i] = r[i]; \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
}; \
|
||||
template <> \
|
||||
struct PtrToArg<const PoolVector<m_type> &> { \
|
||||
_FORCE_INLINE_ static PoolVector<m_type> convert(const void *p_ptr) { \
|
||||
const Array *arr = reinterpret_cast<const Array *>(p_ptr); \
|
||||
PoolVector<m_type> ret; \
|
||||
int len = arr->size(); \
|
||||
ret.resize(len); \
|
||||
{ \
|
||||
PoolVector<m_type>::Write w = ret.write(); \
|
||||
for (int i = 0; i < len; i++) { \
|
||||
w[i] = (*arr)[i]; \
|
||||
} \
|
||||
} \
|
||||
return ret; \
|
||||
} \
|
||||
}
|
||||
|
||||
MAKE_DVECARR(Plane);
|
||||
//for special case StringName
|
||||
|
||||
#define MAKE_STRINGCONV(m_type) \
|
||||
template <> \
|
||||
struct PtrToArg<m_type> { \
|
||||
_FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
|
||||
m_type s = *reinterpret_cast<const String *>(p_ptr); \
|
||||
return s; \
|
||||
} \
|
||||
_FORCE_INLINE_ static void encode(m_type p_vec, void *p_ptr) { \
|
||||
String *arr = reinterpret_cast<String *>(p_ptr); \
|
||||
*arr = p_vec; \
|
||||
} \
|
||||
}; \
|
||||
\
|
||||
template <> \
|
||||
struct PtrToArg<const m_type &> { \
|
||||
_FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
|
||||
m_type s = *reinterpret_cast<const String *>(p_ptr); \
|
||||
return s; \
|
||||
} \
|
||||
}
|
||||
|
||||
#define MAKE_STRINGCONV_BY_REFERENCE(m_type) \
|
||||
template <> \
|
||||
struct PtrToArg<m_type> { \
|
||||
_FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
|
||||
m_type s = *reinterpret_cast<const String *>(p_ptr); \
|
||||
return s; \
|
||||
} \
|
||||
_FORCE_INLINE_ static void encode(const m_type &p_vec, void *p_ptr) { \
|
||||
String *arr = reinterpret_cast<String *>(p_ptr); \
|
||||
*arr = p_vec; \
|
||||
} \
|
||||
}; \
|
||||
\
|
||||
template <> \
|
||||
struct PtrToArg<const m_type &> { \
|
||||
_FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
|
||||
m_type s = *reinterpret_cast<const String *>(p_ptr); \
|
||||
return s; \
|
||||
} \
|
||||
}
|
||||
|
||||
//MAKE_STRINGCONV(StringName);
|
||||
MAKE_STRINGCONV_BY_REFERENCE(IP_Address);
|
||||
|
||||
template <>
|
||||
struct PtrToArg<PoolVector<Face3>> {
|
||||
_FORCE_INLINE_ static PoolVector<Face3> convert(const void *p_ptr) {
|
||||
const PoolVector<Vector3> *dvs = reinterpret_cast<const PoolVector<Vector3> *>(p_ptr);
|
||||
PoolVector<Face3> ret;
|
||||
int len = dvs->size() / 3;
|
||||
ret.resize(len);
|
||||
{
|
||||
PoolVector<Vector3>::Read r = dvs->read();
|
||||
PoolVector<Face3>::Write w = ret.write();
|
||||
for (int i = 0; i < len; i++) {
|
||||
w[i].vertex[0] = r[i * 3 + 0];
|
||||
w[i].vertex[1] = r[i * 3 + 1];
|
||||
w[i].vertex[2] = r[i * 3 + 2];
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
_FORCE_INLINE_ static void encode(PoolVector<Face3> p_vec, void *p_ptr) {
|
||||
PoolVector<Vector3> *arr = reinterpret_cast<PoolVector<Vector3> *>(p_ptr);
|
||||
int len = p_vec.size();
|
||||
arr->resize(len * 3);
|
||||
{
|
||||
PoolVector<Face3>::Read r = p_vec.read();
|
||||
PoolVector<Vector3>::Write w = arr->write();
|
||||
for (int i = 0; i < len; i++) {
|
||||
w[i * 3 + 0] = r[i].vertex[0];
|
||||
w[i * 3 + 1] = r[i].vertex[1];
|
||||
w[i * 3 + 2] = r[i].vertex[2];
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
template <>
|
||||
struct PtrToArg<const PoolVector<Face3> &> {
|
||||
_FORCE_INLINE_ static PoolVector<Face3> convert(const void *p_ptr) {
|
||||
const PoolVector<Vector3> *dvs = reinterpret_cast<const PoolVector<Vector3> *>(p_ptr);
|
||||
PoolVector<Face3> ret;
|
||||
int len = dvs->size() / 3;
|
||||
ret.resize(len);
|
||||
{
|
||||
PoolVector<Vector3>::Read r = dvs->read();
|
||||
PoolVector<Face3>::Write w = ret.write();
|
||||
for (int i = 0; i < len; i++) {
|
||||
w[i].vertex[0] = r[i * 3 + 0];
|
||||
w[i].vertex[1] = r[i * 3 + 1];
|
||||
w[i].vertex[2] = r[i * 3 + 2];
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // METHOD_PTRCALL_H
|
||||
#endif
|
305
sfw/variant/type_info.h
Normal file
305
sfw/variant/type_info.h
Normal file
@ -0,0 +1,305 @@
|
||||
#ifndef GET_TYPE_INFO_H
|
||||
#define GET_TYPE_INFO_H
|
||||
|
||||
/*************************************************************************/
|
||||
/* type_info.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* PANDEMONIUM ENGINE */
|
||||
/* https://github.com/Relintai/pandemonium_engine */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2022-present Péter Magyar. */
|
||||
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/*************************************************************************/
|
||||
|
||||
#ifdef DEBUG_METHODS_ENABLED
|
||||
|
||||
template <bool C, typename T = void>
|
||||
struct EnableIf {
|
||||
typedef T type;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct EnableIf<false, T> {
|
||||
};
|
||||
|
||||
template <typename, typename>
|
||||
struct TypesAreSame {
|
||||
static bool const value = false;
|
||||
};
|
||||
|
||||
template <typename A>
|
||||
struct TypesAreSame<A, A> {
|
||||
static bool const value = true;
|
||||
};
|
||||
|
||||
template <typename B, typename D>
|
||||
struct TypeInherits {
|
||||
static D *get_d();
|
||||
|
||||
static char (&test(B *))[1];
|
||||
static char (&test(...))[2];
|
||||
|
||||
static bool const value = sizeof(test(get_d())) == sizeof(char) &&
|
||||
!TypesAreSame<B volatile const, void volatile const>::value;
|
||||
};
|
||||
|
||||
namespace PandemoniumTypeInfo {
|
||||
enum Metadata {
|
||||
METADATA_NONE,
|
||||
METADATA_INT_IS_INT8,
|
||||
METADATA_INT_IS_INT16,
|
||||
METADATA_INT_IS_INT32,
|
||||
METADATA_INT_IS_INT64,
|
||||
METADATA_INT_IS_UINT8,
|
||||
METADATA_INT_IS_UINT16,
|
||||
METADATA_INT_IS_UINT32,
|
||||
METADATA_INT_IS_UINT64,
|
||||
METADATA_REAL_IS_FLOAT,
|
||||
METADATA_REAL_IS_DOUBLE
|
||||
};
|
||||
}
|
||||
|
||||
// If the compiler fails because it's trying to instantiate the primary 'GetTypeInfo' template
|
||||
// instead of one of the specializations, it's most likely because the type 'T' is not supported.
|
||||
// If 'T' is a class that inherits 'Object', make sure it can see the actual class declaration
|
||||
// instead of a forward declaration. You can always forward declare 'T' in a header file, and then
|
||||
// include the actual declaration of 'T' in the source file where 'GetTypeInfo<T>' is instantiated.
|
||||
template <class T, typename = void>
|
||||
struct GetTypeInfo;
|
||||
|
||||
#define MAKE_TYPE_INFO(m_type, m_var_type) \
|
||||
template <> \
|
||||
struct GetTypeInfo<m_type> { \
|
||||
static const Variant::Type VARIANT_TYPE = m_var_type; \
|
||||
static const PandemoniumTypeInfo::Metadata METADATA = PandemoniumTypeInfo::METADATA_NONE; \
|
||||
static inline PropertyInfo get_class_info() { \
|
||||
return PropertyInfo(VARIANT_TYPE, String()); \
|
||||
} \
|
||||
}; \
|
||||
template <> \
|
||||
struct GetTypeInfo<const m_type &> { \
|
||||
static const Variant::Type VARIANT_TYPE = m_var_type; \
|
||||
static const PandemoniumTypeInfo::Metadata METADATA = PandemoniumTypeInfo::METADATA_NONE; \
|
||||
static inline PropertyInfo get_class_info() { \
|
||||
return PropertyInfo(VARIANT_TYPE, String()); \
|
||||
} \
|
||||
};
|
||||
|
||||
#define MAKE_TYPE_INFO_WITH_META(m_type, m_var_type, m_metadata) \
|
||||
template <> \
|
||||
struct GetTypeInfo<m_type> { \
|
||||
static const Variant::Type VARIANT_TYPE = m_var_type; \
|
||||
static const PandemoniumTypeInfo::Metadata METADATA = m_metadata; \
|
||||
static inline PropertyInfo get_class_info() { \
|
||||
return PropertyInfo(VARIANT_TYPE, String()); \
|
||||
} \
|
||||
}; \
|
||||
template <> \
|
||||
struct GetTypeInfo<const m_type &> { \
|
||||
static const Variant::Type VARIANT_TYPE = m_var_type; \
|
||||
static const PandemoniumTypeInfo::Metadata METADATA = m_metadata; \
|
||||
static inline PropertyInfo get_class_info() { \
|
||||
return PropertyInfo(VARIANT_TYPE, String()); \
|
||||
} \
|
||||
};
|
||||
|
||||
MAKE_TYPE_INFO(bool, Variant::BOOL)
|
||||
MAKE_TYPE_INFO_WITH_META(uint8_t, Variant::INT, PandemoniumTypeInfo::METADATA_INT_IS_UINT8)
|
||||
MAKE_TYPE_INFO_WITH_META(int8_t, Variant::INT, PandemoniumTypeInfo::METADATA_INT_IS_INT8)
|
||||
MAKE_TYPE_INFO_WITH_META(uint16_t, Variant::INT, PandemoniumTypeInfo::METADATA_INT_IS_UINT16)
|
||||
MAKE_TYPE_INFO_WITH_META(int16_t, Variant::INT, PandemoniumTypeInfo::METADATA_INT_IS_INT16)
|
||||
MAKE_TYPE_INFO_WITH_META(uint32_t, Variant::INT, PandemoniumTypeInfo::METADATA_INT_IS_UINT32)
|
||||
MAKE_TYPE_INFO_WITH_META(int32_t, Variant::INT, PandemoniumTypeInfo::METADATA_INT_IS_INT32)
|
||||
MAKE_TYPE_INFO_WITH_META(uint64_t, Variant::INT, PandemoniumTypeInfo::METADATA_INT_IS_UINT64)
|
||||
MAKE_TYPE_INFO_WITH_META(int64_t, Variant::INT, PandemoniumTypeInfo::METADATA_INT_IS_INT64)
|
||||
MAKE_TYPE_INFO(char16_t, Variant::INT)
|
||||
MAKE_TYPE_INFO(char32_t, Variant::INT)
|
||||
MAKE_TYPE_INFO_WITH_META(float, Variant::REAL, PandemoniumTypeInfo::METADATA_REAL_IS_FLOAT)
|
||||
MAKE_TYPE_INFO_WITH_META(double, Variant::REAL, PandemoniumTypeInfo::METADATA_REAL_IS_DOUBLE)
|
||||
|
||||
MAKE_TYPE_INFO(String, Variant::STRING)
|
||||
MAKE_TYPE_INFO(Rect2, Variant::RECT2)
|
||||
MAKE_TYPE_INFO(Rect2i, Variant::RECT2I)
|
||||
MAKE_TYPE_INFO(Vector2, Variant::VECTOR2)
|
||||
MAKE_TYPE_INFO(Vector2i, Variant::VECTOR2I)
|
||||
MAKE_TYPE_INFO(Vector3, Variant::VECTOR3)
|
||||
MAKE_TYPE_INFO(Vector3i, Variant::VECTOR3I)
|
||||
MAKE_TYPE_INFO(Vector4, Variant::VECTOR4)
|
||||
MAKE_TYPE_INFO(Vector4i, Variant::VECTOR4I)
|
||||
MAKE_TYPE_INFO(Plane, Variant::PLANE)
|
||||
MAKE_TYPE_INFO(Quaternion, Variant::QUATERNION)
|
||||
MAKE_TYPE_INFO(AABB, Variant::AABB)
|
||||
MAKE_TYPE_INFO(Basis, Variant::BASIS)
|
||||
MAKE_TYPE_INFO(Transform, Variant::TRANSFORM)
|
||||
MAKE_TYPE_INFO(Transform2D, Variant::TRANSFORM2D)
|
||||
MAKE_TYPE_INFO(Projection, Variant::PROJECTION)
|
||||
MAKE_TYPE_INFO(Color, Variant::COLOR)
|
||||
MAKE_TYPE_INFO(NodePath, Variant::NODE_PATH)
|
||||
MAKE_TYPE_INFO(RID, Variant::RID)
|
||||
MAKE_TYPE_INFO(Dictionary, Variant::DICTIONARY)
|
||||
MAKE_TYPE_INFO(Array, Variant::ARRAY)
|
||||
MAKE_TYPE_INFO(PoolByteArray, Variant::POOL_BYTE_ARRAY)
|
||||
MAKE_TYPE_INFO(PoolIntArray, Variant::POOL_INT_ARRAY)
|
||||
MAKE_TYPE_INFO(PoolRealArray, Variant::POOL_REAL_ARRAY)
|
||||
MAKE_TYPE_INFO(PoolStringArray, Variant::POOL_STRING_ARRAY)
|
||||
MAKE_TYPE_INFO(PoolVector2Array, Variant::POOL_VECTOR2_ARRAY)
|
||||
MAKE_TYPE_INFO(PoolVector2iArray, Variant::POOL_VECTOR2I_ARRAY)
|
||||
MAKE_TYPE_INFO(PoolVector3Array, Variant::POOL_VECTOR3_ARRAY)
|
||||
MAKE_TYPE_INFO(PoolVector3iArray, Variant::POOL_VECTOR3I_ARRAY)
|
||||
MAKE_TYPE_INFO(PoolVector4Array, Variant::POOL_VECTOR4_ARRAY)
|
||||
MAKE_TYPE_INFO(PoolVector4iArray, Variant::POOL_VECTOR4I_ARRAY)
|
||||
MAKE_TYPE_INFO(PoolColorArray, Variant::POOL_COLOR_ARRAY)
|
||||
|
||||
MAKE_TYPE_INFO(StringName, Variant::STRING_NAME)
|
||||
MAKE_TYPE_INFO(IP_Address, Variant::STRING)
|
||||
|
||||
class BSP_Tree;
|
||||
MAKE_TYPE_INFO(BSP_Tree, Variant::DICTIONARY)
|
||||
|
||||
//for RefPtr
|
||||
template <>
|
||||
struct GetTypeInfo<RefPtr> {
|
||||
static const Variant::Type VARIANT_TYPE = Variant::OBJECT;
|
||||
static const PandemoniumTypeInfo::Metadata METADATA = PandemoniumTypeInfo::METADATA_NONE;
|
||||
static inline PropertyInfo get_class_info() {
|
||||
return PropertyInfo(Variant::OBJECT, String(), PROPERTY_HINT_RESOURCE_TYPE, "Reference");
|
||||
}
|
||||
};
|
||||
template <>
|
||||
struct GetTypeInfo<const RefPtr &> {
|
||||
static const Variant::Type VARIANT_TYPE = Variant::OBJECT;
|
||||
static const PandemoniumTypeInfo::Metadata METADATA = PandemoniumTypeInfo::METADATA_NONE;
|
||||
static inline PropertyInfo get_class_info() {
|
||||
return PropertyInfo(Variant::OBJECT, String(), PROPERTY_HINT_RESOURCE_TYPE, "Reference");
|
||||
}
|
||||
};
|
||||
|
||||
//for variant
|
||||
template <>
|
||||
struct GetTypeInfo<Variant> {
|
||||
static const Variant::Type VARIANT_TYPE = Variant::NIL;
|
||||
static const PandemoniumTypeInfo::Metadata METADATA = PandemoniumTypeInfo::METADATA_NONE;
|
||||
static inline PropertyInfo get_class_info() {
|
||||
return PropertyInfo(Variant::NIL, String(), PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct GetTypeInfo<const Variant &> {
|
||||
static const Variant::Type VARIANT_TYPE = Variant::NIL;
|
||||
static const PandemoniumTypeInfo::Metadata METADATA = PandemoniumTypeInfo::METADATA_NONE;
|
||||
static inline PropertyInfo get_class_info() {
|
||||
return PropertyInfo(Variant::NIL, String(), PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT);
|
||||
}
|
||||
};
|
||||
|
||||
#define MAKE_TEMPLATE_TYPE_INFO(m_template, m_type, m_var_type) \
|
||||
template <> \
|
||||
struct GetTypeInfo<m_template<m_type>> { \
|
||||
static const Variant::Type VARIANT_TYPE = m_var_type; \
|
||||
static const PandemoniumTypeInfo::Metadata METADATA = PandemoniumTypeInfo::METADATA_NONE; \
|
||||
static inline PropertyInfo get_class_info() { \
|
||||
return PropertyInfo(VARIANT_TYPE, String()); \
|
||||
} \
|
||||
}; \
|
||||
template <> \
|
||||
struct GetTypeInfo<const m_template<m_type> &> { \
|
||||
static const Variant::Type VARIANT_TYPE = m_var_type; \
|
||||
static const PandemoniumTypeInfo::Metadata METADATA = PandemoniumTypeInfo::METADATA_NONE; \
|
||||
static inline PropertyInfo get_class_info() { \
|
||||
return PropertyInfo(VARIANT_TYPE, String()); \
|
||||
} \
|
||||
};
|
||||
|
||||
MAKE_TEMPLATE_TYPE_INFO(Vector, uint8_t, Variant::POOL_BYTE_ARRAY)
|
||||
MAKE_TEMPLATE_TYPE_INFO(Vector, int, Variant::POOL_INT_ARRAY)
|
||||
MAKE_TEMPLATE_TYPE_INFO(Vector, float, Variant::POOL_REAL_ARRAY)
|
||||
MAKE_TEMPLATE_TYPE_INFO(Vector, String, Variant::POOL_STRING_ARRAY)
|
||||
MAKE_TEMPLATE_TYPE_INFO(Vector, Vector2, Variant::POOL_VECTOR2_ARRAY)
|
||||
MAKE_TEMPLATE_TYPE_INFO(Vector, Vector2i, Variant::POOL_VECTOR2I_ARRAY)
|
||||
MAKE_TEMPLATE_TYPE_INFO(Vector, Vector3, Variant::POOL_VECTOR3_ARRAY)
|
||||
MAKE_TEMPLATE_TYPE_INFO(Vector, Vector3i, Variant::POOL_VECTOR3I_ARRAY)
|
||||
MAKE_TEMPLATE_TYPE_INFO(Vector, Vector4, Variant::POOL_VECTOR4_ARRAY)
|
||||
MAKE_TEMPLATE_TYPE_INFO(Vector, Vector4i, Variant::POOL_VECTOR4I_ARRAY)
|
||||
MAKE_TEMPLATE_TYPE_INFO(Vector, Color, Variant::POOL_COLOR_ARRAY)
|
||||
|
||||
MAKE_TEMPLATE_TYPE_INFO(Vector, Variant, Variant::ARRAY)
|
||||
MAKE_TEMPLATE_TYPE_INFO(Vector, RID, Variant::ARRAY)
|
||||
MAKE_TEMPLATE_TYPE_INFO(Vector, Plane, Variant::ARRAY)
|
||||
MAKE_TEMPLATE_TYPE_INFO(Vector, StringName, Variant::POOL_STRING_ARRAY)
|
||||
|
||||
MAKE_TEMPLATE_TYPE_INFO(PoolVector, Plane, Variant::ARRAY)
|
||||
MAKE_TEMPLATE_TYPE_INFO(PoolVector, Face3, Variant::POOL_VECTOR3_ARRAY)
|
||||
|
||||
template <typename T>
|
||||
struct GetTypeInfo<T *, typename EnableIf<TypeInherits<Object, T>::value>::type> {
|
||||
static const Variant::Type VARIANT_TYPE = Variant::OBJECT;
|
||||
static const PandemoniumTypeInfo::Metadata METADATA = PandemoniumTypeInfo::METADATA_NONE;
|
||||
static inline PropertyInfo get_class_info() {
|
||||
return PropertyInfo(StringName(T::get_class_static()));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct GetTypeInfo<const T *, typename EnableIf<TypeInherits<Object, T>::value>::type> {
|
||||
static const Variant::Type VARIANT_TYPE = Variant::OBJECT;
|
||||
static const PandemoniumTypeInfo::Metadata METADATA = PandemoniumTypeInfo::METADATA_NONE;
|
||||
static inline PropertyInfo get_class_info() {
|
||||
return PropertyInfo(StringName(T::get_class_static()));
|
||||
}
|
||||
};
|
||||
|
||||
#define TEMPL_MAKE_ENUM_TYPE_INFO(m_enum, m_impl) \
|
||||
template <> \
|
||||
struct GetTypeInfo<m_impl> { \
|
||||
static const Variant::Type VARIANT_TYPE = Variant::INT; \
|
||||
static const PandemoniumTypeInfo::Metadata METADATA = PandemoniumTypeInfo::METADATA_NONE; \
|
||||
static inline PropertyInfo get_class_info() { \
|
||||
return PropertyInfo(Variant::INT, String(), PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_CLASS_IS_ENUM, String(#m_enum).replace("::", ".")); \
|
||||
} \
|
||||
};
|
||||
|
||||
#define MAKE_ENUM_TYPE_INFO(m_enum) \
|
||||
TEMPL_MAKE_ENUM_TYPE_INFO(m_enum, m_enum) \
|
||||
TEMPL_MAKE_ENUM_TYPE_INFO(m_enum, m_enum const) \
|
||||
TEMPL_MAKE_ENUM_TYPE_INFO(m_enum, m_enum &) \
|
||||
TEMPL_MAKE_ENUM_TYPE_INFO(m_enum, const m_enum &)
|
||||
|
||||
template <typename T>
|
||||
inline StringName __constant_get_enum_name(T param, const String &p_constant) {
|
||||
if (GetTypeInfo<T>::VARIANT_TYPE == Variant::NIL)
|
||||
ERR_PRINT("Missing VARIANT_ENUM_CAST for constant's enum: " + p_constant);
|
||||
return GetTypeInfo<T>::get_class_info().class_name;
|
||||
}
|
||||
|
||||
#define CLASS_INFO(m_type) (GetTypeInfo<m_type *>::get_class_info())
|
||||
|
||||
#else
|
||||
|
||||
#define MAKE_ENUM_TYPE_INFO(m_enum)
|
||||
#define CLASS_INFO(m_type)
|
||||
|
||||
#endif // DEBUG_METHODS_ENABLED
|
||||
|
||||
#endif // GET_TYPE_INFO_H
|
4014
sfw/variant/variant.cpp
Normal file
4014
sfw/variant/variant.cpp
Normal file
File diff suppressed because it is too large
Load Diff
526
sfw/variant/variant.h
Normal file
526
sfw/variant/variant.h
Normal file
@ -0,0 +1,526 @@
|
||||
#ifndef VARIANT_H
|
||||
#define VARIANT_H
|
||||
|
||||
/*************************************************************************/
|
||||
/* variant.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* PANDEMONIUM ENGINE */
|
||||
/* https://github.com/Relintai/pandemonium_engine */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2022-present Péter Magyar. */
|
||||
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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 "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 {
|
||||
|
||||
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
|
||||
|
||||
};
|
||||
|
||||
enum {
|
||||
// Maximum recursion depth allowed when serializing variants.
|
||||
MAX_RECURSION_DEPTH = 1024,
|
||||
};
|
||||
|
||||
private:
|
||||
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;
|
||||
};
|
||||
|
||||
_FORCE_INLINE_ ObjData &_get_obj();
|
||||
_FORCE_INLINE_ const ObjData &_get_obj() const;
|
||||
|
||||
union {
|
||||
bool _bool;
|
||||
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
|
||||
|
||||
};
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//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
|
3591
sfw/variant/variant_call.cpp
Normal file
3591
sfw/variant/variant_call.cpp
Normal file
File diff suppressed because it is too large
Load Diff
5851
sfw/variant/variant_op.cpp
Normal file
5851
sfw/variant/variant_op.cpp
Normal file
File diff suppressed because it is too large
Load Diff
2082
sfw/variant/variant_parser.cpp
Normal file
2082
sfw/variant/variant_parser.cpp
Normal file
File diff suppressed because it is too large
Load Diff
175
sfw/variant/variant_parser.h
Normal file
175
sfw/variant/variant_parser.h
Normal file
@ -0,0 +1,175 @@
|
||||
#ifndef VARIANT_PARSER_H
|
||||
#define VARIANT_PARSER_H
|
||||
|
||||
/*************************************************************************/
|
||||
/* variant_parser.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* PANDEMONIUM ENGINE */
|
||||
/* https://github.com/Relintai/pandemonium_engine */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2022-present Péter Magyar. */
|
||||
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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 "core/object/resource.h"
|
||||
#include "core/os/file_access.h"
|
||||
#include "core/variant/variant.h"
|
||||
|
||||
class VariantParser {
|
||||
public:
|
||||
struct Stream {
|
||||
private:
|
||||
enum { READAHEAD_SIZE = 2048 };
|
||||
CharType readahead_buffer[READAHEAD_SIZE];
|
||||
uint32_t readahead_pointer = 0;
|
||||
uint32_t readahead_filled = 0;
|
||||
bool eof = false;
|
||||
|
||||
protected:
|
||||
bool readahead_enabled = true;
|
||||
virtual uint32_t _read_buffer(CharType *p_buffer, uint32_t p_num_chars) = 0;
|
||||
virtual bool _is_eof() const = 0;
|
||||
|
||||
public:
|
||||
CharType saved;
|
||||
|
||||
CharType get_char();
|
||||
virtual bool is_utf8() const = 0;
|
||||
bool is_eof() const;
|
||||
|
||||
Stream() :
|
||||
saved(0) {}
|
||||
virtual ~Stream() {}
|
||||
};
|
||||
|
||||
struct StreamFile : public Stream {
|
||||
protected:
|
||||
virtual uint32_t _read_buffer(CharType *p_buffer, uint32_t p_num_chars);
|
||||
virtual bool _is_eof() const;
|
||||
|
||||
public:
|
||||
FileAccess *f;
|
||||
|
||||
virtual bool is_utf8() const;
|
||||
StreamFile(bool p_readahead_enabled = true) {
|
||||
f = nullptr;
|
||||
readahead_enabled = p_readahead_enabled;
|
||||
}
|
||||
};
|
||||
|
||||
struct StreamString : public Stream {
|
||||
private:
|
||||
int pos;
|
||||
|
||||
protected:
|
||||
virtual uint32_t _read_buffer(CharType *p_buffer, uint32_t p_num_chars);
|
||||
virtual bool _is_eof() const;
|
||||
|
||||
public:
|
||||
String s;
|
||||
|
||||
virtual bool is_utf8() const;
|
||||
StreamString(bool p_readahead_enabled = true) {
|
||||
pos = 0;
|
||||
readahead_enabled = p_readahead_enabled;
|
||||
}
|
||||
};
|
||||
|
||||
typedef Error (*ParseResourceFunc)(void *p_self, Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
|
||||
|
||||
struct ResourceParser {
|
||||
void *userdata = nullptr;
|
||||
ParseResourceFunc func = nullptr;
|
||||
ParseResourceFunc ext_func = nullptr;
|
||||
ParseResourceFunc sub_func = nullptr;
|
||||
};
|
||||
|
||||
enum TokenType {
|
||||
TK_CURLY_BRACKET_OPEN,
|
||||
TK_CURLY_BRACKET_CLOSE,
|
||||
TK_BRACKET_OPEN,
|
||||
TK_BRACKET_CLOSE,
|
||||
TK_PARENTHESIS_OPEN,
|
||||
TK_PARENTHESIS_CLOSE,
|
||||
TK_IDENTIFIER,
|
||||
TK_STRING,
|
||||
TK_STRING_NAME,
|
||||
TK_NUMBER,
|
||||
TK_COLOR,
|
||||
TK_COLON,
|
||||
TK_COMMA,
|
||||
TK_PERIOD,
|
||||
TK_EQUAL,
|
||||
TK_EOF,
|
||||
TK_ERROR,
|
||||
TK_MAX
|
||||
};
|
||||
|
||||
enum Expecting {
|
||||
|
||||
EXPECT_OBJECT,
|
||||
EXPECT_OBJECT_KEY,
|
||||
EXPECT_COLON,
|
||||
EXPECT_OBJECT_VALUE,
|
||||
};
|
||||
|
||||
struct Token {
|
||||
TokenType type;
|
||||
Variant value;
|
||||
};
|
||||
|
||||
struct Tag {
|
||||
String name;
|
||||
RBMap<String, Variant> fields;
|
||||
};
|
||||
|
||||
private:
|
||||
static const char *tk_name[TK_MAX];
|
||||
|
||||
template <class T>
|
||||
static Error _parse_construct(Stream *p_stream, Vector<T> &r_construct, int &line, String &r_err_str);
|
||||
static Error _parse_enginecfg(Stream *p_stream, Vector<String> &strings, int &line, String &r_err_str);
|
||||
static Error _parse_dictionary(Dictionary &object, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser = nullptr);
|
||||
static Error _parse_array(Array &array, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser = nullptr);
|
||||
static Error _parse_tag(Token &token, Stream *p_stream, int &line, String &r_err_str, Tag &r_tag, ResourceParser *p_res_parser = nullptr, bool p_simple_tag = false);
|
||||
|
||||
public:
|
||||
static Error parse_tag(Stream *p_stream, int &line, String &r_err_str, Tag &r_tag, ResourceParser *p_res_parser = nullptr, bool p_simple_tag = false);
|
||||
static Error parse_tag_assign_eof(Stream *p_stream, int &line, String &r_err_str, Tag &r_tag, String &r_assign, Variant &r_value, ResourceParser *p_res_parser = nullptr, bool p_simple_tag = false);
|
||||
|
||||
static Error parse_value(Token &token, Variant &value, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser = nullptr);
|
||||
static Error get_token(Stream *p_stream, Token &r_token, int &line, String &r_err_str);
|
||||
static Error parse(Stream *p_stream, Variant &r_ret, String &r_err_str, int &r_err_line, ResourceParser *p_res_parser = nullptr);
|
||||
};
|
||||
|
||||
class VariantWriter {
|
||||
public:
|
||||
typedef Error (*StoreStringFunc)(void *ud, const String &p_string);
|
||||
typedef String (*EncodeResourceFunc)(void *ud, const RES &p_resource);
|
||||
|
||||
static Error write(const Variant &p_variant, StoreStringFunc p_store_string_func, void *p_store_string_ud, EncodeResourceFunc p_encode_res_func, void *p_encode_res_ud, int p_recursion_count = 0);
|
||||
static Error write_to_string(const Variant &p_variant, String &r_string, EncodeResourceFunc p_encode_res_func = nullptr, void *p_encode_res_ud = nullptr);
|
||||
};
|
||||
|
||||
#endif // VARIANT_PARSER_H
|
Loading…
Reference in New Issue
Block a user