/** * \file IMP/rmf/Category.h * \brief Handle read/write of Model data from/to files. * * Copyright 2007-2013 IMP Inventors. All rights reserved. * */ #include #include #include #include #include IMPRMF_BEGIN_NAMESPACE namespace { class ParticleLoadLink: public SimpleLoadLink { typedef SimpleLoadLink P; Pointer m_; template void load_keys(RMF::FileConstHandle fh, RMF::Category cat, base::map &map) { typedef typename RK::TypeTraits Traits; std::vector ks= fh.get_keys(cat); for (unsigned int i=0; i< ks.size(); ++i) { IK ik(fh.get_name(ks[i])); map[ks[i]]= ik; IMP_LOG_TERSE( "Found " << ks[i] << " with " << ik << std::endl); } for (typename base::map::const_iterator it= map.begin(); it != map.end(); ++it) { IMP_LOG_TERSE( "Added key assoc " << fh.get_name(it->first) << " with " << it->second << std::endl); } } template void load_one(Particle *o, RMF::NodeConstHandle nh, RMF::Category cat) { base::map map; load_keys(nh.get_file(), cat, map); /*RMF::show_hierarchy_with_values(nh, frame);*/ for (typename base::map::const_iterator it= map.begin(); it != map.end(); ++it) { if (nh.get_has_value(it->first)) { IK ik= it->second; if (o->has_attribute(ik)) { o->set_value(ik, nh.get_value(it->first)); } else { o->add_attribute(ik, nh.get_value(it->first)); } } else { if (o->has_attribute(it->second)) { o->remove_attribute(it->second); } std::cout << "No value for " << it->first << std::endl; } } } void do_load_one( RMF::NodeConstHandle nh, Particle *o) { RMF::Category cat=nh.get_file().get_category("IMP"); load_one(o, nh, cat); load_one(o, nh, cat); load_one(o, nh, cat); } bool get_is(RMF::NodeConstHandle nh) const { return nh.get_type()==RMF::CUSTOM; } Particle* do_create(RMF::NodeConstHandle name) { return new Particle(m_, name.get_name()); } public: ParticleLoadLink(RMF::FileConstHandle , Model *m): P("ParticleLoadLink%1%") { m_=m; } IMP_OBJECT_INLINE(ParticleLoadLink,IMP_UNUSED(out),); }; class ParticleSaveLink: public SimpleSaveLink { typedef SimpleSaveLink P; RMF::Category cat_; base::map float_; base::map int_; base::map string_; template void save_one(Particle *o, const base::Vector &ks, RMF::NodeHandle nh, base::map &map) { for (unsigned int i=0; i< ks.size(); ++i) { if (map.find(ks[i]) == map.end()) { map[ks[i]] = nh.get_file().get_key(cat_, ks[i].get_string()); } nh.set_value(map.find(ks[i])->second, o->get_value(ks[i])); } } void do_save_one(Particle *o, RMF::NodeHandle nh) { save_one(o, o->get_float_keys(), nh, float_); save_one(o, o->get_int_keys(), nh, int_); save_one(o, o->get_string_keys(), nh, string_); } RMF::NodeType get_type(Particle*) const { return RMF::CUSTOM; } public: ParticleSaveLink(RMF::FileHandle fh): P("ParticleSaveLink%1%"){ cat_=fh.get_category("IMP"); } IMP_OBJECT_INLINE(ParticleSaveLink,IMP_UNUSED(out),); }; } IMP_DEFINE_LINKERS(Particle, particle, particles, Particle*, ParticlesTemp, Particle*, ParticlesTemp, (RMF::FileHandle fh), (RMF::FileConstHandle fh, Model *m), (fh), (fh, m), (fh, IMP::internal::get_model(hs))); IMPRMF_END_NAMESPACE