user_types.qbk 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. [/
  2. Copyright (c) Vladimir Batov 2009-2016
  3. Distributed under the Boost Software License, Version 1.0.
  4. See copy at http://www.boost.org/LICENSE_1_0.txt.
  5. ]
  6. [import ../test/strtol_converter.cpp]
  7. [section:integration_of_user_types Integration of User-Defined Types]
  8. Probably the most obvious way to support a new type is to write a converter (potentially by inheriting from an existing converter) that understands the type and implements whatever transformation functionality is required. That said, conversions to certain types are very common (for example, string\/text-related conversions) and, consequently, an ability to ['extend] support of an ['existing] converter onto a user-defined type might be the preferred option. The obvious example of such design might be the ['`std::iostream`] library and its type-integration mechanism based on
  9. std::istream& operator>>(std::istream&, Type&); // For input
  10. std::ostream& operator<<(std::ostream&, Type const&); // For output
  11. Within the ['Boost.Convert] framework the integration and support of user-defined types is every converter's private business. Every converter is free to implement its own (or re-use an existing) user-type-integration mechanism.
  12. [heading ['boost::cnv::stream et al.]]
  13. Unsurprisingly, the converters based on the ['`std::iostream`] library use the mechanism introduced and supported by that library. That is,
  14. * for output ['Type] needs to be ['Output Streamable];
  15. * for input ['Type] needs to be ['Input Streamable].
  16. which in practical terms means that the type needs to have the following operators defined:
  17. std::istream& operator>>(std::istream&, Type&); // For input
  18. std::ostream& operator<<(std::ostream&, Type const&); // For output
  19. For example,
  20. [change_declaration]
  21. [change_stream_operators]
  22. That allows handling conversions of user-defined types with ['`std::iostream`]-based converters:
  23. [stream_example6]
  24. [heading ['boost::cnv::strtol et al.]]
  25. Other converters (based on `boost::cnv::cnvbase`) implement support for user types similarly but without the ['`std::iostream`]-related overhead (see [link boost_convert.performance.converters_compared Converters Compared]). Namely, new types are supported by the converters after the following is defined:
  26. void operator>>(TypeIn const&, boost::optional<TypeOut>&);
  27. For example, the mentioned ['`change`] class is deployed with `boost::cnv::strol` after the following ['change-to-string] and ['string-to-change] conversions are defined:
  28. [change_convert_operators]
  29. which are not that dissimilar to (but considerably more efficient than) previously shown:
  30. std::istream& operator>>(std::istream&, change&);
  31. std::ostream& operator<<(std::ostream&, change const&);
  32. That allows handling conversions of user-defined types with `boost::cnv::strtol`:
  33. [strtol_user_type]
  34. [endsect]