Browse Source

升级print打印
升级buffer,支持模板append

xx 2 years ago
parent
commit
708dbcd201
4 changed files with 14 additions and 16 deletions
  1. 4 3
      include/ybase/buffer.h
  2. 1 0
      include/yutil/print.h
  3. 1 9
      src/ybase/src/buffer.cpp
  4. 8 4
      src/yutil/src/print.cpp

+ 4 - 3
include/ybase/buffer.h

@@ -44,9 +44,10 @@ public:
     void append(const char* data, size_t length);
     void append(const ylib::buffer& data);
     void append(std::initializer_list<uchar> char_list);
-
-    void append_c(char value);
-    void append_uc(unsigned char value);
+    template<typename T>
+    void append(const T& value) {
+        append((const char*)&value, sizeof(T));
+    }
 
     void clear();
     void resize(size_t length);

+ 1 - 0
include/yutil/print.h

@@ -4,6 +4,7 @@
 namespace ylib
 {
 	void println(const std::string& text,ylib::ConsoleTextColor color = ylib::ConsoleTextColor::DEFAULT);
+	void print(const std::string& text, ylib::ConsoleTextColor color = ylib::ConsoleTextColor::DEFAULT);
 }
 
 

+ 1 - 9
src/ybase/src/buffer.cpp

@@ -74,15 +74,7 @@ void ylib::buffer::append(std::initializer_list<uchar> char_list)
     this->append((char*)new_buf, idx);
     free(new_buf);
 }
-void ylib::buffer::append_c(char value)
-{
-    append((const char*)&value,1);
-}
 
-void ylib::buffer::append_uc(unsigned char value)
-{
-    append((const char*)&value,1);
-}
 
 void ylib::buffer::clear()
 {
@@ -232,7 +224,7 @@ unsigned char &ylib::buffer::operator[](size_t index) const
     if (index + 1 > m_data.m_use_length)
     {
         std::string msg = "ylib::buffer::operator[] index="+std::to_string(index) + " but data length is "+std::to_string(m_data.m_use_length);
-        throw std::exception(msg.c_str());
+        throw ylib::exception(msg.c_str());
     }
     return m_data.data()[index];
 }

+ 8 - 4
src/yutil/src/print.cpp

@@ -10,15 +10,19 @@ const std::string LINUX_RED = "\033[31m";
 const std::string LINUX_GREEN = "\033[32m";
 const std::string LINUX_YELLOW = "\033[33m";
 const std::string LINUX_BLUE = "\033[34m";
-//#endif
+//#endif 
 void ylib::println(const std::string& text, ylib::ConsoleTextColor color)
+{
+	print(text + "\r\n", color);
+}
+void ylib::print(const std::string& text, ylib::ConsoleTextColor color)
 {
 #ifdef _WIN32
-	if(color == ylib::YELLOW)
+	if (color == ylib::YELLOW)
 		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
 	else
 		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
-	printf("%s\r\n", text.c_str());
+	printf("%s", text.c_str());
 	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), ylib::ConsoleTextColor::DEFAULT);
 #else
 	switch (color)
@@ -40,7 +44,7 @@ void ylib::println(const std::string& text, ylib::ConsoleTextColor color)
 		break;
 	default:
 		break;
-	}
+}
 	std::cout << LINUX_RESET << std::endl;
 
 #endif