typename LIB::Ptr library () { const std::string lname = LIB::library_namespace(); //instead of LIB::type_name(); Component::Ptr clib = get_child_ptr(lname); typename LIB::Ptr lib; if ( is_null(clib) ) // doesnt exist so build it { CF::Common::TypeInfo::instance().regist< LIB >( lname ); lib = create_component_ptr< LIB >(lname); cf_assert( is_not_null(lib) ); return lib; } // try to convert existing ptr to LIB::Ptr and return it lib = clib->as_ptr<LIB>(); if( is_null(lib) ) // conversion failed throw CastingFailed( FromHere(), "Found component in CLibraries with name " + lname + " but is not the actual library " + LIB::type_name() ); return lib; }
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 { 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>"); } } }
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> > >()); } } }
void CSetFieldValues::config_field() { URI uri; option("Field").put_value(uri); m_field = Core::instance().root().access_component_ptr(uri)->as_ptr<CField>(); if ( is_null(m_field.lock()) ) throw CastingFailed (FromHere(), "Field must be of a CField or derived type"); }
void CField::config_tree() { URI topology_uri; option("Topology").put_value(topology_uri); CRegion::Ptr topology = Core::instance().root().access_component(topology_uri).as_ptr<CRegion>(); if ( is_null(topology) ) throw CastingFailed (FromHere(), "Topology must be of a CRegion or derived type"); m_topology->link_to(topology); }
TYPE any_to_value( const boost::any & value ) { try { return boost::any_cast< TYPE >( value ); } catch(boost::bad_any_cast& e) { throw CastingFailed( FromHere(), "Bad boost::any cast from ["+ any_type(value) + "] to [" + class_name<TYPE>() + "]."); } }
void LoopOperation::config_elements() { // Safeguard in case elements are set using set_elements // otherwise this would get triggered if (m_call_config_elements) { const URI uri = options().value<URI>("elements"); m_elements = access_component_checked(uri)->handle<Entities>(); if ( is_null(m_elements) ) throw CastingFailed (FromHere(), "Elements must be of a Entities or derived type"); } }
void CLoopOperation::config_elements() { // Safeguard in case elements are set using set_elements // otherwise this would get triggered if (m_call_config_elements) { URI uri; option("Elements").put_value(uri); m_elements = access_component_ptr_checked(uri)->as_ptr_checked<CEntities>(); if ( is_null(m_elements.lock()) ) throw CastingFailed (FromHere(), "Elements must be of a CEntities or derived type"); } }
Handle<LIB> library () { const std::string lname = LIB::library_namespace(); //instead of LIB::type_name(); Handle<Component> clib = get_child(lname); if ( is_null(clib) ) // doesnt exist so build it { cf3::common::TypeInfo::instance().regist< LIB >( lname ); Handle<LIB> lib(create_component< LIB >(lname)); cf3_assert( is_not_null(lib) ); return lib; } // try to convert existing ptr to Handle<LIB> and return it Handle<LIB> lib(clib); if( is_null(lib) ) // conversion failed throw CastingFailed( FromHere(), "Found component in Libraries with name " + lname + " but is not the actual library " + LIB::type_name() ); return lib; }
GraphicalValue * GraphicalValue::createFromOption(Option::ConstPtr option, QWidget * parent) { GraphicalValue * value = nullptr; if(option.get() == nullptr) return value; std::string tag(option->tag()); if(tag != "array" ) { if(option->has_restricted_list()) value = new GraphicalRestrictedList(option, parent); else { std::string type(option->type()); if(type == Protocol::Tags::type<bool>()) // bool option value = new GraphicalBool(option->value<bool>(), parent); else if(type == Protocol::Tags::type<Real>()) // Real option value = new GraphicalDouble(option->value<Real>(), parent); else if(type == Protocol::Tags::type<int>()) // int option value = new GraphicalInt(false, option->value<int>(), parent); else if(type == Protocol::Tags::type<Uint>()) // Uint option value = new GraphicalInt(true, option->value<Uint>(), parent); else if(type == Protocol::Tags::type<std::string>()) // string option value = new GraphicalString(option->value<std::string>().c_str(), parent); else if(type == Protocol::Tags::type<URI>()) // URI option value = new GraphicalUri(boost::dynamic_pointer_cast<OptionURI const>(option), parent); else throw CastingFailed(FromHere(), tag + ": Unknown type"); } } else { if(option->has_restricted_list()) value = new GraphicalArrayRestrictedList(option, parent); else { OptionArray::ConstPtr array = boost::dynamic_pointer_cast<OptionArray const>(option); std::string value_str( array->value_str() ); std::string type( array->elem_type() ); QString sep( array->separator().c_str() ); if(type == Protocol::Tags::type<bool>()) // bool option { QRegExp regex("(true)|(false)|(1)|(0)|(on)|(off)"); value = new GraphicalArray(new QRegExpValidator(regex, parent), sep, parent); } else if(type == Protocol::Tags::type<Real>()) // Real option { QDoubleValidator * val = new QDoubleValidator(nullptr); val->setNotation(QDoubleValidator::ScientificNotation); value = new GraphicalArray(val, sep, parent); } else if(type == Protocol::Tags::type<int>()) // int option value = new GraphicalArray(new QIntValidator(), sep, parent); else if(type == Protocol::Tags::type<Uint>()) // Uint option { QIntValidator * val = new QIntValidator(); val->setBottom(0); value = new GraphicalArray(val, sep, parent); } else if(type == Protocol::Tags::type<std::string>()) // string option value = new GraphicalArray(nullptr,sep, parent); else if(type == Protocol::Tags::type<URI>()) // URI option value = new GraphicalUriArray(sep, parent); else throw CastingFailed(FromHere(), tag + ": Unknown type"); value->setValue( QString(value_str.c_str()).split(array->separator().c_str()) ); } } return value; }