Line data Source code
1 : // SPDX-License-Identifier: Apache-2.0
2 : /**
3 : * Copyright (C) 2021 Jihoon Lee <jhoon.it.lee@samsung.com>
4 : *
5 : * @file connection.cpp
6 : * @date 23 Nov 2021
7 : * @see https://github.com/nnstreamer/nntrainer
8 : * @author Jihoon Lee <jhoon.it.lee@samsung.com>
9 : * @bug No known bugs except for NYI items
10 : * @brief Connection class and related utility functions
11 : */
12 : #include <connection.h>
13 :
14 : #include <common_properties.h>
15 : #include <stdexcept>
16 :
17 : namespace {}
18 :
19 : namespace nntrainer {
20 8839 : Connection::Connection(const std::string &layer_name, unsigned int idx) :
21 8839 : index(idx),
22 17678 : name(props::Name(layer_name).get()) {}
23 :
24 7589 : Connection::Connection(const std::string &string_representation) {
25 : auto &sr = string_representation;
26 : auto pos = sr.find_first_of('(');
27 : auto idx = 0u;
28 7589 : auto name_part = sr.substr(0, pos);
29 :
30 7589 : if (pos != std::string::npos) {
31 3967 : NNTR_THROW_IF(sr.back() != ')', std::invalid_argument)
32 : << "failed to parse connection invalid format: " << sr;
33 :
34 3963 : auto idx_part = std::string(sr.begin() + pos + 1, sr.end() - 1);
35 : /// idx_part must not have '(' or ')' inside
36 7928 : NNTR_THROW_IF(idx_part.find_first_of('(') != std::string::npos or
37 : idx_part.find_first_of(')') != std::string::npos,
38 : std::invalid_argument)
39 : << "failed to parse connection invalid format: " << sr;
40 3959 : idx = str_converter<uint_prop_tag, unsigned>::from_string(idx_part);
41 : }
42 :
43 7582 : index = idx;
44 22745 : name = props::Name(name_part);
45 7589 : }
46 :
47 17100 : Connection::Connection(const Connection &rhs) = default;
48 0 : Connection &Connection::operator=(const Connection &rhs) = default;
49 4889 : Connection::Connection(Connection &&rhs) noexcept = default;
50 0 : Connection &Connection::operator=(Connection &&rhs) noexcept = default;
51 :
52 4201 : bool Connection::operator==(const Connection &rhs) const noexcept {
53 4201 : return index == rhs.index and name == rhs.name;
54 : }
55 :
56 11618 : std::string Connection::toString() const {
57 11618 : std::stringstream ss;
58 23236 : ss << getName() << '(' << getIndex() << ')';
59 11618 : return ss.str();
60 11618 : }
61 :
62 : }; // namespace nntrainer
|