-
Notifications
You must be signed in to change notification settings - Fork 1
/
PacketFactory.hpp
67 lines (62 loc) · 2.51 KB
/
PacketFactory.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# pragma once
#include <optional>
namespace PacketUtils
{
std::unique_ptr<Packet> createPacket(std::optional<ClientID> clientID,
std::optional<RequestOp> op,
std::optional<ClientName> clientName,
std::optional<PublicKey> publicKey,
std::optional<FileName> fileName,
std::optional<FileContent> fileContent)
{
if (clientID && op && clientName)
{
return PacketWithClientName::createUnique(*clientID, *op, *clientName);
}
else if (clientID && clientName && publicKey)
{
return PacketWithPublicKey::createUnique(*clientID, *clientName,
*publicKey);
}
else if (clientID && op && fileName)
{
return PacketWithFileName::createUnique(*clientID, *op, *fileName);
}
else if (clientID && fileName && fileContent)
{
return PacketWithFile::createUnique(*clientID, *fileName, *fileContent);
}
else
{
throw std::invalid_argument("Invalid combination of arguments");
}
}
std::unique_ptr<Packet> createPacket(const ClientID &clientID,
const RequestOp &requestOp,
const ClientName &clientName)
{
return createPacket(clientID, requestOp, clientName, std::nullopt,
std::nullopt, std::nullopt);
}
std::unique_ptr<Packet> createPacket(const ClientID &clientID,
const ClientName &clientName,
const PublicKey &publicKey)
{
return createPacket(clientID, std::nullopt, clientName, publicKey,
std::nullopt, std::nullopt);
}
std::unique_ptr<Packet> createPacket(const ClientID &clientID,
const FileName &fileName,
const FileContent &fileContent)
{
return createPacket(clientID, std::nullopt, std::nullopt, std::nullopt,
fileName, fileContent);
}
std::unique_ptr<Packet> createPacket(const ClientID &clientID,
const RequestOp &requestOp,
const FileName &fileName)
{
return createPacket(clientID, requestOp, std::nullopt, std::nullopt,
fileName, std::nullopt);
}
} // namespace PacketUtils