Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

366 lines
8.4 KiB

  1. /*--------------------------------------------------------------------------*
  2. *
  3. * Microsoft Windows
  4. * Copyright (C) Microsoft Corporation, 1992 - 1998.
  5. *
  6. * File: stgio.inl
  7. *
  8. * Contents: Inlines file structured storage I/O utilities
  9. *
  10. * History: 25-Jun-98 jeffro Created
  11. *
  12. *--------------------------------------------------------------------------*/
  13. #ifndef STGIO_INL
  14. #define STGIO_INL
  15. #pragma once
  16. #include <functional>
  17. #include <algorithm>
  18. /*+-------------------------------------------------------------------------*
  19. * stream_insert
  20. *
  21. * Functor to insert an object in an IStream.
  22. *--------------------------------------------------------------------------*/
  23. template<class T>
  24. struct stream_insert :
  25. public std::binary_function<IStream*, T, IStream&>
  26. {
  27. IStream& operator() (IStream* pstm, const T& t) const
  28. { return (*pstm << t); }
  29. };
  30. /*+-------------------------------------------------------------------------*
  31. * stream_extract
  32. *
  33. * Functor to extract an object from an IStream.
  34. *--------------------------------------------------------------------------*/
  35. template<class T>
  36. struct stream_extract :
  37. public std::binary_function<IStream*, T, IStream&>
  38. {
  39. IStream& operator() (IStream* pstm, T& t) const
  40. { return (*pstm >> t); }
  41. };
  42. /*+-------------------------------------------------------------------------*
  43. * insert_each
  44. *
  45. * Inserts each item in a collection in an IStream.
  46. *--------------------------------------------------------------------------*/
  47. template<class Collection>
  48. void insert_each (IStream* pstm, const Collection& c)
  49. {
  50. std::for_each (c.begin(), c.end(),
  51. std::bind1st (stream_insert<Collection::value_type>(), pstm));
  52. }
  53. /*+-------------------------------------------------------------------------*
  54. * insert_collection
  55. *
  56. * Inserts an entire collection into an IStream.
  57. *--------------------------------------------------------------------------*/
  58. template<class Collection>
  59. void insert_collection (IStream* pstm, const Collection& c)
  60. {
  61. /*
  62. * write the size
  63. */
  64. *pstm << (DWORD) c.size();
  65. /*
  66. * write the elements
  67. */
  68. insert_each (pstm, c);
  69. }
  70. /*+-------------------------------------------------------------------------*
  71. * extract_collection
  72. *
  73. * Extracts an entire collection (written by insert_collection) from an IStream.
  74. *--------------------------------------------------------------------------*/
  75. template<class Collection>
  76. void extract_collection (IStream* pstm, Collection& c)
  77. {
  78. /*
  79. * clear out the current container
  80. */
  81. c.clear();
  82. ASSERT (c.empty());
  83. /*
  84. * read the number of items
  85. */
  86. DWORD cItems;
  87. *pstm >> cItems;
  88. /*
  89. * read each item
  90. */
  91. while (cItems-- > 0)
  92. {
  93. /*
  94. * read the item
  95. */
  96. Collection::value_type t;
  97. *pstm >> t;
  98. /*
  99. * put it in the container
  100. */
  101. c.push_back (t);
  102. }
  103. }
  104. /*+-------------------------------------------------------------------------*
  105. * extract_vector
  106. *
  107. * Extracts an entire vector (written by insert_collection) from an IStream.
  108. *--------------------------------------------------------------------------*/
  109. template<class T>
  110. void extract_vector (IStream* pstm, std::vector<T>& v)
  111. {
  112. /*
  113. * clear out the current container
  114. */
  115. v.clear();
  116. /*
  117. * read the number of items
  118. */
  119. DWORD cItems;
  120. *pstm >> cItems;
  121. /*
  122. * pre-allocate the appropriate number of items (specialization for vector)
  123. */
  124. v.reserve (cItems);
  125. ASSERT (v.empty());
  126. ASSERT (v.capacity() >= cItems);
  127. /*
  128. * read each item
  129. */
  130. while (cItems-- > 0)
  131. {
  132. /*
  133. * read the item
  134. */
  135. T t;
  136. *pstm >> t;
  137. /*
  138. * put it in the container
  139. */
  140. v.push_back (t);
  141. }
  142. }
  143. // specialized scalar vector prototypes (stgio.cpp)
  144. #define DeclareScalarVectorStreamFunctions(scalar_type) \
  145. void extract_vector (IStream* pstm, std::vector<scalar_type>& v); \
  146. void insert_collection (IStream* pstm, const std::vector<scalar_type>& v);
  147. DeclareScalarVectorStreamFunctions (bool);
  148. DeclareScalarVectorStreamFunctions ( char);
  149. DeclareScalarVectorStreamFunctions (unsigned char);
  150. DeclareScalarVectorStreamFunctions ( short);
  151. DeclareScalarVectorStreamFunctions (unsigned short);
  152. DeclareScalarVectorStreamFunctions ( int);
  153. DeclareScalarVectorStreamFunctions (unsigned int);
  154. DeclareScalarVectorStreamFunctions ( long);
  155. DeclareScalarVectorStreamFunctions (unsigned long);
  156. DeclareScalarVectorStreamFunctions ( __int64);
  157. DeclareScalarVectorStreamFunctions (unsigned __int64);
  158. DeclareScalarVectorStreamFunctions (float);
  159. DeclareScalarVectorStreamFunctions (double);
  160. DeclareScalarVectorStreamFunctions (long double);
  161. /*+-------------------------------------------------------------------------*
  162. * extract_set_or_map
  163. *
  164. * Extracts an entire set or map (written by insert_collection) from an IStream.
  165. *--------------------------------------------------------------------------*/
  166. template<class Collection>
  167. void extract_set_or_map (IStream* pstm, Collection& c)
  168. {
  169. /*
  170. * clear out the current container
  171. */
  172. c.clear();
  173. ASSERT (c.empty());
  174. /*
  175. * read the number of items
  176. */
  177. DWORD cItems;
  178. *pstm >> cItems;
  179. /*
  180. * read each item
  181. */
  182. while (cItems-- > 0)
  183. {
  184. /*
  185. * read the item
  186. */
  187. Collection::value_type t;
  188. *pstm >> t;
  189. /*
  190. * put it in the container
  191. */
  192. c.insert (t);
  193. }
  194. }
  195. /*+-------------------------------------------------------------------------*
  196. * operator<<, operator>>
  197. *
  198. * Stream insertion and extraction operators for various types
  199. *--------------------------------------------------------------------------*/
  200. // std::pair<>
  201. template<class T1, class T2>
  202. IStream& operator>> (IStream& stm, std::pair<T1, T2>& p)
  203. {
  204. return (stm >> p.first >> p.second);
  205. }
  206. template<class T1, class T2>
  207. IStream& operator<< (IStream& stm, const std::pair<T1, T2>& p)
  208. {
  209. return (stm << p.first << p.second);
  210. }
  211. // std::list<>
  212. template<class T, class Al>
  213. IStream& operator>> (IStream& stm, std::list<T, Al>& l)
  214. {
  215. extract_collection (&stm, l);
  216. return (stm);
  217. }
  218. template<class T, class Al>
  219. IStream& operator<< (IStream& stm, const std::list<T, Al>& l)
  220. {
  221. insert_collection (&stm, l);
  222. return (stm);
  223. }
  224. // std::deque<>
  225. template<class T, class Al>
  226. IStream& operator>> (IStream& stm, std::deque<T, Al>& l)
  227. {
  228. extract_collection (&stm, l);
  229. return (stm);
  230. }
  231. template<class T, class Al>
  232. IStream& operator<< (IStream& stm, const std::deque<T, Al>& l)
  233. {
  234. insert_collection (&stm, l);
  235. return (stm);
  236. }
  237. // std::vector<>
  238. template<class T, class Al>
  239. IStream& operator>> (IStream& stm, std::vector<T, Al>& v)
  240. {
  241. extract_vector (&stm, v);
  242. return (stm);
  243. }
  244. template<class T, class Al>
  245. IStream& operator<< (IStream& stm, const std::vector<T, Al>& v)
  246. {
  247. insert_collection (&stm, v);
  248. return (stm);
  249. }
  250. // std::set<>
  251. template<class T, class Pr, class Al>
  252. IStream& operator>> (IStream& stm, std::set<T, Pr, Al>& s)
  253. {
  254. extract_set_or_map (&stm, s);
  255. return (stm);
  256. }
  257. template<class T, class Pr, class Al>
  258. IStream& operator<< (IStream& stm, const std::set<T, Pr, Al>& s)
  259. {
  260. insert_collection (&stm, s);
  261. return (stm);
  262. }
  263. // std::multiset<>
  264. template<class T, class Pr, class Al>
  265. IStream& operator>> (IStream& stm, std::multiset<T, Pr, Al>& s)
  266. {
  267. extract_set_or_map (&stm, s);
  268. return (stm);
  269. }
  270. template<class T, class Pr, class Al>
  271. IStream& operator<< (IStream& stm, const std::multiset<T, Pr, Al>& s)
  272. {
  273. insert_collection (&stm, s);
  274. return (stm);
  275. }
  276. // std::map<>
  277. template<class K, class T, class Pr, class Al>
  278. IStream& operator>> (IStream& stm, std::map<K, T, Pr, Al>& m)
  279. {
  280. extract_set_or_map (&stm, m);
  281. return (stm);
  282. }
  283. template<class K, class T, class Pr, class Al>
  284. IStream& operator<< (IStream& stm, const std::map<K, T, Pr, Al>& m)
  285. {
  286. insert_collection (&stm, m);
  287. return (stm);
  288. }
  289. // std::multimap<>
  290. template<class K, class T, class Pr, class Al>
  291. IStream& operator>> (IStream& stm, std::multimap<K, T, Pr, Al>& m)
  292. {
  293. extract_set_or_map (&stm, m);
  294. return (stm);
  295. }
  296. template<class K, class T, class Pr, class Al>
  297. IStream& operator<< (IStream& stm, const std::multimap<K, T, Pr, Al>& m)
  298. {
  299. insert_collection (&stm, m);
  300. return (stm);
  301. }
  302. #endif /* STGIO_INL */