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