Преглед на файлове

Cleaning up the command_builder a little

Sam Jaffe преди 5 години
родител
ревизия
ee13a1c72e
променени са 1 файла, в които са добавени 24 реда и са изтрити 20 реда
  1. 24 20
      command_builder.h

+ 24 - 20
command_builder.h

@@ -15,15 +15,19 @@ namespace build {
 
   public:
     command_builder(std::string const & cmd, int indent = 0);
+
     template <typename T> command_builder && operator<<(T const & value) &&;
-    // TODO(samjaffe): Use the type system for things here...
-    command_builder & operator<<(version const & vers) &;
-    command_builder & operator<<(source_file const & file) &;
-    command_builder & operator<<(output_file const & file) &;
     template <typename T> command_builder & operator<<(T const & value) &;
     template <typename T>
     command_builder & operator<<(std::vector<T> const & value) &;
+
     int execute() const;
+
+  private:
+    template <typename T> void write(T const & value) { buffer_ << value; }
+    void write(version const & vers);
+    void write(source_file const & file);
+    void write(output_file const & file);
   };
 
   command_builder::command_builder(std::string const & cmd, int indent)
@@ -38,11 +42,21 @@ namespace build {
 
   template <typename T>
   command_builder & command_builder::operator<<(T const & value) & {
-    buffer_ << ' ' << value;
+    buffer_ << ' ';
+    write(value);
+    return *this;
+  }
+
+  template <typename T>
+  command_builder &
+  command_builder::operator<<(std::vector<T> const & value) & {
+    for (T const & v : value) {
+      (*this) << v;
+    }
     return *this;
   }
 
-  command_builder & command_builder::operator<<(version const & vers) & {
+  void command_builder::write(version const & vers) {
     switch (vers) {
     case version::cxx11:
       buffer_ << ' ' << "-std=c++11";
@@ -56,26 +70,14 @@ namespace build {
     case none:
       break;
     }
-    return *this;
   }
 
-  command_builder & command_builder::operator<<(source_file const & file) & {
+  void command_builder::write(source_file const & file) {
     buffer_ << " -c " << file.get();
-    return *this;
   }
 
-  command_builder & command_builder::operator<<(output_file const & file) & {
+  void command_builder::write(output_file const & file) {
     buffer_ << " -o " << file.get();
-    return *this;
-  }
-
-  template <typename T>
-  command_builder &
-  command_builder::operator<<(std::vector<T> const & value) & {
-    for (T const & v : value) {
-      (*this) << v;
-    }
-    return *this;
   }
 
   std::ostream & operator<<(std::ostream & os, indent_t const & value) {
@@ -87,6 +89,8 @@ namespace build {
 
   int command_builder::execute() const {
     std::string cmdstr = buffer_.str();
+    // TODO(samjaffe): Not sure this belongs here, so much as command_builder
+    // should take a formatter object (including things like color config)
     std::cout << indent_ << cmdstr << std::endl;
     return std::system(cmdstr.c_str());
   }