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 	/// An alias that allows you to call a getter method for NewFid
67 	alias getNewFid = getFid;	
68 	/// An alias that allows you to call a setter method for NewFid
69 	alias setNewFid = setFid;	
70 	
71 	/// Pack to bytes array
72 	ubyte[] pack()
73 	{
74 		return _representation;
75 	}
76 	
77 	/// Unpack from bytes array
78 	void unpack(ubyte[] bytes...)
79 	{
80 		_representation = bytes[0..4];
81 		_fid = fromLEBytes!uint(bytes[0..4]);
82 	}
83 	
84 	/// Convenient string representation of an object for printing 
85 	override string toString()
86 	{
87 		return format(
88 			`Fid(fid=%d)`,
89 			_fid
90 		);
91 	}
92 	
93 	/// An alias for easier packing into a byte array without having to manually call the pack() method
94 	alias pack this;
95 }