-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactory.h
More file actions
59 lines (55 loc) Β· 2.11 KB
/
Factory.h
File metadata and controls
59 lines (55 loc) Β· 2.11 KB
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
#pragma once
#include "BaseProduct.h"
#include "ProductWithID.h"
#include "ConcreteProductA.h"
#include "ConcreteProductB.h"
#include "TempProduct.h"
#include "ProductTypes.h"
#include <tuple>
#include <memory>
#include <string>
#include <atomic>
#include <variant>
import LOGGER;
// General Factory template
// This template handles creation of products with default parameters
// This class will act as the base class for multiple argument calls
template<typename... Args>
class Factory {
public:
// General method to create a product; for cases with no specific type parameters
std::tuple<ProductVariant, Args...> creatProduct(Args... args) const {
static std::atomic<int> NoIDcounter{ 0 };
INFO("Creating NO ID TEMP PRODUCT\n");
NoIDcounter.fetch_add(1, std::memory_order_relaxed);
ProductVariant product = TempProduct();
return std::make_tuple(std::move(product), args...);
}
};
// Specialization for cases with int and std::string
// This specialization is used when creating ConcreteProductA
// This class will act as the recursive empty class when there is no argument left
template<>
class Factory<int, std::string> {
public:
std::tuple<ProductVariant, int, std::string> creatProduct(int id, int index, const std::string& name) const {
static std::atomic<int> Acounter{ 0 };
INFO("Creating PRODUCT A\n");
Acounter.fetch_add(1, std::memory_order_relaxed);
ProductVariant product = ConcreteProductA(id);
return std::make_tuple(std::move(product), index, name);
}
};
// Specialization for cases with int and other types (T)
// This specialization is used when creating ConcreteProductB
template<typename T>
class Factory<int, T> {
public:
std::tuple<ProductVariant, int, T> creatProduct(int id, T other) const {
static std::atomic<int> Bcounter{ 0 };
INFO("Creating PRODUCT B: \n");
Bcounter.fetch_add(1, std::memory_order_relaxed);
ProductVariant product = ConcreteProductB(id);
return std::make_tuple(std::move(product), id, other);
}
};