clean up object module
This commit is contained in:
parent
4c66704830
commit
5d9e270aa9
68 changed files with 1223 additions and 1229 deletions
|
|
@ -10,12 +10,12 @@
|
|||
#include "core/TypeGroups.hpp"
|
||||
#include "core/TypeMethods.hpp"
|
||||
|
||||
#include "Archiver.hpp"
|
||||
#include "ObjectArchiver.hpp"
|
||||
#include "SizeCounter.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
//#include "interpreter/interpreter.h"
|
||||
#include "core/ObjectArchiver.hpp"
|
||||
|
||||
/* Steps to create custom Object:
|
||||
define name of object
|
||||
|
|
@ -37,80 +37,29 @@ implement construct, destruct and copy methods */
|
|||
#define NDO_MEMH_FROM_NDO(ndo_ptr) (((ObjectMemHead*)ndo_ptr) - 1)
|
||||
#define NDO_FROM_MEMH(ndo_ptr) ((Object*)(ndo_ptr + 1))
|
||||
|
||||
namespace obj {
|
||||
namespace tp::obj {
|
||||
|
||||
|
||||
template<bool tRead>
|
||||
class Archiver : public tp::ArchiverTemplate<tRead> {
|
||||
tp::LocalConnection mConnection;
|
||||
tp::ualni mAddress = 0;
|
||||
tp::ualni mFirstNotWritten = 0;
|
||||
|
||||
public:
|
||||
Archiver() = default;
|
||||
explicit Archiver(const char* location) {
|
||||
mConnection.connect(tp::LocalConnection::Location(location), tp::LocalConnection::Type(tRead));
|
||||
};
|
||||
|
||||
void writeBytes(const tp::int1* val, tp::ualni size) override {
|
||||
mConnection.setPointer(mAddress);
|
||||
mConnection.writeBytes(val, size);
|
||||
incrementAddresses(size);
|
||||
}
|
||||
|
||||
void readBytes(tp::int1* val, tp::ualni size) override {
|
||||
mConnection.setPointer(mAddress);
|
||||
mConnection.readBytes(val, size);
|
||||
incrementAddresses(size);
|
||||
}
|
||||
|
||||
tp::ualni getAddress() { return mAddress; }
|
||||
|
||||
void setAddress(tp::ualni addr) { mAddress = addr; }
|
||||
|
||||
tp::ualni getFreeAddress() { return mFirstNotWritten; }
|
||||
|
||||
void setFreeAddress(tp::ualni addr) { mFirstNotWritten = addr; }
|
||||
|
||||
bool isOpened() { return mConnection.getConnectionStatus().isOpened(); }
|
||||
|
||||
tp::ualni getSize() { return mConnection.size(); }
|
||||
|
||||
private:
|
||||
void incrementAddresses(tp::ualni size) {
|
||||
// if (mAddress + size > mFirstNotWritten) mFirstNotWritten = mAddress + size;
|
||||
mAddress += size;
|
||||
}
|
||||
};
|
||||
|
||||
using ArchiverIn = Archiver<true>;
|
||||
using ArchiverOut = Archiver<false>;
|
||||
|
||||
void save_string(ArchiverOut& file, const std::string& string);
|
||||
tp::ualni save_string_size(const std::string& string);
|
||||
void load_string(ArchiverIn& file, std::string& out);
|
||||
|
||||
extern tp::ModuleManifest gModuleObjects;
|
||||
extern ModuleManifest gModuleObjects;
|
||||
|
||||
extern struct objects_api* NDO;
|
||||
|
||||
struct ObjectMemHead {
|
||||
ObjectMemHead* up;
|
||||
ObjectMemHead* down;
|
||||
tp::alni flags;
|
||||
tp::alni refc;
|
||||
alni flags;
|
||||
alni refc;
|
||||
};
|
||||
|
||||
struct Object {
|
||||
const struct ObjectType* type;
|
||||
};
|
||||
|
||||
typedef void (*object_from_int)(Object* self, tp::alni in);
|
||||
typedef void (*object_from_float)(Object* self, tp::alnf in);
|
||||
typedef void (*object_from_int)(Object* self, alni in);
|
||||
typedef void (*object_from_float)(Object* self, alnf in);
|
||||
typedef void (*object_from_string)(Object* self, const std::string& in);
|
||||
typedef std::string(*object_to_string)(Object* self);
|
||||
typedef tp::alni(*object_to_int)(Object* self);
|
||||
typedef tp::alnf(*object_to_float)(Object* self);
|
||||
typedef alni(*object_to_int)(Object* self);
|
||||
typedef alnf(*object_to_float)(Object* self);
|
||||
|
||||
struct ObjectTypeConversions {
|
||||
object_from_int from_int;
|
||||
|
|
@ -137,22 +86,22 @@ namespace obj {
|
|||
typedef void (*object_destructor)(Object* self);
|
||||
typedef void (*object_copy)(Object* self, const Object* target);
|
||||
|
||||
typedef tp::alni(*object_save_size)(Object* self);
|
||||
typedef alni(*object_save_size)(Object* self);
|
||||
typedef void (*object_save)(Object*, ArchiverOut&);
|
||||
typedef void (*object_load)(ArchiverIn&, Object*);
|
||||
|
||||
typedef bool (*object_compare)(Object*, Object*);
|
||||
|
||||
typedef tp::Buffer<Object*> (*object_debug_all_childs_retrival)(Object*);
|
||||
typedef tp::alni (*object_allocated_size)(Object*); // default value = type->size - sizeof(ObjectType*)
|
||||
typedef tp::alni (*object_allocated_size_recursive)(Object*); // default value = object_allocated_size
|
||||
typedef Buffer<Object*> (*object_debug_all_childs_retrival)(Object*);
|
||||
typedef alni (*object_allocated_size)(Object*); // default value = type->size - sizeof(ObjectType*)
|
||||
typedef alni (*object_allocated_size_recursive)(Object*); // default value = object_allocated_size
|
||||
|
||||
struct ObjectType {
|
||||
const ObjectType* base = nullptr;
|
||||
object_constructor constructor = nullptr;
|
||||
object_destructor destructor = nullptr;
|
||||
object_copy copy = nullptr;
|
||||
tp::alni size = 0;
|
||||
alni size = 0;
|
||||
const char* name;
|
||||
const ObjectTypeConversions* convesions = nullptr;
|
||||
const ObjectTypeAriphmetics* ariphmetics = nullptr;
|
||||
|
|
@ -174,7 +123,7 @@ namespace obj {
|
|||
typedef void (pre_load_callback)(void* self, ArchiverIn&);
|
||||
typedef void (post_save_callback)(void* self, ArchiverOut&);
|
||||
typedef void (post_load_callback)(void* self, ArchiverIn&);
|
||||
typedef tp::alni (slcb_size_callback)(void* self, ArchiverOut&);
|
||||
typedef alni (slcb_size_callback)(void* self, ArchiverOut&);
|
||||
|
||||
struct save_load_callbacks {
|
||||
void* self;
|
||||
|
|
@ -188,8 +137,8 @@ namespace obj {
|
|||
|
||||
struct objects_api {
|
||||
|
||||
tp::Map<std::string, const ObjectType*> types;
|
||||
obj::TypeGroups type_groups;
|
||||
Map<std::string, const ObjectType*> types;
|
||||
TypeGroups type_groups;
|
||||
Interpreter* interp = nullptr;
|
||||
|
||||
objects_api();
|
||||
|
|
@ -197,54 +146,54 @@ namespace obj {
|
|||
|
||||
void define(ObjectType* type);
|
||||
Object* create(const std::string& name);
|
||||
Object* copy(Object* self, const Object* in);
|
||||
bool compare(Object* first, Object* second);
|
||||
Object* instatiate(Object*);
|
||||
void set(Object* self, tp::alni val);
|
||||
void set(Object* self, tp::alnf val);
|
||||
void set(Object* self, const std::string& val);
|
||||
static Object* copy(Object* self, const Object* in);
|
||||
static bool compare(Object* first, Object* second);
|
||||
static Object* instantiate(Object*);
|
||||
static void set(Object* self, alni val);
|
||||
static void set(Object* self, alnf val);
|
||||
static void set(Object* self, const std::string& val);
|
||||
|
||||
tp::alni toInt(Object* self);
|
||||
tp::alnf toFloat(Object* self);
|
||||
bool toBool(Object* self);
|
||||
std::string toString(Object* self);
|
||||
static alni toInt(Object* self);
|
||||
static alnf toFloat(Object* self);
|
||||
static bool toBool(Object* self);
|
||||
static std::string toString(Object* self);
|
||||
|
||||
void clear_object_flags();
|
||||
|
||||
void destroy(Object* in);
|
||||
void destroy(Object* in) const;
|
||||
|
||||
void refinc(Object* in);
|
||||
tp::halni getrefc(Object* in);
|
||||
static void increaseReferenceCount(Object* in);
|
||||
static halni getReferenceCount(Object* in);
|
||||
private:
|
||||
void setrefc(Object* in, tp::halni refc);
|
||||
static void setReferenceCount(Object* in, halni count);
|
||||
public:
|
||||
|
||||
save_load_callbacks* sl_callbacks[SAVE_LOAD_MAX_CALLBACK_SLOTS];
|
||||
tp::alni sl_callbacks_load_idx = 0;
|
||||
save_load_callbacks* sl_callbacks[SAVE_LOAD_MAX_CALLBACK_SLOTS]{};
|
||||
alni sl_callbacks_load_idx = 0;
|
||||
|
||||
void add_sl_callbacks(save_load_callbacks*);
|
||||
|
||||
tp::alni objsize_file(Object* self);
|
||||
tp::alni objsize_file_recursive(Object* self);
|
||||
alni objsize_file(Object* self);
|
||||
alni objsize_file_recursive(Object* self);
|
||||
|
||||
tp::alni objsize_ram(Object* self);
|
||||
tp::alni objsize_ram_recursive_util(Object* self, const ObjectType* type, bool different_object = true);
|
||||
tp::alni objsize_ram_recursive(Object* self);
|
||||
alni objsize_ram(Object* self);
|
||||
alni objsize_ram_recursive_util(Object* self, const ObjectType* type, bool different_object = true);
|
||||
alni objsize_ram_recursive(Object* self);
|
||||
|
||||
bool save(Object*, const std::string& path, bool compressed = true);
|
||||
Object* load(const std::string& path);
|
||||
tp::alni save(ArchiverOut&, Object*);
|
||||
Object* load(ArchiverIn&, tp::alni file_adress);
|
||||
alni save(ArchiverOut&, Object*);
|
||||
Object* load(ArchiverIn&, alni file_adress);
|
||||
};
|
||||
|
||||
void logTypeData(const ObjectType* type);
|
||||
void assertNoLeaks();
|
||||
tp::ualni getObjCount();
|
||||
ualni getObjCount();
|
||||
|
||||
Object* ndo_cast(const Object* in, const ObjectType* to_type);
|
||||
|
||||
template <typename Type>
|
||||
Type* create() {
|
||||
return (Type*)obj::NDO->create(Type::TypeData.name);
|
||||
return (Type*)NDO->create(Type::TypeData.name);
|
||||
}
|
||||
};
|
||||
}
|
||||
56
Objects/public/core/ObjectArchiver.hpp
Normal file
56
Objects/public/core/ObjectArchiver.hpp
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
#pragma once
|
||||
|
||||
#include "ObjectArchiver.hpp"
|
||||
#include "LocalConnection.hpp"
|
||||
|
||||
namespace tp::obj {
|
||||
template<bool tRead>
|
||||
class Archiver : public ArchiverTemplate<tRead> {
|
||||
LocalConnection mConnection;
|
||||
ualni mAddress = 0;
|
||||
ualni mFirstNotWritten = 0;
|
||||
|
||||
public:
|
||||
Archiver() = default;
|
||||
explicit Archiver(const char* location) {
|
||||
mConnection.connect(LocalConnection::Location(location), LocalConnection::Type(tRead));
|
||||
};
|
||||
|
||||
void writeBytes(const int1* val, ualni size) override {
|
||||
mConnection.setPointer(mAddress);
|
||||
mConnection.writeBytes(val, size);
|
||||
incrementAddresses(size);
|
||||
}
|
||||
|
||||
void readBytes(int1* val, ualni size) override {
|
||||
mConnection.setPointer(mAddress);
|
||||
mConnection.readBytes(val, size);
|
||||
incrementAddresses(size);
|
||||
}
|
||||
|
||||
ualni getAddress() { return mAddress; }
|
||||
|
||||
void setAddress(ualni addr) { mAddress = addr; }
|
||||
|
||||
ualni getFreeAddress() { return mFirstNotWritten; }
|
||||
|
||||
void setFreeAddress(ualni addr) { mFirstNotWritten = addr; }
|
||||
|
||||
bool isOpened() { return mConnection.getConnectionStatus().isOpened(); }
|
||||
|
||||
ualni getSize() { return mConnection.size(); }
|
||||
|
||||
private:
|
||||
void incrementAddresses(ualni size) {
|
||||
// if (mAddress + size > mFirstNotWritten) mFirstNotWritten = mAddress + size;
|
||||
mAddress += size;
|
||||
}
|
||||
};
|
||||
|
||||
using ArchiverIn = Archiver<true>;
|
||||
using ArchiverOut = Archiver<false>;
|
||||
|
||||
void save_string(ArchiverOut& file, const std::string& string);
|
||||
ualni save_string_size(const std::string& string);
|
||||
void load_string(ArchiverIn& file, std::string& out);
|
||||
}
|
||||
|
|
@ -2,19 +2,19 @@
|
|||
|
||||
#include "primitives/MethodObject.hpp"
|
||||
|
||||
namespace obj {
|
||||
namespace tp::obj {
|
||||
|
||||
// global singleton API for method object to manage the script
|
||||
struct ScriptSection {
|
||||
|
||||
tp::List<Script*> mScripts;
|
||||
List<Script*> mScripts;
|
||||
|
||||
Script* createScript();
|
||||
void changeScript(Script** current_script, Script** new_script);
|
||||
void abandonScript(Script* script);
|
||||
|
||||
tp::alni get_script_file_adress(Script* in);
|
||||
Script* get_scritp_from_file_adress(tp::alni file_adress);
|
||||
alni get_script_file_adress(Script* in);
|
||||
Script* get_scritp_from_file_adress(alni file_adress);
|
||||
|
||||
~ScriptSection();
|
||||
|
||||
|
|
@ -31,6 +31,6 @@ namespace obj {
|
|||
|
||||
static void save_script_table_to_file(ScriptSection* self, ArchiverOut& file);
|
||||
static void load_script_table_from_file(ScriptSection* self, ArchiverIn& file);
|
||||
static tp::alni save_script_table_to_file_size(ScriptSection* self, ArchiverOut& file);
|
||||
static alni save_script_table_to_file_size(ScriptSection* self, ArchiverOut& file);
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
@ -4,7 +4,7 @@
|
|||
#include "Map.hpp"
|
||||
#include <string>
|
||||
|
||||
namespace obj {
|
||||
namespace tp::obj {
|
||||
|
||||
struct ObjectType;
|
||||
struct objects_api;
|
||||
|
|
@ -13,15 +13,15 @@ namespace obj {
|
|||
|
||||
public:
|
||||
|
||||
typedef tp::Map<std::string, obj::TypeGroups*> Dict;
|
||||
typedef Map<std::string, TypeGroups*> Dict;
|
||||
|
||||
TypeGroups();
|
||||
|
||||
friend struct obj::objects_api;
|
||||
friend struct objects_api;
|
||||
|
||||
explicit TypeGroups(bool is_group);
|
||||
|
||||
void addType(ObjectType* type, tp::InitialierList<const char*> path, tp::alni cur_arg = 0);
|
||||
void addType(ObjectType* type, InitialierList<const char*> path, alni cur_arg = 0);
|
||||
void setType(ObjectType* type);
|
||||
bool isGroup();
|
||||
Dict* getChilds();
|
||||
|
|
@ -36,5 +36,4 @@ namespace obj {
|
|||
const obj::ObjectType* type;
|
||||
};
|
||||
};
|
||||
|
||||
};
|
||||
}
|
||||
|
|
@ -4,7 +4,7 @@
|
|||
#include "Buffer.hpp"
|
||||
#include <string>
|
||||
|
||||
namespace obj {
|
||||
namespace tp::obj {
|
||||
|
||||
struct Interpreter;
|
||||
struct Object;
|
||||
|
|
@ -23,7 +23,7 @@ namespace obj {
|
|||
mutable Object* self = nullptr;
|
||||
Arg args[MAX_ARGS];
|
||||
|
||||
tp::int1 mNargs = 0;
|
||||
int1 mNargs = 0;
|
||||
|
||||
void (*exec)(const TypeMethod* tm) = nullptr;
|
||||
|
||||
|
|
@ -37,14 +37,14 @@ namespace obj {
|
|||
extern TypeMethod gDefaultTypeMethods[3];
|
||||
|
||||
struct TypeMethods {
|
||||
enum : tp::int2 { MAX_TYPE_METHODS = 128 };
|
||||
enum : int2 { MAX_TYPE_METHODS = 128 };
|
||||
|
||||
TypeMethod* methods[MAX_TYPE_METHODS];
|
||||
tp::halni mNMethods = 0;
|
||||
halni mNMethods = 0;
|
||||
|
||||
struct LookupKey {
|
||||
tp::int2 key = -1;
|
||||
tp::int2 type_depth = -1;
|
||||
int2 key = -1;
|
||||
int2 type_depth = -1;
|
||||
operator bool() { return key != -1; }
|
||||
};
|
||||
|
||||
|
|
@ -53,11 +53,10 @@ namespace obj {
|
|||
|
||||
void init();
|
||||
|
||||
tp::halni nMethods() const;
|
||||
halni nMethods() const;
|
||||
|
||||
private:
|
||||
tp::int2 presents(const std::string&) const;
|
||||
const TypeMethod* getMethod(tp::int2) const;
|
||||
int2 presents(const std::string&) const;
|
||||
const TypeMethod* getMethod(int2) const;
|
||||
};
|
||||
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue