1 // Written in the D programming language.
2 
3 /**
4 A type for representing the qid 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.qid;
12 
13 private {
14 	import std.conv : to;
15 	import std..string : format;
16 	
17 	import styx2000.lowlevel.endianness;
18 	
19 	import styx2000.protobj.styxobject;
20 }
21 
22 public {
23 	import styx2000.protoconst.qids;
24 }
25 
26 /**
27 	A class that provides a type for the qid field in some Styx messages. Inherits methods from the StyxObject class. 
28 	See_Also:
29 		https://web.archive.org/web/20201029184954/https://powerman.name/Inferno/man/5/0intro.html
30 */
31 class Qid : StyxObject
32 {
33 	protected {
34 		// file type
35 		STYX_QID_TYPE _type;
36 		// version of file
37 		uint _vers;
38 		// unique path
39 		ulong _path;
40 		
41 		ubyte[] _representation;
42 	}
43 	
44 	/**
45 	A constructor that creates a unique qid number based on the parameters passed to it. 
46 	If it is called without parameters, then the type will be STYX_QID_TYPE.QTFILE and zero values for the remaining parameters. 
47     Params:
48 	type = Type of qid.
49     vers = Unique 32-bit version number for file or directory.
50     path = Unique 64-bit path number for file or directory.
51     
52     Typical usage:
53     ----
54     Qid qid = new Qid(STYX_QID_TYPE.QTFILE, 0, 12345678);
55     ----
56     */
57 	this(STYX_QID_TYPE type = STYX_QID_TYPE.QTFILE, uint vers = 0, ulong path = 0)
58 	{
59 		_type = type;
60 		_vers = vers;
61 		_path = path;
62 		_representation ~= toLEBytes!ubyte(type);
63 		_representation ~= toLEBytes!uint(vers);
64 		_representation ~= toLEBytes!ulong(path);
65 	}
66 	
67 	/// Get Qid type from Qid object
68 	STYX_QID_TYPE getType()
69 	{
70 		return _type;
71 	}
72 	
73 	/// Get version from Qid object
74 	uint getVers()
75 	{
76 		return _vers;
77 	}
78 	
79 	/// Get path from Qid object
80 	ulong getPath()
81 	{
82 		return _path;
83 	}
84 	
85 	/// Set type from STYX_QID_TYPE value
86 	void setType(STYX_QID_TYPE type)
87 	{
88 		_type = type;
89 		_representation[0] = cast(ubyte) type;
90 	}
91 	
92 	/// Set version from unsigned value
93 	void setVers(uint vers)
94 	{
95 		_vers = vers;
96 		_representation[1..5] = toLEBytes!uint(vers);
97 	}
98 	
99 	/// Set path from unsigned value
100 	void setPath(ulong path)
101 	{
102 		_path = path;
103 		_representation[5..13] = toLEBytes!ulong(path);
104 	}
105 	
106 	/// Pack to bytes array
107 	ubyte[] pack()
108 	{
109 		return _representation;
110 	}
111 	
112 	/// Unpack from bytes array
113 	void unpack(ubyte[] bytes...)
114 	{
115 		_representation = bytes[0..13];
116 		_type = cast(STYX_QID_TYPE) fromLEBytes!ubyte(bytes[0]);
117 		_vers = fromLEBytes!uint(bytes[1..5]);
118 		_path = fromLEBytes!ulong(bytes[5..13]);
119 	}
120 	
121 	/// Convenient string representation of an object for printing 
122 	override string toString()
123 	{
124 		return format(
125 			`Qid(type=%s, vers=%d, path=%d)`,
126 			_type.to!string,
127 			_vers,
128 			_path
129 		);
130 	}
131 	
132 	/// An alias for easier packing into a byte array without having to manually call the pack() method
133 	alias pack this;
134 }