mason/src/source.cpp

130 lines
3.0 KiB
C++

#include "source.h"
Source::Source(const std::string& path) :
path(path),
target(std::nullopt),
name(std::nullopt)
{}
Source::Source(const std::string& path, const std::string& target) :
path(path),
target(target),
name(std::nullopt)
{}
Source::Source(const std::string& path, const std::string& target, const std::string& name) :
path(path),
target(target),
name(name)
{}
Source::Source(const std::string& path, const std::optional<std::string>& target, const std::optional<std::string>& name) :
path(path),
target(target),
name(name)
{}
void Source::clearName() {
name = std::nullopt;
}
void Source::clearTarget() {
target = std::nullopt;
}
std::string Source::getPath() const {
return path;
}
std::string Source::getTarget() const {
if (target.has_value())
return target.value();
else
return "";
}
std::string Source::getName() const {
if (name.has_value())
return name.value();
else
return "";
}
std::optional<std::string> Source::getOptionalTarget() const {
return target;
}
std::optional<std::string> Source::getOptionalName() const {
return name;
}
bool Source::hasExplicitTarget() const {
return target.has_value();
}
bool Source::hasExplicitName() const {
return name.has_value();
}
void Source::setPath(const std::string& path) {
Source::path = path;
}
void Source::setTarget(const std::string& target) {
Source::target = target;
}
void Source::setName(const std::string& name) {
Source::name = name;
}
bool Source::operator < (const Source& other) const {
if (path < other.path)
return true;
else if (path > other.path)
return false;
else {
if (hasExplicitTarget()) {
if (other.hasExplicitTarget()) {
if (target.value() < other.target.value())
return true;
else if (target.value() > other.target.value()) {
return false;
} else {
if (hasExplicitName()) {
if (other.hasExplicitName()) {
return name.value() < other.name.value();
} else
return false;
} else
return other.hasExplicitName();
}
} else
return false;
} else {
if (other.hasExplicitTarget())
return true;
else {
if (hasExplicitName()) {
if (other.hasExplicitName()) {
return name.value() < other.name.value();
} else
return false;
} else
return other.hasExplicitName();
}
}
}
}
std::string Source::print() const {
std::string result = path;
if (hasExplicitTarget())
result += " -> " + target.value();
if (hasExplicitName())
result += " (as " + name.value() + ")";
return result;
}