onilink_

Chunks

Jun 29th, 2015
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1.  
  2. typedef void(*FileChunkCaseFunc)(od::StreamData&);
  3. template<unsigned Magic, FileChunkCaseFunc Func>
  4. struct FileChunkCase
  5. {
  6.     static constexpr std::pair<unsigned, FileChunkCaseFunc> pair = std::make_pair(Magic, Func);
  7. };
  8.  
  9. template<class...Args>
  10. class FileChunkReader
  11. {
  12. public:
  13.     void operator()(od::StreamData & s);
  14. };
  15.  
  16. template<class...Args>
  17. void FileChunkReader<Args...>::operator()(od::StreamData & s)
  18. {
  19.     unsigned chunk, len;
  20.     do
  21.     {
  22.         s >> chunk >> len;
  23.        
  24.         for(auto& pair: {Args::pair...})
  25.         {
  26.             if(pair.first == chunk) {
  27.                 pair.second(s);
  28.                 break;
  29.             }
  30.         }
  31.        
  32.         s.seek(len-4, od::StreamData::Cursor);
  33.     }
  34.     while(!s.eof());
  35. }
  36.  
  37. void readBiom(od::StreamData & s)
  38. {
  39.     printf("Biom\n");
  40. }
  41. void readBloc(od::StreamData & s)
  42. {
  43.     printf("Bloc\n");
  44. }
  45.  
  46. void SerializeWorld::load(const std::string & fname)
  47. {
  48.     od::StreamData s;
  49.     s.load(fname);
  50.    
  51.     FileChunkReader<
  52.         FileChunkCase<stringMagic("BIOM"), readBiom>,
  53.         FileChunkCase<stringMagic("BLOC"), readBloc>
  54.     >()(s);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment