Skip to content

Commit 081b960

Browse files
authored
ParmParse: Type Hints (#4364)
## Summary Today, we use the `ParmParse` object not only anymore to read text-based key-value pairs from input files and CLI options, but additionally and sometimes exclusively populate it heavily from the C++ and Python APIs directly. This proposes a concept to store the type of the latest `add` with each entry, which can help to expose `ParmParse` options, e.g., when exporting them to Python dictionaries and other file formats (YAML, TOML, JSON, XML, etc.). When a typehint is found, we can cast to the correct type automatically for the value, instead of returning a string. ## Additional background AMReX-Codes/pyamrex#417 (comment) ## Checklist The proposed changes: - [ ] fix a bug or incorrect behavior in AMReX - [x] add new capabilities to AMReX - [ ] changes answers in the test suite to more than roundoff level - [ ] are likely to significantly affect the results of downstream AMReX users - [ ] include documentation in the code and/or rst files, if appropriate
1 parent d6ab3a9 commit 081b960

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

Src/Base/AMReX_ParmParse.H

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
#include <AMReX_Config.H>
44

55
#include <AMReX_BLassert.H>
6+
#include <AMReX_Box.H>
67
#include <AMReX_Enum.H>
78
#include <AMReX_INT.H>
9+
#include <AMReX_IntVect.H>
810
#include <AMReX_IParser.H>
911
#include <AMReX_Parser.H>
1012
#include <AMReX_TypeTraits.H>
@@ -15,7 +17,9 @@
1517
#include <set>
1618
#include <string>
1719
#include <string_view>
20+
#include <type_traits>
1821
#include <unordered_map>
22+
#include <variant>
1923
#include <vector>
2024

2125
namespace amrex {
@@ -1633,6 +1637,17 @@ public:
16331637
// vector<vector<std::string>>.
16341638
std::vector<std::vector<std::string>> m_vals;
16351639
mutable Long m_count = 0;
1640+
std::variant<
1641+
std::string*,
1642+
bool*,
1643+
int*,
1644+
long*,
1645+
long long*,
1646+
amrex::IntVect*,
1647+
amrex::Box*,
1648+
float*,
1649+
double*
1650+
> m_typehint = static_cast<std::string*>(nullptr);
16361651
};
16371652
using Table = std::unordered_map<std::string, PP_entry>;
16381653

Src/Base/AMReX_ParmParse.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,8 @@ saddval (const std::string& name, const T& ref)
864864
auto& entry = g_table[name];
865865
entry.m_vals.emplace_back(std::vector<std::string>{val.str()});
866866
++entry.m_count;
867+
using T_ptr = std::decay_t<T>*;
868+
entry.m_typehint = static_cast<T_ptr>(nullptr);
867869
}
868870

869871
template <class T>
@@ -881,6 +883,8 @@ saddarr (const std::string& name, const std::vector<T>& ref)
881883
auto& entry = g_table[name];
882884
entry.m_vals.emplace_back(std::move(arr));
883885
++entry.m_count;
886+
using T_ptr = std::decay_t<T>*;
887+
entry.m_typehint = static_cast<T_ptr>(nullptr);
884888
}
885889

886890
// Initialize ParmParse.

0 commit comments

Comments
 (0)