refact Register

This commit is contained in:
a-Sansara 2018-08-23 11:11:58 +02:00
parent 8e2b1a92d2
commit 37f5e43720
7 changed files with 131 additions and 189 deletions

View File

@ -77,7 +77,7 @@ public class Pluie.Yaml.Builder
}
/**
* retiew GLib.Type related to specified tag value.
* retriew GLib.Type related to specified tag value.
* Type may not be registered yet
*/
public static Type? type_from_tag (string tagValue)
@ -176,7 +176,7 @@ public class Pluie.Yaml.Builder
public static void set_from_collection (ref GLib.Object obj, Type parentType, Yaml.Node node, Type type)
{
Yaml.dbg (" > set_from_collection %s (%s)".printf (node.name, type.name ()));
if (type.is_a (typeof (Yaml.Object)) || Yaml.Object.register.is_registered_type (parentType, type)) {
if (type.is_a (typeof (Yaml.Object)) || Yaml.Register.is_registered_type (parentType, type)) {
(obj as Yaml.Object).populate_from_node (node.name, type, node);
}
else {
@ -336,10 +336,10 @@ public class Pluie.Yaml.Builder
foreach (var def in obj.get_class ().list_properties ()){
name = Yaml.Builder.transform_param_name(def.name);
if (name != null && name != "yaml_name") {
if (def.value_type.is_a (typeof (Yaml.Object)) || Yaml.Object.register.is_registered_type(obj.get_type (), def.value_type)) {
if (def.value_type.is_a (typeof (Yaml.Object)) || Yaml.Register.is_registered_type(obj.get_type (), def.value_type)) {
var child = (obj as Yaml.Object).populate_to_node(name, def.value_type, node);
if (child != null) {
child.tag = new Yaml.Tag (Yaml.Object.register.resolve_namespace_type(def.value_type), "v");
child.tag = new Yaml.Tag (Yaml.Register.resolve_namespace_type(def.value_type), "v");
node.add (child);
}
}
@ -348,8 +348,7 @@ public class Pluie.Yaml.Builder
obj.get (name, out enumval);
string data = enumval.value.to_string ();
var n = new Yaml.Mapping.with_scalar (node, name, (string) data);
n.tag = new Yaml.Tag (Yaml.Object.register.resolve_namespace_type(def.value_type), "v");
n.tag = new Yaml.Tag (Yaml.Register.resolve_namespace_type(def.value_type), "v");
}
else if (def.value_type.is_fundamental ()) {
string data = Yaml.Builder.get_basic_type_value(obj, def.value_type, name);
@ -362,7 +361,7 @@ public class Pluie.Yaml.Builder
}
}
}
node.tag = new Yaml.Tag (Yaml.Object.register.resolve_namespace_type(obj.get_type ()), "v");
node.tag = new Yaml.Tag (Yaml.Register.resolve_namespace_type(obj.get_type ()), "v");
if (root) {
var rootNode = new Yaml.Root();
rootNode.add (node);

View File

@ -69,7 +69,7 @@ public class Pluie.Yaml.Example : Yaml.Object
static construct
{
Yaml.Object.register.add_type (
Yaml.Register.add_type (
typeof (Yaml.Example),
typeof (Yaml.ExampleChild),
typeof (Yaml.ExampleStruct),
@ -84,7 +84,7 @@ public class Pluie.Yaml.Example : Yaml.Object
{
this.type_gee_al = new Gee.ArrayList<double?> ();
this.type_gee_alobject = new Gee.ArrayList<Yaml.ExampleChild> ();
register.add_namespace("Gee", "GLib");
Yaml.Register.add_namespace("Gee", "GLib");
Dbg.msg ("%s (%s) instantiated".printf (this.yaml_name, this.get_type().name ()), Log.LINE, Log.FILE);
}
@ -101,7 +101,7 @@ public class Pluie.Yaml.Example : Yaml.Object
*/
public override void populate_from_node (string name, GLib.Type type, Yaml.Node node) {
if (type == typeof (Yaml.ExampleStruct)) {
this.type_struct = ExampleStruct.from_yaml_node (node);
this.type_struct = Yaml.ExampleStruct.from_yaml_node (node);
}
else if (type == typeof (Gee.ArrayList)) {
foreach (var child in node) {
@ -140,8 +140,7 @@ public class Pluie.Yaml.Example : Yaml.Object
public override Yaml.Node? populate_to_node (string name, GLib.Type type, Yaml.Node parent) {
Yaml.Node? node = null;
if (type == typeof (Yaml.ExampleStruct)) {
Yaml.ExampleStruct p = this.type_struct;
node = ExampleStruct.to_yaml_node (ref p, name);
node = this.type_struct.to_yaml_node (name);
}
else if (type == typeof (Gee.ArrayList)) {
switch (name) {
@ -149,7 +148,7 @@ public class Pluie.Yaml.Example : Yaml.Object
Yaml.GeeBuilder.arraylist_to_node (this.type_gee_al, name, parent);
break;
case "type_gee_alobject" :
this.collection_to_node (this.type_gee_alobject, cast_child_collection, name, parent);
Yaml.Object.collection_to_node (this.type_gee_alobject, name, parent);
break;
}

View File

@ -31,12 +31,12 @@
/**
* a test class to implements yamlize
*/
public class Pluie.Yaml.ExampleChild : GLib.Object
public class Pluie.Yaml.ExampleChild : Yaml.Object
{
public string toto { get; set; }
public string tata { get; set; }
public int titi { get; set; }
public bool tutu { get; set; }
public string toto { get; set; }
public string tata { get; set; }
public int titi { get; set; }
public bool tutu { get; set; }
public void method_a ()
{

View File

@ -50,7 +50,7 @@ public struct Pluie.Yaml.ExampleStruct
*/
public static ExampleStruct from_yaml_node (Yaml.Node node)
{
var s = ExampleStruct ();
ExampleStruct s = {};
foreach (var child in node) {
var v = child.val (typeof (uint));
switch (child.name) {
@ -65,12 +65,12 @@ public struct Pluie.Yaml.ExampleStruct
/**
*
*/
public static Yaml.Node to_yaml_node (ref ExampleStruct self, string name)
public Yaml.Node to_yaml_node (string name)
{
var node = new Yaml.Mapping (null, name);
new Yaml.Mapping.with_scalar (node, "red" , self.red.to_string ());
new Yaml.Mapping.with_scalar (node, "green", self.green.to_string ());
new Yaml.Mapping.with_scalar (node, "blue" , self.blue.to_string ());
new Yaml.Mapping.with_scalar (node, "red" , this.red.to_string ());
new Yaml.Mapping.with_scalar (node, "green", this.green.to_string ());
new Yaml.Mapping.with_scalar (node, "blue" , this.blue.to_string ());
return node;
}

View File

@ -37,109 +37,6 @@ using Pluie;
*/
public class Pluie.Yaml.GeeBuilder
{
/**
*
*/
public static void arraylist_from_node (ref Yaml.Object obj, Yaml.Node node, Type type)
{
Gee.ArrayList* p;
obj.get (node.name, out p);
GLib.Type? elementType = null;
if (p != null && (elementType = p->element_type) != null) {
Yaml.dbg (" arraylist_from_node %s (%s<%s>)".printf (node.name, type.name (), elementType.name ()));
if (!elementType.is_object () && elementType.is_fundamental ()) {
switch (elementType)
{
case Type.STRING :
var l = new Gee.ArrayList<string> ();
foreach (var child in node) {
l.add(child.data);
}
obj.set (node.name, l);
break;
case Type.CHAR :
var l = new Gee.ArrayList<int8> ();
foreach (var child in node) {
l.add((int8)child.data.data[0]);
}
obj.set (node.name, l);
break;
case Type.UCHAR :
var l = new Gee.ArrayList<uchar> ();
foreach (var child in node) {
l.add((uint8)child.data.data[0]);
}
obj.set (node.name, l);
break;
case Type.BOOLEAN :
var l = new Gee.ArrayList<bool> ();
foreach (var child in node) {
l.add(child.data == "1" || child.data.down () == "true");
}
obj.set (node.name, l);
break;
case Type.INT :
var l = new Gee.ArrayList<int> ();
foreach (var child in node) {
l.add(int.parse(child.data));
}
obj.set (node.name, l);
break;
case Type.UINT :
var l = new Gee.ArrayList<uint> ();
foreach (var child in node) {
l.add((uint)long.parse(child.data));
}
obj.set (node.name, l);
break;
case Type.LONG :
case Type.INT64 :
var l = new Gee.ArrayList<long> ();
foreach (var child in node) {
l.add((long)int64.parse(child.data));
}
obj.set (node.name, l);
break;
case Type.ULONG :
case Type.UINT64 :
var l = new Gee.ArrayList<ulong> ();
foreach (var child in node) {
l.add((ulong)uint64.parse(child.data));
}
obj.set (node.name, l);
break;
case Type.FLOAT :
var l = new Gee.ArrayList<float?> ();
foreach (var child in node) {
l.add((float)double.parse(child.data));
}
obj.set (node.name, l);
break;
case Type.DOUBLE :
//~ .add(double.parse(child.data));
var l = new Gee.ArrayList<double?> ();
foreach (var child in node) {
//~ obj.sig_set_data (node.name, type, child);
l.add(double.parse(child.data));
}
obj.set (node.name, l);
break;
}
}
else if (elementType.is_object ()) {
//~ var pg = p;
//~ foreach (var child in node) {
//~ pg.add(Yaml.Builder.from_node (child, elementType));
//~ }
}
}
else {
of.warn ("can't retriew Generic Type. did you forget to instanciate the list in yaml_construct () method ?");
}
}
/**
*
*/
@ -196,7 +93,7 @@ public class Pluie.Yaml.GeeBuilder
break;
}
}
var f = new Yaml.Scalar (node, data);
new Yaml.Scalar (node, data);
}
else if (type.is_object ()) {

View File

@ -57,7 +57,7 @@ public abstract class Pluie.Yaml.Object : GLib.Object
{
register = new Yaml.Register();
yaml_tag = new Tag (typeof (Pluie.Yaml.Object).name (), "v");
register.add_namespace("Pluie", "Pluie.Yaml");
Yaml.Register.add_namespace("Pluie", "Pluie.Yaml");
}
/**
@ -110,20 +110,18 @@ public abstract class Pluie.Yaml.Object : GLib.Object
return node;
}
public delegate GLib.Object CastYamlObject (string name, GLib.Object obj);
/**
*
*/
public virtual Yaml.Node? collection_to_node (Gee.Collection list, CastYamlObject castYamlObject, string name, Yaml.Node? parent = null)
public static Yaml.Node? collection_to_node (Gee.Collection list, string name, Yaml.Node? parent = null)
{
var node = new Yaml.Sequence (parent, name);
node.tag = new Yaml.Tag (Yaml.Object.register.resolve_namespace_type(list.get_type ()), "v");
node.tag = new Yaml.Tag (Yaml.Register.resolve_namespace_type(list.get_type ()), "v");
var it = list.iterator ();
var i = 0;
while (it.next ()) {
Yaml.Builder.to_node (
castYamlObject(name, (GLib.Object) it.get ()),
(GLib.Object) it.get (),
node,
false,
i++

View File

@ -33,39 +33,92 @@ using Pluie;
using Gee;
/**
* a class registering type which could be populated
* A Register class responsible to register owner types and related types lists
*
* The register also manage full namespace resolving for the yaml tags generation <<BR>>
* (from Yaml.Object to Yaml.Node)
*
* The registered types means that they can (must) be populated from Yaml.Node
* to Yaml.Object or the opposite.<<BR>>
* See classes Yaml.Builder & Yaml.Object for more details about that.
*/
public class Pluie.Yaml.Register : GLib.Object
public class Pluie.Yaml.Register
{
/**
*
*/
public static Gee.HashMap<Type, Gee.ArrayList<GLib.Type>> rtype { get; internal set; }
static Gee.HashMap<Type, Gee.ArrayList<GLib.Type>> rtype;
/**
*
*/
public static Gee.ArrayList<string> namespaces { get; internal set; }
static Gee.ArrayList<string> namespaces;
/**
*
*/
static Gee.HashMap<Quark, string> ns_resolved;
/**
*
*/
static construct {
Yaml.Register.rtype = new Gee.HashMap<Type, Gee.ArrayList<GLib.Type>> ();
Yaml.Register.namespaces = new Gee.ArrayList<string> ();
Yaml.Register.rtype = new Gee.HashMap<Type, Gee.ArrayList<GLib.Type>> ();
Yaml.Register.namespaces = new Gee.ArrayList<string> ();
Yaml.Register.ns_resolved = new Gee.HashMap<Quark, string> ();
}
/**
*
*/
private Gee.ArrayList<GLib.Type> init_type_list ()
internal Register ()
{
return new Gee.ArrayList<GLib.Type> ();
}
/**
*
* add multiple type to specified owner type
* @param owner_type the owner type to registering types
*/
public bool add_namespace (string name, ...)
public static bool add_type (GLib.Type owner_type, ...)
{
bool done = true;
if (!is_registered (owner_type)) {
rtype.set (owner_type, init_type_list ());
}
var l = va_list();
while (done) {
GLib.Type? t = l.arg<GLib.Type> ();
if (t == null || t == Type.INVALID) {
break;
}
Yaml.dbg ("adding to %s type %s".printf (owner_type.name (), t.name ()));
done = done && rtype.get (owner_type).add (t);
}
return done;
}
/**
* check if specified owner_type is registered
* @param owner_type the owner type to check
*/
public static bool is_registered (GLib.Type owner_type)
{
return rtype.has_key (owner_type);
}
/**
* check if specified type is registered for specified owner_type
* @param owner_type the owner type to check
* @param type the type presumably belonging to owner type
*/
public static bool is_registered_type (GLib.Type owner_type, GLib.Type type)
{
return is_registered (owner_type) && rtype.get (owner_type).contains (type);
}
/**
* add one or multiple namespace for tag resolution
* @param name a namespace to register
*/
public static bool add_namespace (string name, ...)
{
var l = va_list();
Yaml.dbg ("adding namespace %s".printf (name));
@ -84,78 +137,74 @@ public class Pluie.Yaml.Register : GLib.Object
}
/**
*
* resolve full namespace for specified type
* @param type the type to retriew his full namespace
* @return the full namespace
*/
public string resolve_namespace_type (GLib.Type type)
public static string resolve_namespace_type (GLib.Type type)
{
var name = type.name ();
try {
Regex reg = new Regex ("([A-Z]{1}[a-z]+)");
var d = reg.split (type.name (), 0);
var rn = "";
var gb = "";
for (var i = 1; i < d.length; i+=2) {
rn += d[i];
if (namespaces.contains (rn)) {
rn += ".";
gb += d[i];
if (!is_resolved_ns (type)) {
var name = type.name ();
try {
Regex reg = new Regex ("([A-Z]{1}[a-z]+)");
var d = reg.split (type.name (), 0);
var rn = "";
var gb = "";
for (var i = 1; i < d.length; i+=2) {
rn += d[i];
if (namespaces.contains (rn)) {
rn += ".";
gb += d[i];
}
}
// case ENUM which ends with dot
if (rn.substring(-1) == ".") {
rn = name.splice (0, gb.length, rn);
}
name = rn;
}
// case ENUM which ends with dot
if (rn.substring(-1) == ".") {
rn = name.splice (0, gb.length, rn);
catch (GLib.RegexError e) {
of.error (e.message);
}
name = rn;
var serial = get_serial (type);
Yaml.dbg ("resolve_namespace_type %u (%s) => %s".printf (serial, type.name (), name));
ns_resolved.set (serial, name);
}
catch (GLib.RegexError e) {
of.error (e.message);
}
Yaml.dbg ("resolve_namespace_type %s => %s".printf (type.name (), name));
return name;
return get_resolved_ns (type);
}
/**
*
*/
public Gee.ArrayList<GLib.Type>? get_type_list (GLib.Type type)
private static Gee.ArrayList<GLib.Type> init_type_list ()
{
return rtype.get (type);
return new Gee.ArrayList<GLib.Type> ();
}
/**
*
* check if full namespace is already resolved for specified type
* @param type the type to check
*/
public bool add_type (GLib.Type owntype, ...)
private static bool is_resolved_ns (GLib.Type type)
{
bool done = true;
if (!this.is_registered (owntype)) {
rtype.set (owntype, this.init_type_list ());
}
var l = va_list();
while (done) {
GLib.Type? t = l.arg<GLib.Type> ();
if (t == null || t == Type.INVALID) {
break;
}
Yaml.dbg ("adding to %s type %s".printf (owntype.name (), t.name ()));
done = done && rtype.get (owntype).add (t);
}
return done;
return ns_resolved.has_key (get_serial (type));
}
/**
*
* get Quark related to specified type
*/
public bool is_registered (GLib.Type type)
private static Quark get_serial (Type type)
{
return rtype.has_key (type);
return Quark.from_string (type.name ());
}
/**
*
* retriew full namespace value for specified type
* @param type the type to retriew his full namespace
* @return the full namespace for specified type
*/
public bool is_registered_type (GLib.Type type, GLib.Type checktype)
private static string get_resolved_ns (GLib.Type type)
{
return this.is_registered (type) && rtype.get (type).contains (checktype);
return is_resolved_ns (type) ? ns_resolved.get (get_serial (type)) : type.name ();
}
}