Cerata
A library to generate structural hardware designs
identifier.cc
1 // Copyright 2018-2019 Delft University of Technology
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "cerata/vhdl/identifier.h"
16 
17 #include <utility>
18 #include <string>
19 #include <vector>
20 
21 namespace cerata::vhdl {
22 
23 std::string Identifier::ToString() const {
24  std::string ret;
25  for (const auto &p : parts_) {
26  ret += p;
27  if (separator_) {
28  if (p != parts_.back()) {
29  ret += *separator_;
30  }
31  }
32  }
33  return ret;
34 }
35 
36 Identifier::Identifier(std::initializer_list<std::string> parts, std::optional<char> sep) : separator_(std::move(sep)) {
37  for (const auto &p : parts) {
38  parts_.push_back(p);
39  }
40 }
41 
42 Identifier::Identifier(std::deque<std::string> parts, std::optional<char> sep) : separator_(std::move(sep)) {
43  parts_ = std::move(parts);
44 }
45 
46 Identifier &Identifier::append(const std::string &part) {
47  if (!part.empty()) {
48  parts_.push_back(part);
49  }
50  return *this;
51 }
52 
53 Identifier &Identifier::prepend(const std::string &part) {
54  if (!part.empty()) {
55  parts_.push_front(part);
56  }
57  return *this;
58 }
59 
65 Identifier &Identifier::operator+=(const std::string &rhs) {
66  return append(rhs);
67 }
68 
69 Identifier Identifier::operator+(const std::string &rhs) const {
70  Identifier ret = *this;
71  ret.append(rhs);
72  return ret;
73 }
74 
75 } // namespace cerata::vhdl
cerata::vhdl::Identifier::prepend
Identifier & prepend(const std::string &part)
Append a part to the Identifier.
Definition: identifier.cc:53
cerata::vhdl::Identifier::operator+
Identifier operator+(const std::string &rhs) const
Create a copy and add a new part to the Identifier.
Definition: identifier.cc:69
cerata::vhdl::Identifier::append
Identifier & append(const std::string &part)
Append a part to the Identifier.
Definition: identifier.cc:46
cerata::vhdl::Identifier::ToString
std::string ToString() const
Return a human-readable string of the identifier.
Definition: identifier.cc:23
cerata::vhdl::Identifier::operator+=
Identifier & operator+=(const std::string &rhs)
Append a part to the Identifier.
Definition: identifier.cc:65
cerata::vhdl::Identifier
A VHDL Identifier convenience structure.
Definition: identifier.h:30
cerata::vhdl
Contains everything related to the VHDL back-end.
Definition: architecture.cc:31