/*--------------------------------------------------------------------------* * * Microsoft Windows * Copyright (C) Microsoft Corporation, 1992 - 1998. * * File: stgio.inl * * Contents: Inlines file structured storage I/O utilities * * History: 25-Jun-98 jeffro Created * *--------------------------------------------------------------------------*/ #ifndef STGIO_INL #define STGIO_INL #pragma once #include #include /*+-------------------------------------------------------------------------* * stream_insert * * Functor to insert an object in an IStream. *--------------------------------------------------------------------------*/ template struct stream_insert : public std::binary_function { IStream& operator() (IStream* pstm, const T& t) const { return (*pstm << t); } }; /*+-------------------------------------------------------------------------* * stream_extract * * Functor to extract an object from an IStream. *--------------------------------------------------------------------------*/ template struct stream_extract : public std::binary_function { IStream& operator() (IStream* pstm, T& t) const { return (*pstm >> t); } }; /*+-------------------------------------------------------------------------* * insert_each * * Inserts each item in a collection in an IStream. *--------------------------------------------------------------------------*/ template void insert_each (IStream* pstm, const Collection& c) { std::for_each (c.begin(), c.end(), std::bind1st (stream_insert(), pstm)); } /*+-------------------------------------------------------------------------* * insert_collection * * Inserts an entire collection into an IStream. *--------------------------------------------------------------------------*/ template void insert_collection (IStream* pstm, const Collection& c) { /* * write the size */ *pstm << (DWORD) c.size(); /* * write the elements */ insert_each (pstm, c); } /*+-------------------------------------------------------------------------* * extract_collection * * Extracts an entire collection (written by insert_collection) from an IStream. *--------------------------------------------------------------------------*/ template void extract_collection (IStream* pstm, Collection& c) { /* * clear out the current container */ c.clear(); ASSERT (c.empty()); /* * read the number of items */ DWORD cItems; *pstm >> cItems; /* * read each item */ while (cItems-- > 0) { /* * read the item */ Collection::value_type t; *pstm >> t; /* * put it in the container */ c.push_back (t); } } /*+-------------------------------------------------------------------------* * extract_vector * * Extracts an entire vector (written by insert_collection) from an IStream. *--------------------------------------------------------------------------*/ template void extract_vector (IStream* pstm, std::vector& v) { /* * clear out the current container */ v.clear(); /* * read the number of items */ DWORD cItems; *pstm >> cItems; /* * pre-allocate the appropriate number of items (specialization for vector) */ v.reserve (cItems); ASSERT (v.empty()); ASSERT (v.capacity() >= cItems); /* * read each item */ while (cItems-- > 0) { /* * read the item */ T t; *pstm >> t; /* * put it in the container */ v.push_back (t); } } // specialized scalar vector prototypes (stgio.cpp) #define DeclareScalarVectorStreamFunctions(scalar_type) \ void extract_vector (IStream* pstm, std::vector& v); \ void insert_collection (IStream* pstm, const std::vector& v); DeclareScalarVectorStreamFunctions (bool); DeclareScalarVectorStreamFunctions ( char); DeclareScalarVectorStreamFunctions (unsigned char); DeclareScalarVectorStreamFunctions ( short); DeclareScalarVectorStreamFunctions (unsigned short); DeclareScalarVectorStreamFunctions ( int); DeclareScalarVectorStreamFunctions (unsigned int); DeclareScalarVectorStreamFunctions ( long); DeclareScalarVectorStreamFunctions (unsigned long); DeclareScalarVectorStreamFunctions ( __int64); DeclareScalarVectorStreamFunctions (unsigned __int64); DeclareScalarVectorStreamFunctions (float); DeclareScalarVectorStreamFunctions (double); DeclareScalarVectorStreamFunctions (long double); /*+-------------------------------------------------------------------------* * extract_set_or_map * * Extracts an entire set or map (written by insert_collection) from an IStream. *--------------------------------------------------------------------------*/ template void extract_set_or_map (IStream* pstm, Collection& c) { /* * clear out the current container */ c.clear(); ASSERT (c.empty()); /* * read the number of items */ DWORD cItems; *pstm >> cItems; /* * read each item */ while (cItems-- > 0) { /* * read the item */ Collection::value_type t; *pstm >> t; /* * put it in the container */ c.insert (t); } } /*+-------------------------------------------------------------------------* * operator<<, operator>> * * Stream insertion and extraction operators for various types *--------------------------------------------------------------------------*/ // std::pair<> template IStream& operator>> (IStream& stm, std::pair& p) { return (stm >> p.first >> p.second); } template IStream& operator<< (IStream& stm, const std::pair& p) { return (stm << p.first << p.second); } // std::list<> template IStream& operator>> (IStream& stm, std::list& l) { extract_collection (&stm, l); return (stm); } template IStream& operator<< (IStream& stm, const std::list& l) { insert_collection (&stm, l); return (stm); } // std::deque<> template IStream& operator>> (IStream& stm, std::deque& l) { extract_collection (&stm, l); return (stm); } template IStream& operator<< (IStream& stm, const std::deque& l) { insert_collection (&stm, l); return (stm); } // std::vector<> template IStream& operator>> (IStream& stm, std::vector& v) { extract_vector (&stm, v); return (stm); } template IStream& operator<< (IStream& stm, const std::vector& v) { insert_collection (&stm, v); return (stm); } // std::set<> template IStream& operator>> (IStream& stm, std::set& s) { extract_set_or_map (&stm, s); return (stm); } template IStream& operator<< (IStream& stm, const std::set& s) { insert_collection (&stm, s); return (stm); } // std::multiset<> template IStream& operator>> (IStream& stm, std::multiset& s) { extract_set_or_map (&stm, s); return (stm); } template IStream& operator<< (IStream& stm, const std::multiset& s) { insert_collection (&stm, s); return (stm); } // std::map<> template IStream& operator>> (IStream& stm, std::map& m) { extract_set_or_map (&stm, m); return (stm); } template IStream& operator<< (IStream& stm, const std::map& m) { insert_collection (&stm, m); return (stm); } // std::multimap<> template IStream& operator>> (IStream& stm, std::multimap& m) { extract_set_or_map (&stm, m); return (stm); } template IStream& operator<< (IStream& stm, const std::multimap& m) { insert_collection (&stm, m); return (stm); } #endif /* STGIO_INL */