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;
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
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
147 };
148} // namespace armarx::priorknowledge::core
static void addDataPaths(const std::string &dataPathList)
The CMakePackageFinder class provides an interface to the CMake Package finder capabilities.
virtual bool check(const DatasetType &dataset, const IDType &id) const =0
virtual bool checkAll(const DatasetType &dataset) const =0
virtual std::optional< FinderInfoType > find(const DatasetType &dataset, const IDType &id) const =0
virtual std::vector< FinderInfoType > findAll(const DatasetType &dataset) const =0
std::filesystem::path getFullPath(const std::filesystem::path &relPath) const
Definition FinderBase.h:131
FinderBase< IDType, FinderInfoType > Base
Definition FinderBase.h:123
DatasetFinderBase(const std::string &packageName, const std::filesystem::path &relDir)
Definition FinderBase.h:125
void setRelativePath(const std::filesystem::path &p)
Definition FinderBase.h:59
virtual std::vector< FinderInfoType > findAll() const =0
virtual bool check(const IDType &id) const =0
FinderBase & operator=(FinderBase &&)=default
std::filesystem::path getFullPath() const
Definition FinderBase.h:84
virtual bool accept(const std::filesystem::path &idPath) const =0
std::filesystem::path getAbsolutePackagePath() const
Definition FinderBase.h:77
FinderBase & operator=(const FinderBase &)=default
FinderBase(const FinderBase &)=default
std::filesystem::path getRelativePath() const
Definition FinderBase.h:71
virtual std::optional< FinderInfoType > find(const IDType &id) const =0
FinderBase(const std::string &packageName, const std::filesystem::path &relDir)
Definition FinderBase.h:24
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193