FinderBase.h
Go to the documentation of this file.
1 #pragma once
2 
3 // STD/STL
4 #include <filesystem>
5 #include <optional>
6 #include <string>
7 
8 // Base Class
10 
11 // ArmarX
15 
17 {
18  template <class IDType, class FinderInfoType>
19  class FinderBase : public Logging
20  {
21  public:
22  FinderBase() = delete;
23 
24  FinderBase(const std::string& packageName, const std::filesystem::path& relDir) :
25  packageName(packageName), relPackageDataPath(relDir)
26  {
28  }
29 
30  void
32  {
33  CMakePackageFinder packageFinder(packageName);
34  absPackageDataDir = packageFinder.getDataDir();
35  if (absPackageDataDir.empty())
36  {
37  ARMARX_WARNING << "Could not find package '" << packageName << "'.";
38  throw LocalException() << "Could not find package '" << packageName << "'.";
39  }
40  else
41  {
42  ARMARX_INFO << "PriorKnowledgeFinder root directory: " << absPackageDataDir;
43 
44  // make sure this data path is available => e.g. for findArticulatedObjects
45  armarx::ArmarXDataPath::addDataPaths(std::vector<std::string>{absPackageDataDir});
46  }
47  }
48 
49  virtual ~FinderBase()
50  {
51  }
52 
53  FinderBase(FinderBase&&) = default;
54  FinderBase(const FinderBase&) = default;
55  FinderBase& operator=(FinderBase&&) = default;
56  FinderBase& operator=(const FinderBase&) = default;
57 
58  void
59  setRelativePath(const std::filesystem::path& p)
60  {
61  relPackageDataPath = p;
62  }
63 
64  std::string
66  {
67  return packageName;
68  }
69 
70  std::filesystem::path
72  {
73  return relPackageDataPath;
74  }
75 
76  std::filesystem::path
78  {
80  return absPackageDataDir;
81  }
82 
83  std::filesystem::path
84  getFullPath() const
85  {
87  return absPackageDataDir / packageName / relPackageDataPath;
88  }
89 
90  virtual bool accept(const std::filesystem::path& idPath) const = 0;
91  virtual bool checkAll() const = 0;
92  virtual bool check(const IDType& id) const = 0;
93  virtual std::optional<FinderInfoType> find(const IDType& id) const = 0;
94  virtual std::vector<FinderInfoType> findAll() const = 0;
95 
96  protected:
97  void
99  {
100  if (!std::filesystem::exists(absPackageDataDir))
101  {
102  ARMARX_ERROR << "PriorKnowledgeFinder is not initialized yet. Could not resolve "
103  "absolute path for package '"
104  << packageName << "'.";
105  }
106  }
107 
108  protected:
109  std::string packageName;
110 
111  private:
112  std::filesystem::path relPackageDataPath;
113  std::filesystem::path absPackageDataDir;
114  };
115 
116  template <class IDType,
117  class DatasetType,
118  class FinderInfoType> // TODO: concept DatasetFinderInfoType
119  class DatasetFinderBase : public FinderBase<IDType, FinderInfoType>
120  {
121 
122  public:
124 
125  DatasetFinderBase(const std::string& packageName, const std::filesystem::path& relDir) :
126  Base(packageName, relDir)
127  {
128  }
129 
130  std::filesystem::path
131  getFullPath(const std::filesystem::path& relPath) const
132  {
133  return Base::getFullPath() / relPath;
134  }
135 
136  virtual bool checkAll(const DatasetType& dataset) const = 0;
137  virtual bool check(const DatasetType& dataset, const IDType& id) const = 0;
138  virtual std::optional<FinderInfoType> find(const DatasetType& dataset,
139  const IDType& id) const = 0;
140  virtual std::vector<FinderInfoType> findAll(const DatasetType& dataset) const = 0;
141 
142  // Fix hidden virtual functions
143  using Base::checkAll;
144  using Base::check;
145  using Base::find;
146  using Base::findAll;
147  };
148 } // namespace armarx::priorknowledge::core
armarx::priorknowledge::core::DatasetFinderBase
Definition: FinderBase.h:119
armarx::priorknowledge::core::FinderBase::checkAll
virtual bool checkAll() const =0
armarx::priorknowledge::core::FinderBase
Definition: FinderBase.h:19
armarx::priorknowledge::core::FinderBase::getAbsolutePackagePath
std::filesystem::path getAbsolutePackagePath() const
Definition: FinderBase.h:77
armarx::priorknowledge::core::FinderBase::find
virtual std::optional< FinderInfoType > find(const IDType &id) const =0
armarx::priorknowledge::core::FinderBase::getRelativePath
std::filesystem::path getRelativePath() const
Definition: FinderBase.h:71
armarx::CMakePackageFinder
The CMakePackageFinder class provides an interface to the CMake Package finder capabilities.
Definition: CMakePackageFinder.h:53
armarx::priorknowledge::core::FinderBase::accept
virtual bool accept(const std::filesystem::path &idPath) const =0
armarx::priorknowledge::core::DatasetFinderBase::getFullPath
std::filesystem::path getFullPath(const std::filesystem::path &relPath) const
Definition: FinderBase.h:131
armarx::priorknowledge::core::FinderBase::findAll
virtual std::vector< FinderInfoType > findAll() const =0
armarx::priorknowledge::core::FinderBase::packageName
std::string packageName
Definition: FinderBase.h:109
armarx::priorknowledge::core::FinderBase::operator=
FinderBase & operator=(FinderBase &&)=default
armarx::priorknowledge::core::FinderBase::getPackageName
std::string getPackageName() const
Definition: FinderBase.h:65
armarx::priorknowledge::core::DatasetFinderBase::DatasetFinderBase
DatasetFinderBase(const std::string &packageName, const std::filesystem::path &relDir)
Definition: FinderBase.h:125
armarx::CMakePackageFinder::getDataDir
std::string getDataDir() const
Definition: CMakePackageFinder.h:176
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::priorknowledge::core::FinderBase::FinderBase
FinderBase()=delete
ExpressionException.h
CMakePackageFinder.h
armarx::Logging
Base Class for all Logging classes.
Definition: Logging.h:232
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::ArmarXDataPath::addDataPaths
static void addDataPaths(const std::string &dataPathList)
Definition: ArmarXDataPath.cpp:559
armarx::priorknowledge::core::FinderBase::check
virtual bool check(const IDType &id) const =0
armarx::priorknowledge::core::FinderBase::FinderBase
FinderBase(const std::string &packageName, const std::filesystem::path &relDir)
Definition: FinderBase.h:24
armarx::priorknowledge::core::FinderBase::setRelativePath
void setRelativePath(const std::filesystem::path &p)
Definition: FinderBase.h:59
armarx::priorknowledge::core
Definition: FinderBase.h:16
armarx::priorknowledge::core::DatasetFinderBase::find
virtual std::optional< FinderInfoType > find(const DatasetType &dataset, const IDType &id) const =0
Logging.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
ArmarXDataPath.h
armarx::priorknowledge::core::DatasetFinderBase::check
virtual bool check(const DatasetType &dataset, const IDType &id) const =0
armarx::priorknowledge::core::FinderBase::~FinderBase
virtual ~FinderBase()
Definition: FinderBase.h:49
armarx::priorknowledge::core::FinderBase::checkAbsolutePathIsValid
void checkAbsolutePathIsValid() const
Definition: FinderBase.h:98
armarx::priorknowledge::core::FinderBase::recalculateBasePath
void recalculateBasePath()
Definition: FinderBase.h:31
armarx::priorknowledge::core::FinderBase::getFullPath
std::filesystem::path getFullPath() const
Definition: FinderBase.h:84