Line data Source code
1 : // SPDX-License-Identifier: Apache-2.0
2 : /**
3 : * Copyright (C) 2020 Parichay Kapoor <pk.kapoor@samsung.com>
4 : *
5 : * @file databuffer_factory.cpp
6 : * @date 11 October 2020
7 : * @see https://github.com/nnstreamer/nntrainer
8 : * @author Parichay Kapoor <pk.kapoor@samsung.com>
9 : * @bug No known bugs except for NYI items
10 : * @brief This is the databuffer factory.
11 : */
12 :
13 : #include <databuffer_factory.h>
14 :
15 : #include <data_producer.h>
16 : #include <func_data_producer.h>
17 : #include <nntrainer_error.h>
18 : #include <raw_file_data_producer.h>
19 :
20 : namespace nntrainer {
21 :
22 : /**
23 : * @brief Factory creator with constructor
24 : */
25 2 : std::unique_ptr<DataBuffer> createDataBuffer(DatasetType type) {
26 :
27 2 : std::unique_ptr<DataProducer> dp;
28 2 : switch (type) {
29 0 : case DatasetType::FILE:
30 2 : dp = std::make_unique<RawFileDataProducer>();
31 : break;
32 2 : case DatasetType::UNKNOWN:
33 : [[fallthrough]];
34 : default:
35 2 : throw std::invalid_argument("Unsupported constructor type for the dataset");
36 : }
37 :
38 0 : return std::make_unique<DataBuffer>(std::move(dp));
39 : }
40 :
41 : /**
42 : * @brief Factory creator with constructor for dataset
43 : */
44 211 : std::unique_ptr<DataBuffer> createDataBuffer(DatasetType type,
45 : const char *file) {
46 211 : NNTR_THROW_IF(file == nullptr, std::invalid_argument)
47 : << "file shall not be null, use empty constructor instead";
48 :
49 211 : std::unique_ptr<DataProducer> dp;
50 211 : switch (type) {
51 211 : case DatasetType::FILE:
52 211 : dp = std::make_unique<RawFileDataProducer>(file);
53 : break;
54 0 : case DatasetType::UNKNOWN:
55 : [[fallthrough]];
56 : default: {
57 0 : std::stringstream ss;
58 : ss << "Unsupported constructor type for the dataset of type: "
59 0 : << static_cast<int>(type);
60 0 : throw std::invalid_argument(ss.str().c_str());
61 0 : }
62 : };
63 :
64 418 : return std::make_unique<DataBuffer>(std::move(dp));
65 : }
66 :
67 : /**
68 : * @brief Factory creator with constructor for dataset
69 : */
70 35 : std::unique_ptr<DataBuffer> createDataBuffer(DatasetType type, datagen_cb cb,
71 : void *user_data) {
72 :
73 35 : std::unique_ptr<DataProducer> dp;
74 35 : switch (type) {
75 35 : case DatasetType::GENERATOR:
76 35 : dp = std::make_unique<FuncDataProducer>(cb, user_data);
77 : break;
78 0 : case DatasetType::UNKNOWN:
79 : [[fallthrough]];
80 : default: {
81 0 : std::stringstream ss;
82 : ss << "Unsupported constructor type for the dataset of type: "
83 0 : << static_cast<int>(type);
84 0 : throw std::invalid_argument(ss.str().c_str());
85 0 : }
86 : };
87 :
88 70 : return std::make_unique<DataBuffer>(std::move(dp));
89 : }
90 :
91 : } // namespace nntrainer
|