inline void operator()(boost::any& to_set, const boost::any& new_value) { if(new_value.type() == to_set.type()) { to_set = new_value; } else { try { std::vector<Uint> int_vals = boost::any_cast< std::vector<Uint> >(new_value); const Uint nb_vals = int_vals.size(); std::vector<int> result(nb_vals); for(Uint i = 0; i != nb_vals; ++i) { result[i] = static_cast<int>(int_vals[i]); } to_set = result; } catch(boost::bad_any_cast& e) { throw CastingFailed(FromHere(), std::string("Failed to cast object of type ") + new_value.type().name() + " to type std::vector<int>"); } } }
inline void operator()(boost::any& to_set, const boost::any& new_value) { if(new_value.type() == to_set.type()) { to_set = new_value; } else { try { const std::vector< Handle<Component> > generic_value = boost::any_cast< std::vector< Handle<Component> > >(new_value); const Uint nb_entries = generic_value.size(); std::vector< Handle<ComponentT> > new_components(nb_entries); for(Uint i = 0; i != nb_entries; ++i) { new_components[i] = Handle<ComponentT>(generic_value[i]); } to_set = new_components; } catch(boost::bad_any_cast& e) { throw CastingFailed(FromHere(), std::string("Failed to cast object of type ") + new_value.type().name() + " to type " + class_name< std::vector< Handle<ComponentT> > >()); } } }
inline void operator()(boost::any& to_set, const boost::any& new_value) { if(new_value.type() == to_set.type()) { to_set = new_value; } else { try { std::vector<int> int_vals = boost::any_cast< std::vector<int> >(new_value); const Uint nb_vals = int_vals.size(); std::vector<Uint> result(nb_vals); for(Uint i = 0; i != nb_vals; ++i) { if(int_vals[i] < 0) { std::stringstream err; err << "Tried to store a negative value in an unsigned int option array at index " << i; throw BadValue(FromHere(), err.str()); } result[i] = static_cast<Uint>(int_vals[i]); } to_set = result; } catch(boost::bad_any_cast& e) { throw CastingFailed(FromHere(), std::string("Failed to cast object of type ") + new_value.type().name() + " to type std::vector<Uint>"); } } }
void ItemAttribute::set(boost::any a) { clear(); if(a.empty()) return; if(a.type() == typeid(std::string)) { type = STRING; new(data) std::string(boost::any_cast<std::string>(a)); } else if(a.type() == typeid(int32_t)) { type = INTEGER; *reinterpret_cast<int32_t*>(&data) = boost::any_cast<int32_t>(a); } else if(a.type() == typeid(float)) { type = FLOAT; *reinterpret_cast<float*>(&data) = boost::any_cast<float>(a); } else if(a.type() == typeid(bool)) { type = BOOLEAN; *reinterpret_cast<bool*>(&data) = boost::any_cast<bool>(a); } }
template<class T> inline bool is_type( const boost::any& a ) { // see issue on boost, https://svn.boost.org/trac/boost/ticket/754 #if defined __GNUC__ return std::string( a.type().name() ) == typeid( T ).name(); #else return a.type() == typeid( T ); #endif }
static bool is_a( const boost::any& a ) { #if defined __GNUC__ // See issue on boost. https://svn.boost.org/trac/boost/ticket/754 return std::strcmp( a.type().name(), typeid( T ).name() ) == 0; #else return a.type() == typeid( T ); #endif }
UInt32 SceneTreeModel::getChildCount(const boost::any& parent) const { if(parent.type() == typeid(Scene* const)) { return SceneComponentsLast; } else if(parent.type() == typeid(UInt32)) { UInt32 Value(boost::any_cast<UInt32>(parent)); switch(Value) { case BasicComponent: //Basic case BackgroundComponent: //Background case CameraComponent: //Camera return 0; break; case ForegroundsComponent: //Foregrounds return getInternalRoot()->getPrimaryViewport()->getMFForegrounds()->size(); break; case SceneObjectsComponent: //Models return getInternalRoot()->getMFSceneObjects()->size(); break; case LightsComponent: //Lights { NodeRecPtr LightNode(getInternalRoot()->getPrimaryViewport()->getRoot()); UInt32 NumLights(0); while(LightNode->getNChildren() > 0 && LightNode->getChild(0)->getCore()->getType().isDerivedFrom(Light::getClassType())) { LightNode = LightNode->getChild(0); ++NumLights; } return NumLights; } break; case DynamicsComponent: //Dynamics return DynamicsComponentsLast - DynamicsComponentsFirst; break; case ScriptsComponent: //Scripts return 0; break; case BehavioursComponent: //Behaviours return 0; break; case AnimationsComponent: //Animations return 0; break; } } else { return 0; } }
bool Parameter::support(const boost::any& value) { if (value.type() == typeid(int) || value.type() == typeid(bool) || value.type() == typeid(double) || value.type() == typeid(string)) { return true; } return false; }
void PropertyConstraintList::setPathValue(const ObjectIdentifier &path, const boost::any &value) { const ObjectIdentifier::Component & c0 = path.getPropertyComponent(0); double dvalue; if (value.type() == typeid(double)) dvalue = boost::any_cast<double>(value); else if (value.type() == typeid(Quantity)) dvalue = (boost::any_cast<Quantity>(value)).getValue(); else throw std::bad_cast(); if (c0.isArray() && path.numSubComponents() == 1) { int index = c0.getIndex(); if (c0.getIndex() >= _lValueList.size()) throw Base::Exception("Array out of bounds"); switch (_lValueList[index]->Type) { case Angle: dvalue = Base::toRadians<double>(dvalue); break; default: break; } aboutToSetValue(); _lValueList[index]->setValue(dvalue); hasSetValue(); return; } else if (c0.isSimple() && path.numSubComponents() == 2) { ObjectIdentifier::Component c1 = path.getPropertyComponent(1); for (std::vector<Constraint *>::const_iterator it = _lValueList.begin(); it != _lValueList.end(); ++it) { int index = it - _lValueList.begin(); if ((*it)->Name == c1.getName()) { switch (_lValueList[index]->Type) { case Angle: dvalue = Base::toRadians<double>(dvalue); break; default: break; } aboutToSetValue(); _lValueList[index]->setValue(dvalue); hasSetValue(); return; } } } throw Base::Exception("Invalid constraint"); }
error convertToMsParam(boost::any &itr, msParam_t *t) { if(itr.type() == typeid(std::string)) { fillStrInMsParam( t, const_cast<char*>( boost::any_cast<std::string>(itr).c_str() )); } else if(itr.type() == typeid(std::string *)) { fillStrInMsParam( t, const_cast<char*>( (*(boost::any_cast<std::string *>(itr))).c_str() )); } else if(itr.type() == typeid(msParam_t*)) { memset(t, 0, sizeof(*t)); replMsParam(boost::any_cast<msParam_t*>(itr), t); } else { return ERROR(-1, "cannot convert parameter"); } return SUCCESS(); }
// Float tests 6 digits of accuracy void dc9() { ct.colWidth = 4; ct.constraintType = CalpontSystemCatalog::NO_CONSTRAINT; ct.colDataType = CalpontSystemCatalog::FLOAT; ct.scale = 0; ct.precision = 4; bool pushWarning; data = "0.123456"; anyval = converter.convertColumnData(ct, data, pushWarning, false); CPPUNIT_ASSERT(anyval.type() == typeid(float)); float floatval = any_cast<float>(anyval); CPPUNIT_ASSERT(inTolerance(floatval, 0.123456, 0.000001)); data = "3456.01"; anyval = converter.convertColumnData(ct, data, pushWarning, false); CPPUNIT_ASSERT(anyval.type() == typeid(float)); floatval = any_cast<float>(anyval); CPPUNIT_ASSERT(inTolerance(floatval, 3456.01, 0.01)); data = "(3456.01)"; anyval = converter.convertColumnData(ct, data, pushWarning, false); CPPUNIT_ASSERT(anyval.type() == typeid(float)); floatval = any_cast<float>(anyval); CPPUNIT_ASSERT(inTolerance(floatval, 3456.01, 0.01)); data = "6.02214E+23"; anyval = converter.convertColumnData(ct, data, pushWarning, false); CPPUNIT_ASSERT(anyval.type() == typeid(float)); floatval = any_cast<float>(anyval); CPPUNIT_ASSERT(inTolerance(floatval, 6.02214E+23, 0.00001E+23)); data = "1.60217E-19"; anyval = converter.convertColumnData(ct, data, pushWarning, false); CPPUNIT_ASSERT(anyval.type() == typeid(float)); floatval = any_cast<float>(anyval); CPPUNIT_ASSERT(inTolerance(floatval, 1.60217E-19, 0.00001E-19)); }
bool IniFile::set(const string& key, const boost::any& data) { if (data.type() == typeid(string)) return set(key, boost::any_cast<const string&>(data)); if (data.type() == typeid(unsigned)) return set(key, boost::any_cast<unsigned>(data)); if (data.type() == typeid(int)) return set(key, boost::any_cast<int>(data)); if (data.type() == typeid(bool)) return set(key, boost::any_cast<bool>(data)); gLog.warning(tr("Internal Error: IniFile::set, key=%s, data.type=%s"), key.c_str(), data.type().name()); return false; }
BOOL CStatFilePlugin::ProcessData( boost::any& anyData ) { if(anyData.type() == typeid(std::string)) { std::string strPath = boost::any_cast<std::string>(anyData); if(::GetFileAttributes(strPath.c_str())==-1) { std::cout<<"目录不存在"<<std::endl; return FALSE; } m_info.m_strPath = strPath; std::cout<<"现在正在统计的是:"<<m_info.m_strPath<<std::endl; std::cout<<"请等待!!!"<<std::endl; std::string strTmpDir = m_info.m_strPath + TEXT("\\"); // 统计文件数 StatAllFileInFolder(strTmpDir); std::cout<<"文件夹数:"<<m_info.m_FolderNum<<std::endl; std::cout<<"文件数:"<<m_info.m_FileNum<<std::endl; CPluginFactory* pPluginFactory = CPluginFactory::Instance(); IPluginObjPtr ptrOuttoFile = pPluginFactory->GetPlugin(_T("OutputFile.dll")); boost::any anyInfo = m_info; ptrOuttoFile->ProcessData(anyInfo); return TRUE; } return FALSE; }
bool ArnModelW::setData(const WModelIndex& index, const boost::any& value, int role) { if (index.isValid() && role == Wt::EditRole) { ArnNode* node = nodeFromIndex( index); if (!node) return false; if (node->isFolder()) { if (node->_valueChild) { if (node->_setMap) { node->_valueChild->setValue( node->_setMap->key( toQString( boost::any_cast<WString>(value)))); } else { node->_valueChild->setValue( toQString( boost::any_cast<WString>(value))); } emitDataChangedI( index); } } else { if (value.type() == typeid(QVariant)) node->setValue( boost::any_cast<QVariant>(value)); else node->setValue( toQString( boost::any_cast<WString>(value))); emitDataChangedI( index); } return true; } return false; }
bool IniFile::get(const string& key, boost::any& data) const { if (data.type() == typeid(string)) return get(key, boost::any_cast<string&>(data)); if (data.type() == typeid(unsigned)) return get(key, boost::any_cast<unsigned&>(data)); if (data.type() == typeid(int)) return get(key, boost::any_cast<int&>(data)); if (data.type() == typeid(bool)) return get(key, boost::any_cast<bool&>(data)); // Unhandled data type gLog.warning(tr("Internal Error: IniFile::get, key=%s, data.type=%s"), key.c_str(), data.type().name()); return false; }
typename std::enable_if<std::is_copy_constructible<U>::value && std::is_nothrow_destructible<U>::value, void>::type setAsAnyImpl(C* object, const boost::any& value) const { if(value.type() == typeid(U)) object->*m_member = boost::any_cast<U>(value); else std::cout << "Script trying to set a value as boost::any but with an invalid type !" << std::endl; }
typename std::enable_if<std::is_copy_constructible<T>::value && std::is_nothrow_destructible<T>::value, T>::type ConvertFromAnyImpl(const boost::any& value) { if(value.type() == typeid(T)) return boost::any_cast<T>(value); else return T(); //TODO: Add a warning }
bool IsotopeForm::getContents( boost::any& any ) const { if ( any.type() != typeid( adcontrols::ProcessMethod* ) ) return false; adcontrols::ProcessMethod* pm = boost::any_cast< adcontrols::ProcessMethod* >( any ); pm->appendMethod( *pMethod_ ); return true; }
inline double euclidianNormSquared( Iterator it, const Iterator end, int num_components, const boost::any& pinfo = boost::any() ) { static_cast<void>(num_components); // Suppress warning in the serial case. static_cast<void>(pinfo); // Suppress warning in non-MPI case. #if HAVE_MPI if ( pinfo.type() == typeid(ParallelISTLInformation) ) { const ParallelISTLInformation& info = boost::any_cast<const ParallelISTLInformation&>(pinfo); typedef typename Iterator::value_type Scalar; Scalar product = 0.0; int size_per_component = (end - it); size_per_component /= num_components; // two lines to supresse unused warning. assert((end - it) == num_components * size_per_component); if( num_components == 1 ) { auto component_container = boost::make_iterator_range(it, end); info.computeReduction(component_container, Opm::Reduction::makeInnerProductFunctor<double>(), product); } else { auto& maskContainer = info.getOwnerMask(); auto mask = maskContainer.begin(); assert(static_cast<int>(maskContainer.size()) == size_per_component); for(int cell = 0; cell < size_per_component; ++cell, ++mask) { Scalar cell_product = (*it) * (*it); ++it; for(int component=1; component < num_components; ++component, ++it) { cell_product += (*it) * (*it); } product += cell_product * (*mask); } } return info.communicator().sum(product); } else #endif { double product = 0.0 ; for( ; it != end; ++it ) { product += ( *it * *it ); } return product; } }
bool ColumnBaseTyped<Type>::multiply(const boost::any& new_value){ if(new_value.empty()) return false; if(typeid(Type)==new_value.type()){ Type value = boost::any_cast<Type>(new_value); for(unsigned int i=0;i<this->size();i++){ this->operator[](i)*=value; } return true; } return false; }
void Object::update(const std::string& name, const boost::any& value) throw (boost::bad_any_cast) { check_field(name, value); if (!fields_[name].empty() && fields_[name].type() != value.type()) { throw boost::bad_any_cast(); } fields_[name] = value; ui_.push(id(), name, value); }
bool setData(const Wt::WModelIndex& index, const boost::any& value, int role=Wt::EditRole) { if (role == Wt::CheckStateRole && value.type() == typeid(bool)) { dbo::Transaction t(fApp->session()); const CommentPtr& o = resultRow(index.row()); o.modify()->set_deleted(boost::any_cast<bool>(value)); t.commit(); dataChanged().emit(index, this->index(index.row(), text_column)); return true; } return BaseQM::setData(index, value, Wt::EditRole); }
bool ColumnBaseTyped<Type>::minus(const boost::any& new_value){ //shared_pointer_namespace::shared_ptr<ColumnBaseTyped<Type> > typed_column = shared_pointer_namespace::static_pointer_cast<ColumnBaseTyped<Type> >(column); if(new_value.empty()) return false; if(typeid(Type)==new_value.type()){ Type value = boost::any_cast<Type>(new_value); for(unsigned int i=0;i<this->size();i++){ this->operator[](i)-=value; } return true; } return false; }
bool IsotopeForm::setContents( boost::any& a ) { if ( a.type() != typeid( adcontrols::ProcessMethod ) ) return false; adcontrols::ProcessMethod& pm = boost::any_cast< adcontrols::ProcessMethod& >(a); const adcontrols::IsotopeMethod* p = pm.find< adcontrols::IsotopeMethod >(); if ( ! p ) return false; *pMethod_ = *p; // update_data( *p ); return true; }
bool ColumnBaseTyped<Type>::division(const boost::any& new_value){ if(new_value.empty()) return false; if(typeid(Type)==new_value.type()){ Type value = boost::any_cast<Type>(new_value); //check that we do not devide by zero if(value==0) return false; for(unsigned int i=0;i<this->size();i++){ this->operator[](i)/=value; } return true; } return false; }
bool ColumnBaseTyped<Type>::add(const boost::any& new_value){ if(new_value.empty()) return false; if(typeid(Type)==new_value.type()){ Type value = boost::any_cast<Type>(new_value); //std::transform(myvec.begin(), myvec.end(), myvec.begin(), //bind2nd(std::plus<double>(), 1.0)); for(unsigned int i=0;i<this->size();i++){ this->operator[](i)+=value; } return true; } return false; }
error convertFromMsParam(boost::any& itr, msParam_t *t) { if(std::string(t->type).compare(STR_MS_T) == 0) { if(itr.type() == typeid(std::string *)) { *(boost::any_cast<std::string *>(itr)) = std::string(reinterpret_cast<char*>( t->inOutStruct) ); } } else if (t->type) { replMsParam(t, boost::any_cast<msParam_t*>(itr)); } else { return ERROR(-1, "type was null, cannot convert type"); } return SUCCESS(); }
// mid-range positive decimals (fixed-point) void dc6() { ct.colWidth = 8; ct.constraintType = CalpontSystemCatalog::NO_CONSTRAINT; ct.colDataType = CalpontSystemCatalog::BIGINT; ct.scale = 1; ct.precision = 18; bool pushWarning; data = "123456789012345.6"; anyval = converter.convertColumnData(ct, data, pushWarning, false); CPPUNIT_ASSERT(anyval.type() == typeid(long long)); int64_t bigintval = static_cast<int64_t>(any_cast<long long>(anyval)); CPPUNIT_ASSERT(bigintval == 1234567890123456LL); ct.scale = 3; data = "1234567890123.456"; anyval = converter.convertColumnData(ct, data, pushWarning, false); CPPUNIT_ASSERT(anyval.type() == typeid(long long)); bigintval = static_cast<int64_t>(any_cast<long long>(anyval)); CPPUNIT_ASSERT(bigintval == 1234567890123456LL); ct.scale = 4; data = "123456789012.3456"; anyval = converter.convertColumnData(ct, data, pushWarning, false); CPPUNIT_ASSERT(anyval.type() == typeid(long long)); bigintval = static_cast<int64_t>(any_cast<long long>(anyval)); CPPUNIT_ASSERT(bigintval == 1234567890123456LL); }
// Double tests 15 digits of accuracy void dc10() { ct.colWidth = 8; ct.constraintType = CalpontSystemCatalog::NO_CONSTRAINT; ct.colDataType = CalpontSystemCatalog::DOUBLE; ct.scale = 0; ct.precision = 4; bool pushWarning; data = "0.123456789012345"; anyval = converter.convertColumnData(ct, data, pushWarning, false); CPPUNIT_ASSERT(anyval.type() == typeid(double)); double doubleval = any_cast<double>(anyval); CPPUNIT_ASSERT(inTolerance(doubleval, 0.123456789012345, 0.000000000000001)); data = "123456.000000001"; anyval = converter.convertColumnData(ct, data, pushWarning, false); CPPUNIT_ASSERT(anyval.type() == typeid(double)); doubleval = any_cast<double>(anyval); CPPUNIT_ASSERT(inTolerance(doubleval, 123456.000000001, 0.000000001)); data = "(123456.000000001)"; anyval = converter.convertColumnData(ct, data, pushWarning, false); CPPUNIT_ASSERT(anyval.type() == typeid(double)); doubleval = any_cast<double>(anyval); CPPUNIT_ASSERT(inTolerance(doubleval, 123456.000000001, 0.000000001)); data = "6.02214179000000E+23"; anyval = converter.convertColumnData(ct, data, pushWarning, false); CPPUNIT_ASSERT(anyval.type() == typeid(double)); doubleval = any_cast<double>(anyval); CPPUNIT_ASSERT(inTolerance(doubleval, 6.02214179000000E+23, 0.00000000000001E+23)); data = "1.60217653140000E-19"; anyval = converter.convertColumnData(ct, data, pushWarning, false); CPPUNIT_ASSERT(anyval.type() == typeid(double)); doubleval = any_cast<double>(anyval); CPPUNIT_ASSERT(inTolerance(doubleval, 1.60217653140000E-19, 0.00000000000001E-19)); data = "3.14159265358979"; anyval = converter.convertColumnData(ct, data, pushWarning, false); CPPUNIT_ASSERT(anyval.type() == typeid(double)); doubleval = any_cast<double>(anyval); CPPUNIT_ASSERT(inTolerance(doubleval, 3.14159265358979, 0.00000000000001)); }
bool ValueParameter::set_unsafe(const boost::any& v) { bool change = true; if (!value_.empty()) { if (v.type() == typeid(int)) { change = boost::any_cast<int>(value_) != boost::any_cast<int>(v); } else if (v.type() == typeid(double)) { change = boost::any_cast<double>(value_) != boost::any_cast<double>(v); } else if (v.type() == typeid(bool)) { change = boost::any_cast<bool>(value_) != boost::any_cast<bool>(v); } else if (v.type() == typeid(std::string)) { change = boost::any_cast<std::string>(value_) != boost::any_cast<std::string>(v); } else if (v.type() == typeid(long)) { change = boost::any_cast<long>(value_) != boost::any_cast<long>(v); } } if (change) { value_ = v; return true; } return false; }