1 // Written in the D programming language.
2 
3 /**
4 A type for representing the mode 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.mode;
12 
13 private {
14 	import std.conv : to;
15 	import std..string : format;
16 	
17 	import styx2000.protobj.styxobject;
18 }
19 
20 public {
21 	import styx2000.protoconst.modes;
22 }
23 
24 /**
25 	A class that provides a type for the mode field in some Styx messages. Inherits methods from StyxObject class. 
26 	See_Also:
27 		https://web.archive.org/web/20201029184954/https://powerman.name/Inferno/man/5/0intro.html
28 */
29 class Mode : StyxObject
30 {
31 	protected {
32 		STYX_FILE_MODE _mode;
33 		ubyte[] _representation;
34 	}
35 	
36 	/**
37 	A constructor that creates an object of the Mode class with the given parameter in the form of STYX_FILE_MODE type. 
38 	If called without parameters, then the default parameter is STYX_FILE_MODE.OREAD. 
39     Params:
40     mode = File mode.
41     
42     Typical usage:
43     ----
44     Mode mode = new Mode(STYX_FILE_MODE.OWRITE);
45     ----
46     */
47 	this(STYX_FILE_MODE mode = STYX_FILE_MODE.OREAD)
48 	{
49 		_mode = mode;
50 		_representation = [cast(ubyte) mode];
51 	}
52 	
53 	/// Get mode from Mode object
54 	STYX_FILE_MODE getMode()
55 	{
56 		return _mode;
57 	}
58 	
59 	/// Set mode from STYX_FILE_MODE value
60 	void setMode(STYX_FILE_MODE mode)
61 	{
62 		_mode = mode;
63 		_representation = [cast(ubyte) mode];
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]];
76 		_mode = cast(STYX_FILE_MODE) bytes[0];
77 	}
78 	
79 	/// Convenient string representation of an object for printing 
80 	override string toString()
81 	{
82 		return format(
83 			`Mode(mode=%s)`,
84 			_mode.to!string
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 }