ソースを参照

Working constexpr that fails compile on invalid.

If you use something the constexpr ANSIColor
doesn't understand it won't compile if you
use constexpr before the ANSIColor variable
declaration.
Steve Thielemann 3 年 前
コミット
e77f62b5c0
2 ファイル変更6 行追加3 行削除
  1. 4 1
      ansicolor.h
  2. 2 2
      director.cpp

+ 4 - 1
ansicolor.h

@@ -3,6 +3,7 @@
 
 
 #include <cstdint>
 #include <cstdint>
 #include <string>
 #include <string>
+#include <stdexcept>
 
 
 #define CSI "\x1b["
 #define CSI "\x1b["
 
 
@@ -58,6 +59,7 @@ constexpr long strhash(const char *txt) {
   for (int x = 0; x < 3; ++x) {
   for (int x = 0; x < 3; ++x) {
     if (txt[x] == 0) break;
     if (txt[x] == 0) break;
     char c = txt[x];
     char c = txt[x];
+    // toupper
     if ((c >= 'a') && (c <= 'z')) c &= ~0x20;
     if ((c >= 'a') && (c <= 'z')) c &= ~0x20;
     result = (result << 8) | c;
     result = (result << 8) | c;
   }
   }
@@ -85,7 +87,6 @@ class ANSIColor {
   COLOR fg;
   COLOR fg;
   /** Background color */
   /** Background color */
   COLOR bg;
   COLOR bg;
-
   int attr;
   int attr;
 
 
  public:
  public:
@@ -184,6 +185,8 @@ class ANSIColor {
           else
           else
             fg = COLOR::WHITE;
             fg = COLOR::WHITE;
           break;
           break;
+        default:
+          throw std::range_error("Invalid/unknown color or attribute");
       }
       }
 
 
       // skip to the space character
       // skip to the space character

+ 2 - 2
director.cpp

@@ -967,8 +967,8 @@ void Director::macro_have_input(void) {
       char c = toupper(id->input[0]);
       char c = toupper(id->input[0]);
       macro_item.assign(1, c);
       macro_item.assign(1, c);
       if (galaxy.meta["macros"].contains(macro_item)) {
       if (galaxy.meta["macros"].contains(macro_item)) {
-        ANSIColor key(COLOR::GREEN, ATTR::BOLD);
-        ANSIColor value(COLOR::BLUE, ATTR::BOLD);
+        constexpr ANSIColor key("BOLD GREEN"); // COLOR::GREEN, ATTR::BOLD);
+        constexpr ANSIColor value("BOLD BLUE"); // COLOR::BLUE, ATTR::BOLD);
         std::string output =
         std::string output =
             str(boost::format("%1%%2% : %3%%4%\n\r") % key() % macro_item %
             str(boost::format("%1%%2% : %3%%4%\n\r") % key() % macro_item %
                 value() % galaxy.meta["macros"][macro_item]);
                 value() % galaxy.meta["macros"][macro_item]);