00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef FILEFRAGMENTS_FRAGMENT_HPP_INCLUDED
00023 #define FILEFRAGMENTS_FRAGMENT_HPP_INCLUDED
00024
00025 namespace detail
00026 {
00027
00028 template< class Payload, typename OffsetType >
00029 class Fragment : private Payload
00030 {
00031 public:
00032 typedef Payload PayloadType;
00033 typedef PayloadTraits< PayloadType > Traits;
00034 typedef OffsetType SizeType;
00035 typedef BadFragment< Fragment > BadFragmentException;
00036 Fragment(SizeType begin, SizeType end,
00037 const PayloadType& payload = PayloadType() )
00038 : Payload( payload ), range_( begin, end )
00039 {
00040 if( Traits::allowEmptyFragments
00041 ? end < begin
00042 : end <= begin )
00043 {
00044
00045 range_.first = ~0ULL - 1000;
00046 range_.second = ~0ULL;
00047 CString errorMsg;
00048 errorMsg.Format( _T( "FF::SimpleFragment - invalid args in c'tor - begin: %I64u - end: %I64u" ),
00049 begin, end );
00050 theApp.Message( MSG_ERROR, errorMsg );
00051 }
00052 }
00053 Fragment(::std::pair< SizeType, SizeType > range,
00054 const PayloadType& payload = PayloadType() )
00055 : Payload( payload ), range_( range )
00056 {
00057 if( Traits::allowEmptyFragments
00058 ? range.second < range.first
00059 : range.second <= range.first )
00060 {
00061
00062 range_.first = ~0ULL - 1000;
00063 range_.second = ~0ULL;
00064 CString errorMsg;
00065 errorMsg.Format( _T( "FF::SimpleFragment - invalid args in c'tor (pair) - begin: %I64u - end: %I64u" ),
00066 begin, end );
00067 theApp.Message( MSG_ERROR, errorMsg );
00068 }
00069 }
00070
00071
00072 Fragment(const Fragment& other, SizeType begin, SizeType end)
00073 : Payload( other ), range_( begin, end )
00074 {
00075 if( Traits::allowEmptyFragments
00076 ? end < begin
00077 : end <= begin )
00078 {
00079
00080 range_.first = ~0ULL - 1000;
00081 range_.second = ~0ULL;
00082 CString errorMsg;
00083 errorMsg.Format( _T( "FF::SimpleFragment - invalid args in c'tor (resize) - begin: %I64u - end: %I64u" ),
00084 begin, end );
00085 theApp.Message( MSG_ERROR, errorMsg );
00086 }
00087 }
00088
00089
00090 template< class OtherPayload, typename OtherOffsetType >
00091 Fragment(const Fragment< OtherPayload, OtherOffsetType >& other,
00092 const PayloadType& payload = PayloadType() )
00093 : Payload( payload ), range_( other.range_ )
00094 { }
00095
00096 SizeType begin() const { return range_.first; }
00097 SizeType end() const { return range_.second; }
00098 SizeType size() const { return end() - begin(); }
00099 SizeType length() const { return size(); }
00100
00101 const PayloadType& value() const { return *this; }
00102
00103 private:
00104 ::std::pair< SizeType, SizeType > range_;
00105 };
00106
00107 template
00108 <
00109 class Payload,
00110 typename OffsetType
00111 >
00112 bool operator==(
00113 const Fragment< Payload, OffsetType >& lhs,
00114 const Fragment< Payload, OffsetType >& rhs )
00115 {
00116 return lhs.begin() == rhs.begin() && lhs.end() == rhs.end()
00117 && Fragment< Payload, OffsetType >::Traits::equals( lhs, rhs );
00118 }
00119
00120 template< class FragmentT >
00121 struct CompareFragments
00122 : public ::std::binary_function< FragmentT, FragmentT, bool >
00123 {
00124 typedef FragmentT FragmentType;
00125 typedef typename FragmentType::Traits Traits;
00126 typedef ::std::binary_function< FragmentType, FragmentType, bool >
00127 TemplateType;
00128 typedef typename TemplateType::result_type ResultType;
00129 typedef typename TemplateType::first_argument_type FirstArgumentType;
00130 typedef typename TemplateType::second_argument_type SecondArgumentType;
00131 ResultType operator()(FirstArgumentType lhs, SecondArgumentType rhs) const
00132 {
00133 return lhs.end() < rhs.begin()
00134 || lhs.end() == rhs.begin() && !Traits::equals( lhs, rhs );
00135 }
00136 };
00137
00138 }
00139
00140 #endif // #ifndef FILEFRAGMENTS_FRAGMENT_HPP_INCLUDED