xref: /haiku/src/add-ons/kernel/file_systems/netfs/headers/shared/Request.h (revision c90684742e7361651849be4116d0e5de3a817194)
1 // Request.h
2 
3 #ifndef NET_FS_REQUEST_H
4 #define NET_FS_REQUEST_H
5 
6 #include <string.h>
7 
8 #include <HashString.h>
9 
10 #include "SLList.h"
11 
12 class RequestFlattener;
13 class RequestMemberVisitor;
14 class RequestUnflattener;
15 class RequestVisitor;
16 
17 // Data
18 struct Data {
19 	const void*	address;
20 	int32		size;
21 
22 	Data() : address(NULL), size(0) {}
23 
24 	void SetTo(const void* address, int32 size)
25 	{
26 		this->address = (size > 0 ? address : NULL);
27 		this->size = size;
28 	}
29 
30 	const void* GetData() const
31 	{
32 		return address;
33 	}
34 
35 	int32 GetSize() const
36 	{
37 		return size;
38 	}
39 };
40 
41 // StringData
42 struct StringData : Data {
43 	void SetTo(const char* string)
44 	{
45 		Data::SetTo(string, (string ? strlen(string) + 1 : 0));
46 	}
47 
48 	void SetTo(const HashString& string)
49 	{
50 		SetTo(string.GetString());
51 	}
52 
53 	const char* GetString() const
54 	{
55 		return (const char*)address;
56 	}
57 };
58 
59 // RequestMember
60 class RequestMember {
61 public:
62 								RequestMember();
63 	virtual						~RequestMember();
64 
65 	virtual	void				ShowAround(RequestMemberVisitor* visitor) = 0;
66 };
67 
68 // FlattenableRequestMember
69 class FlattenableRequestMember : public RequestMember {
70 public:
71 								FlattenableRequestMember();
72 	virtual						~FlattenableRequestMember();
73 
74 	virtual	status_t			Flatten(RequestFlattener* flattener) = 0;
75 	virtual	status_t			Unflatten(RequestUnflattener* unflattener) = 0;
76 };
77 
78 // RequestBuffer
79 class RequestBuffer : public SLListLinkImpl<RequestBuffer> {
80 private:
81 								RequestBuffer() {}
82 								~RequestBuffer() {}
83 
84 public:
85 	static	RequestBuffer*		Create(uint32 dataSize);
86 	static	void				Delete(RequestBuffer* buffer);
87 
88 			void*				GetData();
89 			const void*			GetData() const;
90 };
91 
92 // Request
93 class Request : public FlattenableRequestMember {
94 public:
95 								Request(uint32 type);
96 	virtual						~Request();
97 
98 			uint32				GetType() const;
99 
100 			void				AttachBuffer(RequestBuffer* buffer);
101 
102 	virtual	status_t			Accept(RequestVisitor* visitor) = 0;
103 
104 	virtual	status_t			Flatten(RequestFlattener* flattener);
105 	virtual	status_t			Unflatten(RequestUnflattener* unflattener);
106 
107 private:
108 			uint32				fType;
109 			SLList<RequestBuffer> fBuffers;
110 };
111 
112 // RequestMemberVisitor
113 class RequestMemberVisitor {
114 public:
115 								RequestMemberVisitor();
116 	virtual						~RequestMemberVisitor();
117 
118 	virtual	void				Visit(RequestMember* member, bool& data) = 0;
119 	virtual	void				Visit(RequestMember* member, int8& data) = 0;
120 	virtual	void				Visit(RequestMember* member, uint8& data) = 0;
121 	virtual	void				Visit(RequestMember* member, int16& data) = 0;
122 	virtual	void				Visit(RequestMember* member, uint16& data) = 0;
123 	virtual	void				Visit(RequestMember* member, int32& data) = 0;
124 	virtual	void				Visit(RequestMember* member, uint32& data) = 0;
125 	virtual	void				Visit(RequestMember* member, int64& data) = 0;
126 	virtual	void				Visit(RequestMember* member, uint64& data) = 0;
127 	virtual	void				Visit(RequestMember* member, Data& data) = 0;
128 	virtual	void				Visit(RequestMember* member,
129 									StringData& data) = 0;
130 	virtual	void				Visit(RequestMember* member,
131 									RequestMember& subMember) = 0;
132 	virtual	void				Visit(RequestMember* member,
133 									FlattenableRequestMember& subMember) = 0;
134 };
135 
136 #endif	// NET_FS_REQUEST_H
137