mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 08:32:43 +00:00 
			
		
		
		
	LibGfx: Extraction of Streamer from P*MLoader
Problem: - `Streamer` is the same in [PBM,PGM,PPM]Loader class implementations. Solution: - Extract it to its own header file to reduce maintenance burden. - Implement `read` in terms of `read_bytes` to make the class "DRY". - Decorate all functions with `constexpr`.
This commit is contained in:
		
							parent
							
								
									9ca34c3047
								
							
						
					
					
						commit
						58c52b155a
					
				
					 4 changed files with 81 additions and 126 deletions
				
			
		|  | @ -25,6 +25,7 @@ | |||
|  */ | ||||
| 
 | ||||
| #include "PBMLoader.h" | ||||
| #include "Streamer.h" | ||||
| #include <AK/Endian.h> | ||||
| #include <AK/LexicalPath.h> | ||||
| #include <AK/MappedFile.h> | ||||
|  | @ -59,48 +60,6 @@ struct PBMLoadingContext { | |||
|     RefPtr<Gfx::Bitmap> bitmap; | ||||
| }; | ||||
| 
 | ||||
| class Streamer { | ||||
| public: | ||||
|     Streamer(const u8* data, size_t size) | ||||
|         : m_data_ptr(data) | ||||
|         , m_size_remaining(size) | ||||
|     { | ||||
|     } | ||||
| 
 | ||||
|     template<typename T> | ||||
|     bool read(T& value) | ||||
|     { | ||||
|         if (m_size_remaining < sizeof(T)) | ||||
|             return false; | ||||
|         value = *((const NetworkOrdered<T>*)m_data_ptr); | ||||
|         m_data_ptr += sizeof(T); | ||||
|         m_size_remaining -= sizeof(T); | ||||
|         return true; | ||||
|     } | ||||
| 
 | ||||
|     bool read_bytes(u8* buffer, size_t count) | ||||
|     { | ||||
|         if (m_size_remaining < count) | ||||
|             return false; | ||||
|         memcpy(buffer, m_data_ptr, count); | ||||
|         m_data_ptr += count; | ||||
|         m_size_remaining -= count; | ||||
|         return true; | ||||
|     } | ||||
| 
 | ||||
|     bool at_end() const { return !m_size_remaining; } | ||||
| 
 | ||||
|     void step_back() | ||||
|     { | ||||
|         m_data_ptr -= 1; | ||||
|         m_size_remaining += 1; | ||||
|     } | ||||
| 
 | ||||
| private: | ||||
|     const u8* m_data_ptr { nullptr }; | ||||
|     size_t m_size_remaining { 0 }; | ||||
| }; | ||||
| 
 | ||||
| static int read_number(Streamer& streamer) | ||||
| { | ||||
|     u8 byte; | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Lenny Maiorani
						Lenny Maiorani