1 // Written in the D programming language.
2 
3 /**
4 A type for representing the data object of the 9P / Styx protocol. 
5 
6 Copyright: LightHouse Software, 2021
7 License:   $(HTTP https://github.com/aquaratixc/ESL-License, Experimental Software License 1.0).
8 Authors:   Oleg Bakharev,
9 		   Ilya Pertsev
10 */
11 module styx2000.protobj.data;
12 
13 private {
14 	import std.conv : to;
15 	import std..string : format;
16 	
17 	import styx2000.protobj.styxobject;
18 }
19 
20 /**
21 	A class that provides a type for the data field in some Styx messages. Inherits methods from StyxObject class. 
22 	See_Also:
23 		https://web.archive.org/web/20201029184954/https://powerman.name/Inferno/man/5/0intro.html
24 */
25 class Data : StyxObject
26 {
27 	protected {		
28 		ubyte[] _representation;
29 	}
30 	
31 	/**
32 	A constructor that creates an object of the Data class with the given parameter in the form of byte array. 
33 	If called without parameters, then the default parameter is empty unsigned byte array. 
34     Params:
35     data = Array of unsigned bytes.
36     
37     Typical usage:
38     ----
39     Data data = new Data(0);
40     ----
41     */
42 	this(ubyte[] data = [])
43 	{
44 		_representation = data;
45 	}
46 	
47 	/// Get value from Data object
48 	ubyte[] getData()
49 	{
50 		return _representation;
51 	}
52 	
53 	/// Set value for Data from bytes array
54 	void setData(ubyte[] data...)
55 	{
56 		_representation = data;
57 	}
58 	
59 	/// Pack to bytes array
60 	ubyte[] pack()
61 	{
62 		return this.getData;
63 	}
64 	
65 	/// Unpack from bytes array
66 	void unpack(ubyte[] bytes...)
67 	{
68 		this.setData(bytes);
69 	}
70 	
71 	/// Convenient string representation of an object for printing 
72 	override string toString()
73 	{
74 		return format(`Data(bytes=%s)`, _representation.to!string);
75 	}
76 	
77 	/// An alias for easier packing into a byte array without having to manually call the pack() method
78 	alias pack this;
79 }