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