1 // Written in the D programming language.
2 
3 /**
4 A type for representing the fid 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.fid;
12 
13 private {
14 	import std..string : format;
15 
16 	import styx2000.lowlevel.endianness;
17 	
18 	import styx2000.protoconst.base : STYX_NOFID;
19 	
20 	import styx2000.protobj.styxobject;
21 }
22 
23 /**
24 	A class that provides a type for the fid field in some Styx messages. Inherits methods from the StyxObject class. 
25 	See_Also:
26 		https://web.archive.org/web/20201029184954/https://powerman.name/Inferno/man/5/0intro.html
27 */
28 class Fid : StyxObject
29 {
30 	protected {
31 		uint _fid;
32 		
33 		ubyte[] _representation;
34 	}
35 	
36 	/**
37 	A constructor that creates an object of the Fid class with the given parameter in the form of some integer value representing fid. 
38 	If called without parameters, then the default parameter is the STYX_NOFID value from styx2000.protoconst.base. 
39     Params:
40     fid = Unique 32-bit value assigned by the Styx client.
41     
42     Typical usage:
43     ----
44     Fid fid = new Fid(0);
45     ----
46     */
47 	this(uint fid = STYX_NOFID)
48 	{
49 		_fid = fid;
50 		_representation = toLEBytes!uint(fid);
51 	}
52 	
53 	/// Get unsigned value from Fid object
54 	uint getFid()
55 	{
56 		return _fid;
57 	}
58 	
59 	/// Set value for data from unsigned value
60 	void setFid(uint fid)
61 	{
62 		_fid = fid;
63 		_representation = toLEBytes!uint(fid);
64 	}
65 	
66 	/// Pack to bytes array
67 	ubyte[] pack()
68 	{
69 		return _representation;
70 	}
71 	
72 	/// Unpack from bytes array
73 	void unpack(ubyte[] bytes...)
74 	{
75 		_representation = bytes[0..4];
76 		_fid = fromLEBytes!uint(bytes[0..4]);
77 	}
78 	
79 	/// Convenient string representation of an object for printing 
80 	override string toString()
81 	{
82 		return format(
83 			`Fid(fid=%d)`,
84 			_fid
85 		);
86 	}
87 	
88 	/// An alias for easier packing into a byte array without having to manually call the pack() method
89 	alias pack this;
90 }