util: Add stub unit tests for the call types in the m5 utility.
[gem5.git] / util / m5 / src / call_type.cc
index 96b886933769a8cd75048b75415ad3846a491073..839a92c4e54e9d535fb2be4b241667619cbe1360 100644 (file)
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <cassert>
+#include <sstream>
+
 #include "args.hh"
 #include "call_type.hh"
-#include "usage.hh"
 
-#if ENABLE_CT_addr
-#include "addr_call_type.hh"
-#endif
-#if ENABLE_CT_inst
-#include "inst_call_type.hh"
-#endif
-#if ENABLE_CT_semi
-#include "semi_call_type.hh"
-#endif
+std::map<std::string, CallType &> &
+CallType::map()
+{
+    static std::map<std::string, CallType &> all;
+    return all;
+}
+
+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 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");
+        }
+    }
 
-#define default_call_type_init() \
-    M5OP_MERGE_TOKENS(DEFAULT_CALL_TYPE, _call_type_init())
+    assert(def);
+    def->init();
+    return def;
+}
 
-DispatchTable *
-init_call_type(Args *args)
+std::string
+CallType::usageSummary()
 {
-#   if ENABLE_CT_inst
-    if (inst_call_type_detect(args))
-        return inst_call_type_init();
-#   endif
-#   if ENABLE_CT_addr
-    int detect = addr_call_type_detect(args);
-    if (detect < 0)
-        usage();
-    if (detect > 0)
-        return addr_call_type_init();
-#   endif
-#   if ENABLE_CT_semi
-    if (semi_call_type_detect(args))
-        return semi_call_type_init();
-#   endif
-    return default_call_type_init();
+    std::string summary = "";
+    for (auto p: map())
+        summary += p.second.formattedUsage();
+    return summary;
+}
+
+std::string
+CallType::formattedUsage() const
+{
+    std::ostringstream os;
+    os << "    ";
+    printBrief(os);
+    if (isDefault())
+        os << " (default)";
+    os << std::endl;
+
+    os << "        ";
+    printDesc(os);
+    os << std::endl;
+    return os.str();
 }