Alexandria  2.14.1
Please provide a description of the project.
QualifiedName.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2020 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
25 #include <algorithm>
26 #include <boost/algorithm/string.hpp>
29 
30 namespace Euclid {
31 namespace XYDataset {
32 
34  : m_groups {std::move(groups)}, m_dataset_name {std::move(name)} {
35  for (auto& group : m_groups) {
36  if (group.empty() || group.find('/') != std::string::npos) {
37  throw Elements::Exception() << "Invalid group name : \"" << group << "\""
38  << " in qualified name : \"" << m_qualified_name.append(group).append("/") << "\"";
39  }
40  m_qualified_name.append(group).append("/");
41  }
42  if (m_dataset_name.empty() || m_dataset_name.find('/') != std::string::npos) {
43  throw Elements::Exception() << "Invalid name : \"" << m_qualified_name.append(m_dataset_name) << "\"";
44  }
45  m_qualified_name.append(m_dataset_name);
46 }
47 
49  std::vector<std::string> groups {};
50  boost::split(groups, qualified_name, boost::is_any_of("/"));
51  // The last one is the name, so we remove it
52  groups.pop_back();
53  return groups;
54 }
55 
56 std::string getName(const std::string& qualified_name) {
57  std::vector<std::string> groups {};
58  boost::split(groups, qualified_name, boost::is_any_of("/"));
59  return groups.back();
60 }
61 
63  : QualifiedName(getGroups(qualified_name), getName(qualified_name)) { }
64 
66  return m_groups;
67 }
68 
70  return m_dataset_name;
71 }
72 
74  return m_qualified_name;
75 }
76 
77 bool QualifiedName::belongsInGroup(const QualifiedName& group) const {
78  if (group.m_groups.size()+1 > this->m_groups.size()) {
79  return false;
80  }
81  bool group_check = std::equal(group.m_groups.begin(), group.m_groups.end(), this->m_groups.begin());
82  return group_check
83  ? group.m_dataset_name == this->m_groups.at(group.m_groups.size())
84  : false;
85 }
86 
87 size_t QualifiedName::hash() const {
88  if (m_hash == 0) {
89  std::hash<std::string> stringHash;
90  m_hash = stringHash(qualifiedName());
91  }
92  return m_hash;
93 }
94 
95 bool QualifiedName::operator<(const QualifiedName& other) const {
96  size_t thisHash = this->hash();
97  size_t otherHash = other.hash();
98  if (thisHash != otherHash) {
99  return thisHash < otherHash;
100  } else{
101  return this->qualifiedName() < other.qualifiedName();
102  }
103 }
104 
105 bool QualifiedName::operator==(const QualifiedName& other) const {
106  size_t thisHash = this->hash();
107  size_t otherHash = other.hash();
108  if (thisHash != otherHash) {
109  return false;
110  } else{
111  return this->qualifiedName() == other.qualifiedName();
112  }
113 }
114 
115 bool QualifiedName::operator!=(const QualifiedName& other) const {
116  return !(*this == other);
117 }
118 
119 std::ostream& operator<<(std::ostream& stream, const QualifiedName& qualified_name) {
120  stream << qualified_name.qualifiedName();
121  return stream;
122 }
123 
124 } // namespace XYDataset
125 } // end of namespace Euclid
Euclid::XYDataset::QualifiedName::groups
const std::vector< std::string > & groups() const
Returns the groups qualifying the name.
Definition: QualifiedName.cpp:65
std::string
STL class.
std::equal
T equal(T... args)
std::move
T move(T... args)
Euclid::XYDataset::QualifiedName::operator!=
bool operator!=(const QualifiedName &other) const
Checks if this QualifiedName is not equal with the parameter.
Definition: QualifiedName.cpp:115
Euclid::XYDataset::getGroups
std::vector< std::string > getGroups(const std::string &qualified_name)
Definition: QualifiedName.cpp:48
std::vector< std::string >
std::vector::size
T size(T... args)
Euclid::XYDataset::QualifiedName
Represents a name qualified with a set of groups.
Definition: QualifiedName.h:66
Euclid::XYDataset::QualifiedName::qualifiedName
const std::string & qualifiedName() const
Returns the qualified name as a string.
Definition: QualifiedName.cpp:73
Euclid::XYDataset::QualifiedName::operator==
bool operator==(const QualifiedName &other) const
Checks if this QualifiedName is equal with the parameter.
Definition: QualifiedName.cpp:105
Euclid::XYDataset::QualifiedName::m_dataset_name
std::string m_dataset_name
Definition: QualifiedName.h:201
Euclid::XYDataset::operator<<
std::ostream & operator<<(std::ostream &stream, const QualifiedName &qualified_name)
Make the QualifiedName streamable.
Definition: QualifiedName.cpp:119
Euclid::XYDataset::QualifiedName::QualifiedName
QualifiedName(std::vector< std::string > groups, std::string name)
Constructs a QualifiedName with the given group and name.
Definition: QualifiedName.cpp:33
std::string::at
T at(T... args)
std::ostream
STL class.
Exception.h
Euclid::XYDataset::QualifiedName::m_groups
std::vector< std::string > m_groups
Definition: QualifiedName.h:200
Elements::Exception
QualifiedName.h
Euclid::XYDataset::QualifiedName::m_qualified_name
std::string m_qualified_name
Definition: QualifiedName.h:202
std::vector::begin
T begin(T... args)
Euclid::XYDataset::QualifiedName::m_hash
size_t m_hash
Definition: QualifiedName.h:203
std::vector::end
T end(T... args)
Euclid::XYDataset::QualifiedName::datasetName
const std::string & datasetName() const
Returns the unqualified name.
Definition: QualifiedName.cpp:69
Euclid
Definition: InstOrRefHolder.h:29
Euclid::XYDataset::QualifiedName::belongsInGroup
bool belongsInGroup(const QualifiedName &group) const
Checks if the QualifiedName belongs in a given group.
Definition: QualifiedName.cpp:77
Euclid::XYDataset::getName
std::string getName(const std::string &qualified_name)
Definition: QualifiedName.cpp:56
Euclid::XYDataset::QualifiedName::hash
size_t hash() const
Returns the hash value of the QualifiedName.
Definition: QualifiedName.cpp:87
std::hash
Euclid::XYDataset::QualifiedName::operator<
bool operator<(const QualifiedName &other) const
Compares this QualifiedName with the parameter.
Definition: QualifiedName.cpp:95