using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using RainmeterStudio.Core.Model;
namespace RainmeterStudio.Core.Storage
{
///
/// A special type of tree that implements a very small subset of tree operations, and can be serialized
///
///
public class SerializableTree
{
///
/// Gets or sets the attached data
///
[XmlElement("data")]
public T Data { get; set; }
///
/// Gets or sets the list of children
///
[XmlArray("children"), XmlArrayItem("child")]
public List> Children { get; set; }
///
/// Initializes the serializable tree
///
public SerializableTree()
{
Children = new List>();
Data = default(T);
}
///
/// Initializes the serializable tree with specified data
///
/// Data
public SerializableTree(T data)
{
Children = new List>();
Data = data;
}
}
///
/// Extension methods for converting to and from serializable trees
///
public static class SerializableTreeExtensions
{
///
/// Converts tree into a serializable tree
///
/// Data type of tree
/// Root node
/// Serializable tree
public static SerializableTree AsSerializableTree(this Tree root)
{
// Convert current node
SerializableTree sRoot = new SerializableTree(root.Data);
// Add children
foreach (var child in root.Children)
sRoot.Children.Add(AsSerializableTree(child));
// Return root
return sRoot;
}
///
/// Converts serializable tree into a tree
///
/// Data type of tree
/// Root node
/// Tree
public static Tree AsTree(this SerializableTree root)
{
// Convert current node
Tree sRoot = new Tree(root.Data);
// Add children
foreach (var child in root.Children)
sRoot.Add(AsTree(child));
// Return root
return sRoot;
}
}
}