summaryrefslogtreecommitdiffstats
path: root/source/core/slang-name-value.h
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2023-04-29 09:24:26 -0400
committerGitHub <noreply@github.com>2023-04-29 09:24:26 -0400
commit19c0866b050a022406867aa650302f4efbf8e010 (patch)
treef5ed4e1f5d27865518daf81c7e861b4908186b23 /source/core/slang-name-value.h
parentc571bcb025009f9c662e8d631fa49dbfed560287 (diff)
CommandOptions (#2856)
* WIP CommandOptions * Fix some output issues. * Simplify word wrapping. * Add file extensions. * Change how lookup takes place. Add appendSplit functions to StringUtil. Make Categories hold the index range of their options. * Small improvement. * Lookup with partial option names. * Associate user values. * Encoding flags in the name. * Refactor setting up of command options. * Use CommandOptions in slang-options. * Remove old help text. * Cache the CommandOptions on the Session. * Range checking. Fix bug in the Options handling. * Extra checks for validity. * Get categories directly. * Slight improvements over output. * Added NameValue types. * Fix typo. Remove some now unused diagnostics. Fix diagnostic in testing, as output has changed. * Add minimal usage message. * Remove platform executable extension from diagnostics output. * Some improvements around getting names from NameValue types. * Improve some option descriptions. * Small fixes.
Diffstat (limited to 'source/core/slang-name-value.h')
-rw-r--r--source/core/slang-name-value.h76
1 files changed, 76 insertions, 0 deletions
diff --git a/source/core/slang-name-value.h b/source/core/slang-name-value.h
new file mode 100644
index 000000000..a50e149a8
--- /dev/null
+++ b/source/core/slang-name-value.h
@@ -0,0 +1,76 @@
+#ifndef SLANG_CORE_NAME_VALUE_H
+#define SLANG_CORE_NAME_VALUE_H
+
+#include "slang-basic.h"
+
+#include "slang-array-view.h"
+
+namespace Slang
+{
+
+// When associating values with names we use this type
+typedef int32_t ValueInt;
+
+struct NameValue
+{
+ ValueInt value;
+ const char* name;
+};
+
+struct NamesValue
+{
+ ValueInt value;
+ const char* names; ///< Can hold comma delimited list of names
+
+};
+
+struct NamesDescriptionValue
+{
+ ValueInt value;
+ const char* names; ///< Can hold comma delimited list of names
+ const char* description; ///< Optional description, can hold null ptr or empty string if not defined
+};
+
+struct NameValueUtil
+{
+ enum class NameKind
+ {
+ First, ///< Only the first name if there is more than one
+ All, ///< All valid associated names
+ };
+
+ /// Use the default type to infer the actual type desired
+ template <typename T>
+ static T findValue(const ConstArrayView<NameValue>& opts, const UnownedStringSlice& slice, T defaultValue) { return (T)findValue(opts, slice, ValueInt(defaultValue)); }
+ template <typename T>
+ static T findValue(const ConstArrayView<NamesValue>& opts, const UnownedStringSlice& slice, T defaultValue) { return (T)findValue(opts, slice, ValueInt(defaultValue)); }
+ template <typename T>
+ static T findValue(const ConstArrayView<NamesDescriptionValue>& opts, const UnownedStringSlice& slice, T defaultValue) { return (T)findValue(opts, slice, ValueInt(defaultValue)); }
+
+ /// Given a slice finds the associated value. If no entry is found, defaultValue is returned
+ static ValueInt findValue(const ConstArrayView<NameValue>& opts, const UnownedStringSlice& slice, ValueInt defaultValue = -1);
+ static ValueInt findValue(const ConstArrayView<NamesValue>& opts, const UnownedStringSlice& slice, ValueInt defaultValue = -1);
+ static ValueInt findValue(const ConstArrayView<NamesDescriptionValue>& opts, const UnownedStringSlice& slice, ValueInt defaultValue = -1);
+
+ /// Given a value find the name. If there are multiple names, returns the first name
+ static UnownedStringSlice findName(const ConstArrayView<NameValue>& opts, ValueInt value, const UnownedStringSlice& defaultName = UnownedStringSlice());
+ static UnownedStringSlice findName(const ConstArrayView<NamesValue>& opts, ValueInt value, const UnownedStringSlice& defaultName = UnownedStringSlice());
+ static UnownedStringSlice findName(const ConstArrayView<NamesDescriptionValue>& opts, ValueInt value, const UnownedStringSlice& defaultName = UnownedStringSlice());
+
+ /// Append all the names from opts to out
+ static void appendNames(NameKind kind, const ConstArrayView<NameValue>& opts, List<UnownedStringSlice>& out);
+ static void appendNames(NameKind kind, const ConstArrayView<NamesValue>& opts, List<UnownedStringSlice>& out);
+ static void appendNames(NameKind kind, const ConstArrayView<NamesDescriptionValue>& opts, List<UnownedStringSlice>& out);
+
+ /// Return the list of all valid names
+ static List<UnownedStringSlice> getNames(NameKind kind, const ConstArrayView<NameValue>& opts);
+ static List<UnownedStringSlice> getNames(NameKind kind, const ConstArrayView<NamesValue>& opts);
+ static List<UnownedStringSlice> getNames(NameKind kind, const ConstArrayView<NamesDescriptionValue>& opts);
+
+ /// Get the description
+ static UnownedStringSlice findDescription(const ConstArrayView<NamesDescriptionValue>& opts, ValueInt value, const UnownedStringSlice& defaultDescription = UnownedStringSlice());
+};
+
+} // namespace Slang
+
+#endif