1 // Written in the D programming language.
2 
3 /**
4 A type for representing the tag 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.tag;
12 
13 private {
14 	import std..string : format;
15 	
16 	import styx2000.lowlevel.endianness;
17 	
18 	import styx2000.protoconst.base : STYX_NOTAG;
19 	
20 	import styx2000.protobj.styxobject;
21 }
22 
23 /**
24 	A class that provides a type for the tag field in some Styx messages. Inherits methods from the Fid class and the StyxObject class. 
25 	See_Also:
26 		https://web.archive.org/web/20201029184954/https://powerman.name/Inferno/man/5/0intro.html
27 */
28 class Tag : StyxObject
29 {
30 	protected {
31 		ushort _tag;
32 		
33 		ubyte[] _representation;
34 	}
35 	
36 	/**
37 	A constructor that creates an object of the Afid 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_NOTAG value from styx2000.protoconst.base. 
39     Params:
40     tag = Unique 16-bit value for tagging messages.
41     
42     Typical usage:
43     ----
44     Tag tag = new Tag(10);
45     ----
46     */
47 	this(ushort tag = STYX_NOTAG)
48 	{
49 		_tag = tag;
50 		_representation = toLEBytes!ushort(tag);
51 	}
52 	
53 	/// Get as unsigned value
54 	ushort getTag()
55 	{
56 		return _tag;
57 	}
58 	
59 	/// Set Tag from unsigned value
60 	void setTag(ushort tag)
61 	{
62 		_tag = tag;
63 		_representation = toLEBytes!ushort(tag);
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..2];
76 		_tag = fromLEBytes!ushort(bytes[0..2]);
77 	}
78 	
79 	/// Convenient string representation of an object for printing 
80 	override string toString()
81 	{
82 		return format(`Tag(tag=0x%0.4x)`, _tag);
83 	}
84 	
85 	/// An alias for easier packing into a byte array without having to manually call the pack() method
86 	alias pack this;
87 }