00001 // 00002 // FileFragments.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_HPP_INCLUDED 00023 #define FILEFRAGMENTS_HPP_INCLUDED 00024 00025 #include <utility> 00026 #include <stdexcept> 00027 #include <algorithm> 00028 #include <functional> 00029 #include <deque> 00030 #include <list> 00031 #include <set> 00032 #include <limits> 00033 00034 #ifdef _MSC_VER // ToDo: use boost/cstdint.hpp instead 00035 typedef unsigned __int64 u64; // http://sourceforge.net/projects/boost/ 00036 typedef unsigned __int32 u32; 00037 typedef unsigned __int16 u16; 00038 typedef unsigned __int8 u8; 00039 typedef __int64 i64; 00040 typedef __int32 i32; 00041 typedef __int16 i16; 00042 typedef __int8 i8; 00043 #else 00044 typedef unsigned long long u64; 00045 typedef unsigned int u32; 00046 typedef unsigned short u16; 00047 typedef unsigned char u8; 00048 typedef long long i64; 00049 typedef int i32; 00050 typedef short i16; 00051 typedef signed char i8; 00052 #endif 00053 00054 #include "Shareaza.h" 00055 00056 namespace FF // FileFragments 00057 { 00058 00059 namespace detail 00060 { 00061 00062 // Forward declarations 00063 00064 // @ Exception The general exception class 00065 // All exceptions thrown here will be of that type or 00066 // derived from it, except for std::bad_alloc and 00067 // exceptions thrown by the Payload class 00068 class Exception; 00069 // @BadFragment Will be thrown when trying to create a fragment with 00070 // negative size ( or zero size if that's forbidden by traits ) 00071 template< class FragmentT > class BadFragment; 00072 // @BadRange Thrown by List when trying to insert or erase a fragment 00073 // which excceds the limits of the list 00074 template< class FragmentT > class BadRange; 00075 00076 // @Payload defines traits for any Payload type which serves as value 00077 // type for a fragment, currently used: 00078 // whether empty Fragments are allowed and 00079 // the strategy used for merging fragments 00080 template< class Payload > struct PayloadTraits; 00081 00082 // @Fragment defines Fragments, takes the Payload type to be used and the 00083 // integral type used to represent offsets 00084 // Payload type must be of class type and have accessible default, 00085 // and copy-constructors, assignment and destructor. 00086 // Its Copy-c'tor and assignment are not allowed to throw. 00087 // This allows a fragment to transport more information aside 00088 // from the range it represents, for example a download source 00089 // (to improve bad source detection and alike) or a counter 00090 // that will be increased when merging occurs in a list. 00091 // No function aside from contructors throw. 00092 template 00093 < 00094 class Payload = EmptyType, 00095 typename OffsetType = u64 00096 > 00097 class Fragment; 00098 template< class FragmentT > struct CompareFragments; 00099 00100 // @List Defines a near container that stores fragments and provides 00101 // automatic sorting and merging (according to traits). 00102 // Searching, insertion and deletion are generally done in 00103 // logarithmic time. 00104 // Iterators may not be used to change a fragment, but are needed 00105 // in conjunction with insert and erase operations. 00106 // Insert and erase always invalidate all iterators into the 00107 // container except for the erase that takes a single iterators; 00108 // that method only invalidates the argument. 00109 // All methods are safe and transparant in the presence of 00110 // exceptions, mutating operations will leave the list in a 00111 // valid if unpredictable state. Swap never throws. Non-mutating 00112 // standard-algorithms can be used. 00113 template 00114 < 00115 class FragmentT, 00116 class ContainerT = ::std::set< FragmentT, CompareFragments< FragmentT > > 00117 > 00118 class List; 00119 00120 // @Queue A queue for simple fragments (without Payload), but 00121 // without access restrictions, provides methods to erase 00122 // fragments directly. 00123 class Queue; 00124 00125 } // namespace detail 00126 00127 class EmptyType { }; 00128 00129 #include "FileFragments/Exception.hpp" 00130 #include "FileFragments/PayloadTraits.hpp" 00131 #include "FileFragments/Fragment.hpp" 00132 #include "FileFragments/List.hpp" 00133 #include "FileFragments/Queue.hpp" 00134 00135 using detail::Exception; 00136 00137 typedef detail::Fragment< EmptyType, u64 > SimpleFragment; 00138 typedef detail::BadFragment< SimpleFragment > SimpleBadFragment; 00139 typedef detail::BadRange< SimpleFragment > SimpleBadRange; 00140 typedef detail::List< SimpleFragment > SimpleFragmentList; 00141 typedef detail::Queue SimpleFragmentQueue; 00142 00143 // explicit instances to force errors and improve compilation speed 00144 template SimpleFragment; 00145 template SimpleBadFragment; 00146 template SimpleBadRange; 00147 template SimpleFragmentList; 00148 00149 #include "FileFragments/Compatibility.hpp" 00150 00151 } // namespace FF 00152 00153 #endif // #ifndef FILEFRAGMENTS_HPP_INCLUDED