| 
									
										
										
										
											2019-07-27 16:37:48 +02:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/StdLibExtras.h>
 | 
					
						
							| 
									
										
										
										
											2019-07-30 15:16:14 +02:00
										 |  |  | #include <LibCore/CIODevice.h>
 | 
					
						
							| 
									
										
										
										
											2019-07-27 16:37:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-30 15:16:14 +02:00
										 |  |  | class CIODeviceStreamReader { | 
					
						
							| 
									
										
										
										
											2019-07-27 16:37:48 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2019-07-30 15:16:14 +02:00
										 |  |  |     CIODeviceStreamReader(CIODevice& device) | 
					
						
							|  |  |  |         : m_device(device) | 
					
						
							| 
									
										
										
										
											2019-07-27 16:37:48 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool handle_read_failure() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return exchange(m_had_failure, false); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     template<typename T> | 
					
						
							| 
									
										
										
										
											2019-07-30 15:16:14 +02:00
										 |  |  |     CIODeviceStreamReader& operator>>(T& value) | 
					
						
							| 
									
										
										
										
											2019-07-27 16:37:48 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-07-30 15:16:14 +02:00
										 |  |  |         int nread = m_device.read((u8*)&value, sizeof(T)); | 
					
						
							| 
									
										
										
										
											2019-07-27 16:37:48 +02:00
										 |  |  |         ASSERT(nread == sizeof(T)); | 
					
						
							|  |  |  |         if (nread != sizeof(T)) | 
					
						
							|  |  |  |             m_had_failure = true; | 
					
						
							|  |  |  |         return *this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2019-07-30 15:16:14 +02:00
										 |  |  |     CIODevice& m_device; | 
					
						
							| 
									
										
										
										
											2019-07-27 16:37:48 +02:00
										 |  |  |     bool m_had_failure { false }; | 
					
						
							|  |  |  | }; |