Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

Fragment.hpp

Go to the documentation of this file.
00001 //
00002 // FileFragments/Fragment.hpp
00003 //
00004 // Copyright (c) Shareaza Development Team, 2002-2005.
00005 // This file is part of SHAREAZA (www.shareaza.com)
00006 //
00007 // Shareaza is free software; you can redistribute it
00008 // and/or modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2 of
00010 // the License, or (at your option) any later version.
00011 //
00012 // Shareaza is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with Shareaza; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 //            throw BadFragmentException( begin, end, payload );
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 //            throw BadFragmentException( range.first, range.second, payload );
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     // A resizing copy-c'tor 
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 //            throw BadFragmentException( begin, end, other );
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     // Fragment conversion
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 //Implementation
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 } // namespace detail
00139 
00140 #endif // #ifndef FILEFRAGMENTS_FRAGMENT_HPP_INCLUDED

Generated on Thu Dec 15 10:39:42 2005 for Shareaza 2.2.1.0 by  doxygen 1.4.2