Skip to content

Commit eb4e258

Browse files
committed
Adapter registry as an iterator
Hide the vector of adapter names in an AdapterRegistry object. The vector is immutable and the new object has some traits of an iterator.
1 parent 60e789b commit eb4e258

File tree

3 files changed

+53
-10
lines changed

3 files changed

+53
-10
lines changed

source/loader/ur_adapter_registry.hpp

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ namespace loader {
1717

1818
class AdapterRegistry {
1919
public:
20-
std::vector<std::string> discoveredPlatforms;
21-
2220
AdapterRegistry() {
2321
std::optional<std::string> altPlatforms;
2422

@@ -29,24 +27,70 @@ class AdapterRegistry {
2927
logger::error(e.what());
3028
}
3129
if (!altPlatforms) {
32-
discoverKnownPlatforms();
30+
discoverKnownAdapters();
3331
}
3432

3533
std::stringstream ss(*altPlatforms);
3634
while (ss.good()) {
3735
std::string substr;
3836
getline(ss, substr, ',');
39-
discoveredPlatforms.emplace_back(substr);
37+
discovered_adapters.emplace_back(substr);
38+
}
39+
}
40+
41+
struct Iterator {
42+
using value_type = const std::string;
43+
using pointer = value_type *;
44+
45+
Iterator(pointer ptr) noexcept : current_adapter(ptr) {}
46+
47+
Iterator &operator++() noexcept {
48+
current_adapter++;
49+
return *this;
50+
}
51+
52+
Iterator operator++(int) {
53+
Iterator tmp = *this;
54+
++(*this);
55+
return tmp;
56+
}
57+
58+
bool operator!=(const Iterator &other) const noexcept {
59+
return this->current_adapter != other.current_adapter;
4060
}
61+
62+
const value_type operator*() const { return *this->current_adapter; }
63+
64+
private:
65+
pointer current_adapter;
66+
};
67+
68+
const std::string &operator[](size_t i) const {
69+
return discovered_adapters[i];
70+
}
71+
72+
bool empty() const noexcept { return discovered_adapters.size() == 0; }
73+
74+
size_t size() const noexcept { return discovered_adapters.size(); }
75+
76+
const Iterator begin() const noexcept {
77+
return Iterator(&(*discovered_adapters.cbegin()));
78+
}
79+
80+
const Iterator end() const noexcept {
81+
return Iterator(&(*discovered_adapters.cbegin()) +
82+
discovered_adapters.size());
4183
}
4284

4385
private:
86+
std::vector<std::string> discovered_adapters;
87+
4488
static constexpr std::array<const char *, 1> knownPlatformNames{
4589
MAKE_LIBRARY_NAME("ur_adapter_level_zero", "0")};
4690

47-
void discoverKnownPlatforms() {
91+
void discoverKnownAdapters() {
4892
for (const auto &path : knownPlatformNames) {
49-
discoveredPlatforms.emplace_back(path);
93+
discovered_adapters.emplace_back(path);
5094
}
5195
}
5296
};

source/loader/ur_loader.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@
66
*
77
*/
88
#include "ur_loader.hpp"
9-
#include "ur_adapter_registry.hpp"
109

1110
namespace loader {
1211
///////////////////////////////////////////////////////////////////////////////
1312
context_t *context;
1413

1514
///////////////////////////////////////////////////////////////////////////////
1615
ur_result_t context_t::init() {
17-
AdapterRegistry ar;
18-
19-
for (const auto &name : ar.discoveredPlatforms) {
16+
for (const auto &name : adapter_registry) {
2017
auto handle = LibLoader::loadAdapterLibrary(name.c_str());
2118
if (handle) {
2219
platforms.emplace_back(std::move(handle));

source/loader/ur_loader.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef UR_LOADER_HPP
1010
#define UR_LOADER_HPP 1
1111

12+
#include "ur_adapter_registry.hpp"
1213
#include "ur_ldrddi.hpp"
1314
#include "ur_lib_loader.hpp"
1415

@@ -30,6 +31,7 @@ class context_t {
3031
ur_api_version_t version = UR_API_VERSION_0_9;
3132

3233
platform_vector_t platforms;
34+
AdapterRegistry adapter_registry;
3335

3436
bool forceIntercept = false;
3537

0 commit comments

Comments
 (0)