Skip to content

Commit 8eb2d72

Browse files
committed
Use slightly more modern C++
1 parent ee79710 commit 8eb2d72

7 files changed

Lines changed: 162 additions & 152 deletions

File tree

examples/compiler_flags.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,20 @@ int main(int argc, char* argv[]) {
3535
};
3636

3737
for (const auto& target_name : targets) {
38-
const auto* target = archspec::get_target(target_name);
38+
auto target = archspec::get_target(target_name);
3939

4040
if (!target) {
4141
std::cout << target_name << ": NOT FOUND" << std::endl;
4242
continue;
4343
}
4444

4545
std::cout << "=== " << target_name << " ===" << std::endl;
46-
std::cout << " Vendor: " << target->vendor() << std::endl;
47-
std::cout << " Family: " << target->family() << std::endl;
46+
std::cout << " Vendor: " << target->get().vendor() << std::endl;
47+
std::cout << " Family: " << target->get().family() << std::endl;
4848
std::cout << std::endl;
4949

5050
for (const auto& [compiler, version] : compilers) {
51-
std::string flags = target->optimization_flags(compiler, version);
51+
std::string flags = target->get().optimization_flags(compiler, version);
5252

5353
std::cout << " " << std::setw(12) << std::left << (compiler + " " + version + ":");
5454
if (flags.empty()) {

include/archspec/detect.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55

66
#include "microarchitecture.hpp"
77
#include <string>
8+
#include <string_view>
89
#include <set>
910
#include <optional>
1011

1112
namespace archspec {
1213

1314
// Architecture constants
14-
constexpr const char* ARCH_X86_64 = "x86_64";
15-
constexpr const char* ARCH_AARCH64 = "aarch64";
16-
constexpr const char* ARCH_PPC64LE = "ppc64le";
17-
constexpr const char* ARCH_PPC64 = "ppc64";
18-
constexpr const char* ARCH_RISCV64 = "riscv64";
15+
constexpr std::string_view ARCH_X86_64 = "x86_64";
16+
constexpr std::string_view ARCH_AARCH64 = "aarch64";
17+
constexpr std::string_view ARCH_PPC64LE = "ppc64le";
18+
constexpr std::string_view ARCH_PPC64 = "ppc64";
19+
constexpr std::string_view ARCH_RISCV64 = "riscv64";
1920

2021
/**
2122
* Information detected about the host CPU
@@ -62,7 +63,7 @@ std::vector<const Microarchitecture*> compatible_microarchitectures(const Detect
6263
* Uses the specified architecture string
6364
*/
6465
std::vector<const Microarchitecture*> compatible_microarchitectures(const DetectedCpuInfo& info,
65-
const std::string& arch);
66+
std::string_view arch);
6667

6768
/**
6869
* Compare microarchitectures for sorting: prefers more ancestors and more features

include/archspec/microarchitecture.hpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
#define ARCHSPEC_MICROARCHITECTURE_HPP
55

66
#include <string>
7+
#include <string_view>
78
#include <vector>
89
#include <set>
910
#include <map>
1011
#include <memory>
1112
#include <functional>
13+
#include <optional>
1214
#include <cstdint>
1315

1416
namespace archspec {
@@ -63,7 +65,7 @@ class Microarchitecture {
6365
}
6466

6567
// Check if a feature is supported (includes aliases)
66-
bool has_feature(const std::string& feature) const;
68+
bool has_feature(std::string_view feature) const;
6769

6870
// Get all ancestors (parents and their parents recursively)
6971
std::vector<std::string> ancestors() const;
@@ -83,7 +85,7 @@ class Microarchitecture {
8385
bool operator>=(const Microarchitecture& other) const;
8486

8587
// Get optimization flags for a compiler
86-
std::string optimization_flags(const std::string& compiler, const std::string& version) const;
88+
std::string optimization_flags(std::string_view compiler, std::string_view version) const;
8789

8890
// Convert to/from string representation
8991
std::string to_string() const {
@@ -117,11 +119,11 @@ class MicroarchitectureDatabase {
117119
// Get the singleton instance
118120
static MicroarchitectureDatabase& instance();
119121

120-
// Get a microarchitecture by name
121-
const Microarchitecture* get(const std::string& name) const;
122+
// Get a microarchitecture by name (returns nullopt if not found)
123+
std::optional<std::reference_wrapper<const Microarchitecture>> get(std::string_view name) const;
122124

123125
// Check if a microarchitecture exists
124-
bool exists(const std::string& name) const;
126+
bool exists(std::string_view name) const;
125127

126128
// Get all known microarchitecture names
127129
std::vector<std::string> all_names() const;
@@ -132,8 +134,8 @@ class MicroarchitectureDatabase {
132134
}
133135

134136
// Load from various formats
135-
bool load_from_file(const std::string& path);
136-
bool load_from_string(const std::string& json_data);
137+
bool load_from_file(std::string_view path);
138+
bool load_from_string(std::string_view json_data);
137139

138140
// Get feature aliases
139141
const std::map<std::string, std::set<std::string>>& feature_aliases() const {
@@ -171,17 +173,17 @@ class MicroarchitectureDatabase {
171173
bool loaded_ = false;
172174

173175
// Allow JSON parsing helper access to private members
174-
friend bool load_json_into_database(MicroarchitectureDatabase& db,
175-
const std::string& json_data);
176+
friend bool load_json_into_database(MicroarchitectureDatabase& db, std::string_view json_data);
176177
};
177178

178179
// Convenience function to get a microarchitecture by name
179-
inline const Microarchitecture* get_target(const std::string& name) {
180+
inline std::optional<std::reference_wrapper<const Microarchitecture>>
181+
get_target(std::string_view name) {
180182
return MicroarchitectureDatabase::instance().get(name);
181183
}
182184

183185
// Create a generic microarchitecture with no features
184-
Microarchitecture generic_microarchitecture(const std::string& name);
186+
Microarchitecture generic_microarchitecture(std::string_view name);
185187

186188
} // namespace archspec
187189

src/archspec_c.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,21 @@ char* archspec_get_features(const char* name) {
7171
if (!name)
7272
return nullptr;
7373
const auto& db = archspec::MicroarchitectureDatabase::instance();
74-
const auto* target = db.get(name);
74+
auto target = db.get(name);
7575
if (!target)
7676
return nullptr;
77-
return to_c_string(join_features(target->features()));
77+
return to_c_string(join_features(target->get().features()));
7878
}
7979

8080
char* archspec_get_flags(const char* name, const char* compiler) {
8181
if (!name || !compiler)
8282
return nullptr;
8383
const auto& db = archspec::MicroarchitectureDatabase::instance();
84-
const auto* target = db.get(name);
84+
auto target = db.get(name);
8585
if (!target)
8686
return nullptr;
8787
// Use empty version string to get default flags
88-
std::string flags = target->optimization_flags(compiler, "");
88+
std::string flags = target->get().optimization_flags(compiler, "");
8989
if (flags.empty())
9090
return nullptr;
9191
return to_c_string(flags);
@@ -106,10 +106,10 @@ int archspec_has_feature(const char* name, const char* feature) {
106106
if (!name || !feature)
107107
return 0;
108108
const auto& db = archspec::MicroarchitectureDatabase::instance();
109-
const auto* target = db.get(name);
109+
auto target = db.get(name);
110110
if (!target)
111111
return 0;
112-
return target->has_feature(feature) ? 1 : 0;
112+
return target->get().has_feature(feature) ? 1 : 0;
113113
}
114114

115115
int archspec_host_has_feature(const char* feature) {

src/detect.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ std::string get_machine() {
6666
GetNativeSystemInfo(&sysInfo);
6767
switch (sysInfo.wProcessorArchitecture) {
6868
case PROCESSOR_ARCHITECTURE_AMD64:
69-
return ARCH_X86_64;
69+
return std::string(ARCH_X86_64);
7070
case PROCESSOR_ARCHITECTURE_ARM64:
71-
return ARCH_AARCH64;
71+
return std::string(ARCH_AARCH64);
7272
case PROCESSOR_ARCHITECTURE_INTEL:
7373
return "i686";
7474
default:
@@ -80,13 +80,13 @@ std::string get_machine() {
8080
size_t size = sizeof(brand);
8181
if (sysctlbyname("machdep.cpu.brand_string", brand, &size, nullptr, 0) == 0) {
8282
if (std::string(brand).find("Apple") != std::string::npos)
83-
return ARCH_AARCH64;
83+
return std::string(ARCH_AARCH64);
8484
}
8585
struct utsname uts;
8686
if (uname(&uts) == 0) {
8787
std::string machine = uts.machine;
8888
if (machine == "arm64")
89-
return ARCH_AARCH64;
89+
return std::string(ARCH_AARCH64);
9090
return machine;
9191
}
9292
return "unknown";
@@ -95,9 +95,9 @@ std::string get_machine() {
9595
if (uname(&uts) == 0) {
9696
std::string machine = uts.machine;
9797
if (machine == "arm64")
98-
return ARCH_AARCH64;
98+
return std::string(ARCH_AARCH64);
9999
if (machine == "amd64")
100-
return ARCH_X86_64;
100+
return std::string(ARCH_X86_64);
101101
return machine;
102102
}
103103
return "unknown";
@@ -378,7 +378,7 @@ std::optional<std::string> brand_string() {
378378

379379
namespace compatibility {
380380

381-
static bool is_in_family(const Microarchitecture& target, const std::string& family_name) {
381+
static bool is_in_family(const Microarchitecture& target, std::string_view family_name) {
382382
if (target.name() == family_name)
383383
return true;
384384
for (const auto& ancestor : target.ancestors()) {
@@ -416,11 +416,11 @@ bool check_aarch64(const DetectedCpuInfo& info, const Microarchitecture& target)
416416

417417
#if defined(__APPLE__)
418418
if (!info.name.empty()) {
419-
const auto* model = MicroarchitectureDatabase::instance().get(info.name);
419+
auto model = MicroarchitectureDatabase::instance().get(info.name);
420420
if (model) {
421421
if (target.name() == info.name)
422422
return true;
423-
for (const auto& ancestor : model->ancestors()) {
423+
for (const auto& ancestor : model->get().ancestors()) {
424424
if (ancestor == target.name())
425425
return true;
426426
}
@@ -446,7 +446,7 @@ bool check_riscv64(const DetectedCpuInfo& info, const Microarchitecture& target)
446446
} // namespace compatibility
447447

448448
std::vector<const Microarchitecture*> compatible_microarchitectures(const DetectedCpuInfo& info,
449-
const std::string& arch) {
449+
std::string_view arch) {
450450
std::vector<const Microarchitecture*> result;
451451
const auto& db = MicroarchitectureDatabase::instance();
452452

@@ -460,8 +460,8 @@ std::vector<const Microarchitecture*> compatible_microarchitectures(const Detect
460460
else if (arch == ARCH_RISCV64)
461461
checker = compatibility::check_riscv64;
462462
else {
463-
if (const auto* generic = db.get(arch))
464-
result.push_back(generic);
463+
if (auto generic = db.get(arch))
464+
result.push_back(&generic->get());
465465
return result;
466466
}
467467

@@ -471,8 +471,8 @@ std::vector<const Microarchitecture*> compatible_microarchitectures(const Detect
471471
}
472472

473473
if (result.empty()) {
474-
if (const auto* generic = db.get(arch))
475-
result.push_back(generic);
474+
if (auto generic = db.get(arch))
475+
result.push_back(&generic->get());
476476
}
477477

478478
return result;

0 commit comments

Comments
 (0)