ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Support.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "Support.hpp"
18
19#include <memory>
20#include <cassert>
21#include <fstream>
22#include <stdexcept>
23
24namespace
25{
26
27template <typename T>
28std::unique_ptr<T> open_fstream(const std::string &path, std::ios_base::openmode mode)
29{
30 if (path == "-")
31 {
32 return nullptr;
33 }
34
35 auto stream = std::make_unique<T>(path.c_str(), mode);
36 if (!stream->is_open())
37 {
38 throw std::runtime_error{"Failed to open " + path};
39 }
40 return stream;
41}
42
43} // namespace
44
45std::string Cmdline::get(unsigned int index) const
46{
47 if (index >= _argc)
48 throw std::runtime_error("Argument index out of bound");
49
50 return std::string(_argv[index]);
51}
52
53std::string Cmdline::get_or(unsigned int index, const std::string &s) const
54{
55 if (index >= _argc)
56 return s;
57
58 return std::string(_argv[index]);
59}
60
61std::unique_ptr<UI> make_ui(const Cmdline &cmdargs)
62{
63 auto iocfg = std::make_unique<UI>();
64
65 auto in = open_fstream<std::ifstream>(cmdargs.get_or(0, "-"), std::ios::in | std::ios::binary);
66 iocfg->in(std::move(in));
67
68 auto out = open_fstream<std::ofstream>(cmdargs.get_or(1, "-"), std::ios::out | std::ios::binary);
69 iocfg->out(std::move(out));
70
71 return iocfg;
72}
std::string get(unsigned int index) const
Definition Support.cpp:45
std::string get_or(unsigned int index, const std::string &) const
Definition Support.cpp:53
std::unique_ptr< UI > make_ui(const Cmdline &cmdargs)
Definition Support.cpp:61