util: Add stub unit tests for the call types in the m5 utility.
[gem5.git] / util / m5 / src / call_type.cc
index 5deaae1c54438aa9b7e01c14a6d10559235bae70..839a92c4e54e9d535fb2be4b241667619cbe1360 100644 (file)
 #include <cassert>
 #include <sstream>
 
+#include "args.hh"
 #include "call_type.hh"
 
-std::vector<CallType *> &
-CallType::allTypes()
+std::map<std::string, CallType &> &
+CallType::map()
 {
-    static std::vector<CallType *> all;
+    static std::map<std::string, CallType &> all;
     return all;
 }
 
-CallType &
+CallType::CheckArgsResult
+CallType::checkArgs(Args &args)
+{
+    if (args.size() && args[0] == "--" + name) {
+        args.pop();
+        return CheckArgsResult::Match;
+    }
+    return CheckArgsResult::NoMatch;
+}
+
+CallType *
 CallType::detect(Args &args)
 {
     CallType *def = nullptr;
 
-    for (auto *ct: allTypes()) {
-        if (ct->checkArgs(args)) {
-            ct->init();
-            return *ct;
+    for (auto p: map()) {
+        auto &ct = p.second;
+        if (ct.isDefault())
+            def = &ct;
+        auto result = ct.checkArgs(args);
+        switch (result) {
+          case CheckArgsResult::Match:
+            ct.init();
+            return &ct;
+          case CheckArgsResult::NoMatch:
+            continue;
+          case CheckArgsResult::Usage:
+            return nullptr;
+          default:
+            assert(!"Bad checkArgs result");
         }
-        if (ct->isDefault())
-            def = ct;
     }
 
     assert(def);
     def->init();
-    return *def;
+    return def;
 }
 
 std::string
 CallType::usageSummary()
 {
     std::string summary = "";
-    for (auto *ct: allTypes())
-        summary += ct->formattedUsage();
+    for (auto p: map())
+        summary += p.second.formattedUsage();
     return summary;
 }