test-client-godot/modules/referentialmessage/referentialmessagecore.h
2020-04-19 14:37:04 +02:00

105 lines
No EOL
3.3 KiB
C++

/*
Header ReferentialMessageCore : Class to store referential to decode message
Copyright (C) 2019 AleaJactaEst
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef REFERENTIAL_MESSAGE_CORE_H
#define REFERENTIAL_MESSAGE_CORE_H
#include "core/reference.h"
#include "core/dictionary.h"
#include "modules/bitstream/bitstream.h"
#include "modules/debug/debug.h"
#include "modules/impulse/impulsebase.h"
inline uint32_t getPowerOf2(uint32_t v)
{
// See code : khanat-opennel-code/code/nel/src/misc/common.cpp
uint32_t res = 1;
uint32_t ret = 0;
uint32_t limit = 8*sizeof(uint32_t);
while(res < v && ret < limit)
{
ret ++;
res <<= 1; // res *= 2;
}
return ret;
}
class ElementReferential : public Reference
{
GDCLASS(ElementReferential, Reference)
private:
uint32_t _id;
uint32_t _power2;
Vector<Ref<ElementReferential>> _children;
public:
ElementReferential() {_id=0; _power2=0;}
~ElementReferential() {_children.clear();}
void set_id(uint32_t id) {_id=id;}
void set_size(uint32_t size) {_children.resize(size); _power2=getPowerOf2(size);DBG_PRINT("[" + itos(size) + " /" + itos(_power2) + "]");}
int get_size() { return _children.size();}
Ref<ElementReferential> add_child(uint32_t id, uint32_t pos) { Ref<ElementReferential> tmp; tmp.instance(); tmp->_id = id; _children.set(pos, tmp); tmp.unref(); return _children[pos]; }
void show(int level=0, int pos=0);
int read_command(Ref<BitStream> msgin);
};
class ReferentialMessageCore : public Reference
{
GDCLASS(ReferentialMessageCore, Reference)
protected:
static void _bind_methods();
public:
class ElementHead
{
private:
uint32_t _size;
uint32_t _pos;
String _name;
public:
ElementHead() {}
~ElementHead() {}
void set_size(uint32_t size){_size = size;}
void set_pos(uint32_t pos){_pos = pos;}
void set_name(String name){_name = name;}
void create(uint32_t pos, uint32_t size,String name) {set_pos(pos);set_size(size);set_name(name);}
uint32_t get_size() {return _size;}
uint32_t get_pos() {return _pos;}
String get_name() {return _name;}
};
private:
static ReferentialMessageCore *singleton;
BitStream _encoder[ImpulseBase::Impulse::__LAST_ELEMENT];
Ref<ElementReferential> _decoder;
void read_referential_step(Dictionary step, Vector<ElementHead> head, Ref<ElementReferential> root);
public:
static ReferentialMessageCore * get_singleton();
ReferentialMessageCore() {_decoder.instance();}
~ReferentialMessageCore() {_decoder.unref();}
void read_referential(Dictionary dictionary_message);
void show();
static void clear_session();
int read_command(Ref<BitStream> msgin);
};
#endif