-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn227.cpp
120 lines (101 loc) · 5.48 KB
/
n227.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <string>
#include <type_traits>
namespace n227_radio_factory
{
struct Capacitor
{
std::string capacitance;
};
struct Resistor
{
double resistance;
};
template <typename ComponentType, size_t BinCapacity, typename RequestPolicy = void>
class ComponentBin
{
public:
struct RequestForm
{
std::string description;
int quantity_requested;
};
// Provide a default RequestPolicy if none is specified.
template <typename U = RequestPolicy>
typename std::enable_if<std::is_same_v<U, void>, ComponentType>::type
retrieve(RequestForm const &request)
{
// Default policy: No special handling.
// TODO: Implement the logic to retrieve the component based on the request for the default policy.
return ComponentType{};
}
// Overload for custom RequestPolicy. This demonstrates using the third template argument.
template <typename U = RequestPolicy>
typename std::enable_if<!std::is_same_v<U, void>, ComponentType>::type
retrieve(RequestForm const &request)
{
// Apply the custom RequestPolicy.
return RequestPolicy::handle(request);
}
static constexpr size_t capacity() { return BinCapacity; }
};
// Example of a custom RequestPolicy - demonstrates the third template argument.
struct SeniorStaffOnlyPolicy
{
static Resistor handle(const ComponentBin<Resistor, 10, SeniorStaffOnlyPolicy>::RequestForm &request)
{
// Simulate a check if the requester has the required seniority (not implemented).
if (request.description.find("Urgent") != std::string::npos)
{
// TODO: Implement the logic for senior staff to retrieve resistors, potentially overriding normal procedures.
return Resistor{};
}
else
{
std::cout << "Request flagged for standard pro cessing." << std::endl;
return {}; // Or some default/error handling
}
}
};
// Explicit instantiation for integers as ComponentType, a capacity of 5, and the default policy.
// template int ComponentBin<int, 5>::retrieve(ComponentBin<int, 5>::RequestForm const &);
template int n227_radio_factory::ComponentBin<int, 5, void>::retrieve<void>(n227_radio_factory::ComponentBin<int, 5, void>::RequestForm const&);
// Explicit instantiation for Resistor, capacity of 10, and the SeniorStaffOnlyPolicy. Demonstrates the third template argument.
// template Resistor ComponentBin<Resistor, 10, SeniorStaffOnlyPolicy>::retrieve(ComponentBin<Resistor, 10, SeniorStaffOnlyPolicy>::RequestForm const &);
template n227_radio_factory::Resistor n227_radio_factory::ComponentBin<n227_radio_factory::Resistor, 10, n227_radio_factory::SeniorStaffOnlyPolicy>::retrieve<n227_radio_factory::SeniorStaffOnlyPolicy>(n227_radio_factory::ComponentBin<n227_radio_factory::Resistor, 10, n227_radio_factory::SeniorStaffOnlyPolicy>::RequestForm const&);
} // namespace n227_radio_factory
void run_tests()
{
using namespace n227_radio_factory;
// Test case 1: Integer bin with default policy
ComponentBin<int, 5> int_bin;
ComponentBin<int, 5>::RequestForm int_request = {"Needed for circuit A", 2};
int retrieved_int = int_bin.retrieve(int_request);
static_assert(std::is_same_v<decltype(retrieved_int), int>, "Test Case 1 Failed: Incorrect return type");
// The actual value depends on the TODO implementation.
// Test case 2: Resistor bin with SeniorStaffOnlyPolicy
ComponentBin<Resistor, 10, SeniorStaffOnlyPolicy> resistor_bin_senior;
ComponentBin<Resistor, 10, SeniorStaffOnlyPolicy>::RequestForm resistor_request_urgent = {"Urgent resistors for assembly line", 5};
Resistor retrieved_resistor_senior_urgent = resistor_bin_senior.retrieve(resistor_request_urgent);
static_assert(std::is_same_v<decltype(retrieved_resistor_senior_urgent), Resistor>, "Test Case 2 Failed: Incorrect return type");
// The behavior depends on the TODO implementation within SeniorStaffOnlyPolicy.
ComponentBin<Resistor, 10, SeniorStaffOnlyPolicy>::RequestForm resistor_request_standard = {"Standard resistor request", 3};
Resistor retrieved_resistor_senior_standard = resistor_bin_senior.retrieve(resistor_request_standard);
static_assert(std::is_same_v<decltype(retrieved_resistor_senior_standard), Resistor>, "Test Case 2b Failed: Incorrect return type");
// Test case 3: Capacitor bin with default policy (demonstrating a different ComponentType)
ComponentBin<Capacitor, 20> capacitor_bin;
ComponentBin<Capacitor, 20>::RequestForm capacitor_request = {"Capacitors for filtering", 10};
Capacitor retrieved_capacitor = capacitor_bin.retrieve(capacitor_request);
static_assert(std::is_same_v<decltype(retrieved_capacitor), Capacitor>, "Test Case 3 Failed: Incorrect return type");
// The actual content of the capacitor depends on the TODO implementation.
// Static assert to check the bin capacity (demonstrates the second template argument).
static_assert(ComponentBin<float, 100>::capacity() == 100, "Test Case 4 Failed: Incorrect capacity");
static_assert(ComponentBin<Resistor, 50>::capacity() == 50, "Test Case 5 Failed: Incorrect capacity");
}
int main()
{
std::cout << "Running Tests..." << std::endl;
run_tests();
std::cout << "Tests Complete." << std::endl;
return 0;
}