00001 00025 #include "config.h" 00026 #include "libnebular.hpp" 00027 #include "libnebular-plugin.hpp" 00028 00029 #include <sstream> 00030 00031 #ifndef LIBNEBULAR_EXTRA_TEMPLATES_HPP_ 00032 #define LIBNEBULAR_EXTRA_TEMPLATES_HPP_ 00033 00034 #if HAVE_BOOST_LEXICAL_CAST_HPP 00035 #include <boost/lexical_cast.hpp> 00036 #endif 00037 00038 namespace libnebular{ 00039 #if HAVE_BOOST_LEXICAL_CAST_HPP 00040 00041 template <typename Src> String PicturePropContainer::castToPropString(const Src &value){ 00042 try{ 00043 String result = boost::lexical_cast<String>(value); 00044 return result; 00045 }catch(boost::bad_lexical_cast &){ 00046 throw LIBNEBULAR_ERROR("Cannot convert from requested type to property string"); 00047 } 00048 } 00049 00050 template <typename Dest> Dest PicturePropContainer::castFromPropString(const String &value){ 00051 try{ 00052 if( 00053 boost::is_integral<Dest>::value 00054 ){ 00055 const String hexPrefix = "0x"; 00056 if( value.substr(0, hexPrefix.length()) == hexPrefix ){ 00057 std::istringstream streamFromArg(value.substr( hexPrefix.length() )); 00058 Dest result; 00059 streamFromArg >> std::hex >> result; 00060 return result; 00061 } 00062 } 00063 00064 Dest result = boost::lexical_cast<Dest>(value); 00065 return result; 00066 }catch(boost::bad_lexical_cast &){ 00067 throw LIBNEBULAR_ERROR(String() + "Cannot convert from property string \"" + 00068 value + "\" to requested type"); 00069 } 00070 } 00071 00072 #endif // HAVE_BOOST_LEXICAL_CAST_HPP 00073 00074 00075 namespace bits{ 00076 template <typename BitContainer> 00077 Integer getContigLength(BitContainer a){ 00078 if(a < 0){ 00079 throw LIBNEBULAR_ERROR("Bit mask shouldn't be negative"); 00080 } 00081 00082 while( a != 0 && ((a & 1) == 0) ){ 00083 a >>= 1; 00084 } 00085 00086 Integer length = 0; 00087 while( ((a & 1) != 0) ){ 00088 a >>= 1; 00089 ++length; 00090 } 00091 00092 if(a != 0){ 00093 return ARE_NOT_CONTIG; 00094 } 00095 00096 return length; 00097 } 00098 00099 template <typename BitContainer> 00100 Integer getHighestBitIndex(BitContainer a){ 00101 Integer highestBitIndex = -1; 00102 if(a < 0){ 00103 throw LIBNEBULAR_ERROR("Bit mask shouldn't be negative"); 00104 } 00105 00106 while( a != 0 ){ 00107 a >>= 1; 00108 ++highestBitIndex; 00109 } 00110 00111 if(highestBitIndex == -1){ 00112 return HAVE_NOT; 00113 } 00114 00115 return highestBitIndex; 00116 } 00117 00118 00119 template <typename BitContainer> 00120 bool areContig(BitContainer a){ 00121 return getContigLength(a) != ARE_NOT_CONTIG; 00122 } 00123 00124 template <typename BitContainer> 00125 bool areSubsetOf(BitContainer a, BitContainer b){ 00126 return (~a & b) == 0; 00127 } 00128 00129 template <typename BitContainer> 00130 BitContainer generateContig(Integer length){ 00131 BitContainer bits = 0; 00132 for(Integer idxBit = 0; idxBit < length; ++idxBit){ 00133 bits <<= 1; 00134 ++bits; 00135 } 00136 return bits; 00137 } 00138 } 00139 00140 } 00141 00142 #endif // LIBNEBULAR_EXTRA_TEMPLATES_HPP_