00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef TORRENT_IO_HPP_INCLUDED
00034 #define TORRENT_IO_HPP_INCLUDED
00035
00036 #include <boost/cstdint.hpp>
00037
00038 namespace libtorrent
00039 {
00040 namespace detail
00041 {
00042 template <class T> struct type {};
00043
00044
00045
00046
00047 template <class T, class InIt>
00048 inline T read_impl(InIt& start, type<T>)
00049 {
00050 T ret = 0;
00051 for (int i = 0; i < (int)sizeof(T); ++i)
00052 {
00053 ret <<= 8;
00054 ret |= static_cast<unsigned char>(*start);
00055 ++start;
00056 }
00057 return ret;
00058 }
00059
00060 template <class T, class OutIt>
00061 inline void write_impl(T val, OutIt& start)
00062 {
00063 for (int i = (int)sizeof(T)-1; i >= 0; --i)
00064 {
00065 *start = static_cast<unsigned char>((val >> (i * 8)) & 0xff);
00066 ++start;
00067 }
00068 }
00069
00070
00071
00072 template <class InIt>
00073 boost::int64_t read_int64(InIt& start)
00074 { return read_impl(start, type<boost::int64_t>()); }
00075
00076 template <class InIt>
00077 boost::uint64_t read_uint64(InIt& start)
00078 { return read_impl(start, type<boost::uint64_t>()); }
00079
00080 template <class InIt>
00081 boost::uint32_t read_uint32(InIt& start)
00082 { return read_impl(start, type<boost::uint32_t>()); }
00083
00084 template <class InIt>
00085 boost::int32_t read_int32(InIt& start)
00086 { return read_impl(start, type<boost::int32_t>()); }
00087
00088 template <class InIt>
00089 boost::int16_t read_int16(InIt& start)
00090 { return read_impl(start, type<boost::int16_t>()); }
00091
00092 template <class InIt>
00093 boost::uint16_t read_uint16(InIt& start)
00094 { return read_impl(start, type<boost::uint16_t>()); }
00095
00096 template <class InIt>
00097 boost::int8_t read_int8(InIt& start)
00098 { return read_impl(start, type<boost::int8_t>()); }
00099
00100 template <class InIt>
00101 boost::uint8_t read_uint8(InIt& start)
00102 { return read_impl(start, type<boost::uint8_t>()); }
00103
00104
00105 template <class OutIt>
00106 void write_uint64(boost::uint64_t val, OutIt& start)
00107 { write_impl(val, start); }
00108
00109 template <class OutIt>
00110 void write_int64(boost::int64_t val, OutIt& start)
00111 { write_impl(val, start); }
00112
00113 template <class OutIt>
00114 void write_uint32(boost::uint32_t val, OutIt& start)
00115 { write_impl(val, start); }
00116
00117 template <class OutIt>
00118 void write_int32(boost::int32_t val, OutIt& start)
00119 { write_impl(val, start); }
00120
00121 template <class OutIt>
00122 void write_uint16(boost::uint16_t val, OutIt& start)
00123 { write_impl(val, start); }
00124
00125 template <class OutIt>
00126 void write_int16(boost::int16_t val, OutIt& start)
00127 { write_impl(val, start); }
00128
00129 template <class OutIt>
00130 void write_uint8(boost::uint8_t val, OutIt& start)
00131 { write_impl(val, start); }
00132
00133 template <class OutIt>
00134 void write_int8(boost::int8_t val, OutIt& start)
00135 { write_impl(val, start); }
00136
00137 }
00138 }
00139
00140 #endif // TORRENT_IO_HPP_INCLUDED