Object D中的自动数据持久化
有人想过在D中实现某种自动数据(对象)持久化吗?我对这个问题的理想解决方案是:Object D中的自动数据持久化,object,reflection,persistence,d,Object,Reflection,Persistence,D,有人想过在D中实现某种自动数据(对象)持久化吗?我对这个问题的理想解决方案是: @persistent int x = 1; 这对静态变量最为有效,但动态变量也是可能的 这些变量将存储在键值存储数据库中。键可以是基于作用域变量名和类型的指纹摘要,再加上当前加载代码的一些摘要。您可以对模板执行类似的操作。看看这个: import std.stdio; // do not declare two of these on the same line or they'll get mixed up
@persistent int x = 1;
这对静态变量最为有效,但动态变量也是可能的
这些变量将存储在键值存储数据库中。键可以是基于作用域变量名和类型的指纹摘要,再加上当前加载代码的一些摘要。您可以对模板执行类似的操作。看看这个:
import std.stdio;
// do not declare two of these on the same line or they'll get mixed up
struct persistent(Type, string file = __FILE__, size_t line = __LINE__) {
Type info;
alias info this;
// require an initializer
@disable this();
// with the initializer
this(Type t) {
// if it is in the file, we should load it here
// else...
info = t;
}
~this() {
// you should actually save it to the file
writeln("Saving ", info, " as key ",
file,":",line);
}
}
void main() {
persistent!int x = 10;
}
如果您运行它,您将看到initalizer和write,并且如果您填写了一个文件备份(可能是使用键和值的json,或者处理更多类型的其他序列化程序),它应该能够保存。您还可以让dtor保存到全局缓冲区,然后让模块析构函数实际将其保存到文件中(模块构造函数也加载该文件),这样它就不会在每次函数调用时尝试读取/写入文件
所有变量的行为都将如同它们是静态的一样,因为您可以看到这里的键是声明的文件和行号,没有环境输入。但是,这很简单,应该可以用。你会想把这篇文章发到网上,然后看看他们怎么说,我不认为他们会这么做有很多原因。