ios_base_state_ptr.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // boost/chrono/utility/ios_base_pword_ptr.hpp ------------------------------------------------------------//
  2. // Copyright 2011 Vicente J. Botet Escriba
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/chrono for documentation.
  6. #ifndef BOOST_CHRONO_UTILITY_IOS_BASE_STATE_PTR_HPP
  7. #define BOOST_CHRONO_UTILITY_IOS_BASE_STATE_PTR_HPP
  8. #include <ios>
  9. #include <boost/assert.hpp>
  10. /**
  11. *
  12. */
  13. namespace boost
  14. {
  15. namespace chrono
  16. {
  17. namespace detail
  18. {
  19. /**
  20. * xalloc key holder.
  21. */
  22. template <typename T>
  23. struct xalloc_key_holder
  24. {
  25. static int value; //< the xalloc value associated to T.
  26. static bool initialized; //< whether the value has been initialized or not.
  27. };
  28. template <typename T>
  29. int xalloc_key_holder<T>::value = 0;
  30. template <typename T>
  31. bool xalloc_key_holder<T>::initialized = false;
  32. }
  33. /**
  34. * xalloc key initialiazer.
  35. *
  36. * Declare a static variable of this type to ensure that the xalloc_key_holder<T> is initialized correctly.
  37. */
  38. template <typename T>
  39. struct xalloc_key_initializer
  40. {
  41. xalloc_key_initializer()
  42. {
  43. if (!detail::xalloc_key_holder<T>::initialized)
  44. {
  45. detail::xalloc_key_holder<T>::value = std::ios_base::xalloc();
  46. detail::xalloc_key_holder<T>::initialized = true;
  47. }
  48. }
  49. };
  50. /**
  51. * @c ios_state_ptr is a smart pointer to a ios_base specific state.
  52. */
  53. template <typename Final, typename T>
  54. class ios_state_ptr
  55. {
  56. ios_state_ptr& operator=(ios_state_ptr const& rhs) ;
  57. public:
  58. /**
  59. * The pointee type
  60. */
  61. typedef T element_type;
  62. /**
  63. * Explicit constructor.
  64. * @param ios the ios
  65. * @Effects Constructs a @c ios_state_ptr by storing the associated @c ios.
  66. */
  67. explicit ios_state_ptr(std::ios_base& ios) :
  68. ios_(ios)
  69. {
  70. }
  71. /**
  72. * Nothing to do as xalloc index can not be removed.
  73. */
  74. ~ios_state_ptr()
  75. {
  76. }
  77. /**
  78. * @Effects Allocates the index if not already done.
  79. * Registers the callback responsible of maintaining the state pointer coherency, if not already done.
  80. * Retrieves the associated ios pointer
  81. * @return the retrieved pointer statically casted to const.
  82. */
  83. T const* get() const BOOST_NOEXCEPT
  84. {
  85. register_once(index(), ios_);
  86. void* &pw = ios_.pword(index());
  87. if (pw == 0)
  88. {
  89. return 0;
  90. }
  91. return static_cast<const T*> (pw);
  92. }
  93. /**
  94. * @Effects Allocates the index if not already done.
  95. * Registers the callback responsible of maintaining the state pointer coherency, if not already done.
  96. * Retrieves the associated ios pointer
  97. * @return the retrieved pointer.
  98. */
  99. T * get() BOOST_NOEXCEPT
  100. {
  101. register_once(index(), ios_);
  102. void* &pw = ios_.pword(index());
  103. if (pw == BOOST_NULLPTR)
  104. {
  105. return BOOST_NULLPTR;
  106. }
  107. return static_cast<T*> (pw);
  108. }
  109. /**
  110. * @Effects as if @c return get();
  111. * @return the retrieved pointer.
  112. */
  113. T * operator->()BOOST_NOEXCEPT
  114. {
  115. return get();
  116. }
  117. /**
  118. * @Effects as if @c return get();
  119. * @return the retrieved pointer.
  120. */
  121. T const * operator->() const BOOST_NOEXCEPT
  122. {
  123. return get();
  124. }
  125. /**
  126. * @Effects as if @c return *get();
  127. * @return a reference to the retrieved state.
  128. * @Remark The behavior is undefined if @c get()==0.
  129. */
  130. T & operator*() BOOST_NOEXCEPT
  131. {
  132. return *get();
  133. }
  134. /**
  135. * @Effects as if @c return *get();
  136. * @return a reference to the retrieved state.
  137. * @Remark The behavior is undefined if @c get()==0.
  138. */
  139. T const & operator *() const BOOST_NOEXCEPT
  140. {
  141. return *get();
  142. }
  143. /**
  144. * @Effects reset the current pointer after storing in a temporary variable the pointer to the current state.
  145. * @return the stored state pointer.
  146. */
  147. T * release() BOOST_NOEXCEPT
  148. {
  149. void*& pw = ios_.pword(index());
  150. T* ptr = static_cast<T*> (pw);
  151. pw = 0;
  152. return ptr;
  153. }
  154. /**
  155. *
  156. * @param new_ptr the new pointer.
  157. * @Effects deletes the current state and replace it with the new one.
  158. */
  159. void reset(T* new_ptr = 0)BOOST_NOEXCEPT
  160. {
  161. register_once(index(), ios_);
  162. void*& pw = ios_.pword(index());
  163. delete static_cast<T*> (pw);
  164. pw = new_ptr;
  165. }
  166. #if defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
  167. typedef T* (ios_state_ptr::*bool_type)();
  168. operator bool_type() const BOOST_NOEXCEPT
  169. {
  170. return (get()!=0)?&ios_state_ptr::release:0;
  171. }
  172. bool operator!() const BOOST_NOEXCEPT
  173. {
  174. return (get()==0)?&ios_state_ptr::release:0;
  175. }
  176. #else
  177. /**
  178. * Explicit conversion to bool.
  179. */
  180. explicit operator bool() const BOOST_NOEXCEPT
  181. {
  182. return get()!=0;
  183. }
  184. #endif
  185. std::ios_base& getios()BOOST_NOEXCEPT
  186. {
  187. return ios_;
  188. }
  189. std::ios_base& getios() const BOOST_NOEXCEPT
  190. {
  191. return ios_;
  192. }
  193. /**
  194. * Implicit conversion to the ios_base
  195. */
  196. operator std::ios_base&() BOOST_NOEXCEPT
  197. {
  198. return ios_;
  199. }
  200. /**
  201. * Implicit conversion to the ios_base const
  202. */
  203. operator std::ios_base&() const BOOST_NOEXCEPT
  204. {
  205. return ios_;
  206. }
  207. private:
  208. static inline bool is_registerd(std::ios_base& ios)
  209. {
  210. long iw = ios.iword(index());
  211. return (iw == 1);
  212. }
  213. static inline void set_registered(std::ios_base& ios)
  214. {
  215. long& iw = ios.iword(index());
  216. iw = 1;
  217. }
  218. static inline void callback(std::ios_base::event evt, std::ios_base& ios, int index)
  219. {
  220. switch (evt)
  221. {
  222. case std::ios_base::erase_event:
  223. {
  224. void*& pw = ios.pword(index);
  225. if (pw != BOOST_NULLPTR)
  226. {
  227. T* ptr = static_cast<T*> (pw);
  228. delete ptr;
  229. pw = BOOST_NULLPTR;
  230. }
  231. break;
  232. }
  233. case std::ios_base::copyfmt_event:
  234. {
  235. void*& pw = ios.pword(index);
  236. if (pw != BOOST_NULLPTR)
  237. {
  238. pw = new T(*static_cast<T*> (pw));
  239. }
  240. break;
  241. }
  242. default:
  243. break;
  244. }
  245. }
  246. static inline int index()
  247. {
  248. return detail::xalloc_key_holder<Final>::value;
  249. }
  250. static inline void register_once(int indx, std::ios_base& ios)
  251. {
  252. // needs a mask registered
  253. if (!is_registerd(ios))
  254. {
  255. set_registered(ios);
  256. ios.register_callback(callback, indx);
  257. }
  258. }
  259. protected:
  260. std::ios_base& ios_;
  261. //static detail::xalloc_key_initializer<Final> xalloc_key_initializer_;
  262. };
  263. //template <typename Final, typename T>
  264. //detail::xalloc_key_initializer<Final> ios_state_ptr<Final,T>::xalloc_key_initializer_;
  265. /**
  266. * @c ios_state_not_null_ptr is a non null variant of @c ios_state_ptr.
  267. * @tparm T
  268. * @Requires @c T must be @c DefaultConstructible and @c HeapAllocatable
  269. */
  270. template <typename Final, typename T>
  271. class ios_state_not_null_ptr: public ios_state_ptr<Final, T>
  272. {
  273. typedef ios_state_ptr<Final, T> base_type;
  274. public:
  275. explicit ios_state_not_null_ptr(std::ios_base& ios) :
  276. base_type(ios)
  277. {
  278. if (this->get() == BOOST_NULLPTR)
  279. {
  280. this->base_type::reset(new T());
  281. }
  282. }
  283. ~ios_state_not_null_ptr()
  284. {
  285. }
  286. void reset(T* new_value) BOOST_NOEXCEPT
  287. {
  288. BOOST_ASSERT(new_value!=BOOST_NULLPTR);
  289. this->base_type::reset(new_value);
  290. }
  291. };
  292. /**
  293. * This class is useful to associate some flags to an std::ios_base.
  294. */
  295. template <typename Final>
  296. class ios_flags
  297. {
  298. public:
  299. /**
  300. *
  301. * @param ios the associated std::ios_base.
  302. * @Postcondition <c>flags()==0</c>
  303. */
  304. explicit ios_flags(std::ios_base& ios) :
  305. ios_(ios)
  306. {
  307. }
  308. ~ios_flags()
  309. {
  310. }
  311. /**
  312. * @Returns The format control information.
  313. */
  314. long flags() const BOOST_NOEXCEPT
  315. {
  316. return value();
  317. }
  318. /**
  319. * @param v the new bit mask.
  320. * @Postcondition <c>v == flags()</c>.
  321. * @Returns The previous value of @c flags().
  322. */
  323. long flags(long v)BOOST_NOEXCEPT
  324. {
  325. long tmp = flags();
  326. ref() = v;
  327. return tmp;
  328. }
  329. /**
  330. * @param v the new value
  331. * @Effects: Sets @c v in @c flags().
  332. * @Returns: The previous value of @c flags().
  333. */
  334. long setf(long v)
  335. {
  336. long tmp = value();
  337. ref() |= v;
  338. return tmp;
  339. }
  340. /**
  341. * @param mask the bit mask to clear.
  342. * @Effects: Clears @c mask in @c flags().
  343. */
  344. void unsetf(long mask)
  345. {
  346. ref() &= ~mask;
  347. }
  348. /**
  349. *
  350. * @param v
  351. * @param mask
  352. * @Effects: Clears @c mask in @c flags(), sets <c>v & mask</c> in @c flags().
  353. * @Returns: The previous value of flags().
  354. */
  355. long setf(long v, long mask)
  356. {
  357. long tmp = value();
  358. unsetf(mask);
  359. ref() |= v & mask;
  360. return tmp;
  361. }
  362. /**
  363. * implicit conversion to the @c ios_base
  364. */
  365. operator std::ios_base&()BOOST_NOEXCEPT
  366. {
  367. return ios_;
  368. }
  369. /**
  370. * implicit conversion to the @c ios_base const
  371. */
  372. operator std::ios_base const&() const BOOST_NOEXCEPT
  373. {
  374. return ios_;
  375. }
  376. private:
  377. long value() const BOOST_NOEXCEPT
  378. {
  379. return ios_.iword(index());
  380. }
  381. long& ref()BOOST_NOEXCEPT
  382. {
  383. return ios_.iword(index());
  384. }
  385. static inline int index()
  386. {
  387. return detail::xalloc_key_holder<Final>::value;
  388. }
  389. ios_flags& operator=(ios_flags const& rhs) ;
  390. std::ios_base& ios_;
  391. //static detail::xalloc_key_initializer<Final> xalloc_key_initializer_;
  392. };
  393. //template <typename Final>
  394. //detail::xalloc_key_initializer<Final> ios_flags<Final>::xalloc_key_initializer_;
  395. } // namespace chrono
  396. } // namespace boost
  397. #endif // header