rcpp_framework/core/resource.h

121 lines
4.0 KiB
C
Raw Normal View History

#ifndef RESOURCE_H
#define RESOURCE_H
#include "core/containers/vector.h"
#include "core/string.h"
2021-08-21 14:40:30 +02:00
#include "rapidjson/document.h"
#include "reference.h"
2021-08-21 14:40:30 +02:00
#if DATABASES_ENABLED
class Database;
class QueryBuilder;
2021-08-21 14:40:30 +02:00
#endif
//Based on Godot Engine's object method_bind initializers
#define RCPP_RESOURCE(m_class, m_inherits) \
public: \
static ResourcePropertyDB properties; \
\
static void initialize_class(ResourcePropertyDB *pdb = nullptr) { \
\
if (pdb == nullptr) { \
pdb = &m_class::properties; \
} \
\
m_inherits::initialize_class(pdb); \
\
if (m_class::_get_bind_properties() != m_inherits::_get_bind_properties()) { \
_bind_properties(pdb); \
} \
} \
\
protected: \
inline static void (*_get_bind_properties())() { \
return &m_class::_bind_properties; \
} \
\
private:
//add db_get etc templates to this
//#if DATABASES_ENABLED
//static Ref<RBACRank> db_get(const int id) {
// return Resource::db_get<RBACRank>(id);
//}
//#endif
//etc
//ResourcePropertyDB * -> bind properties param
class Resource : public Reference {
RCPP_OBJECT(Resource, Reference);
public:
//uncomment!
//static ResourcePropertyDB properties;
int id;
//since we will need getters and setters, this should be added
bool dirty;
2021-08-21 14:40:30 +02:00
virtual String to_json(rapidjson::Document *into = nullptr);
virtual void from_json(const String &data);
2021-08-21 14:40:30 +02:00
virtual String to_ini() { return ""; }
virtual void from_ini(const String &data) {}
#if DATABASES_ENABLED
virtual void db_load() {}
//should use transactions if a resource has subresources, when qg is null it should start and end a transaction.
virtual void db_save(QueryBuilder *qb = nullptr) {}
//these could be a template, so just every subclass can return itself with these. Also these could use transactions
template <class T>
static Ref<T> db_get(const int id);
static Ref<Resource> db_get_all();
static void db_save_all(const Vector<Ref<Resource> > &resources);
#endif
2021-11-16 17:34:01 +01:00
//add serialization to and from requests
//could also build a form validator automatically using the property registration api
//Resource::register<T>() foreach parents cast call
//RBACRank::register()
Resource();
~Resource();
protected:
inline static void (*_get_bind_properties())() {
return &Resource::_bind_properties;
}
//add ResourcePropertyDB *pdb param
//register id
static void _bind_properties() {}
public: //should be protected, but bug in clang++
//init like the macro
static void initialize_class() {}
private:
};
//minden resource derivaltnak:
//PropertyDB db;
//minden benne van
//RBACRanks::PropertyDB -> minden benne van, osclass is
//RBACPermission::PropertyDB -> minden bene van osok is
template <class T>
Ref<T> Resource::db_get(const int id) {
Ref<T> data;
data.instance();
return data;
}
#endif