Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GridFactory.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file src/protocols/qsar/scoring_grid/GridFactory.cc
11 /// @author Sam DeLuca
12 
15 //#include <protocols/qsar/qsarTypeManager.hh>
16 #include <utility/exit.hh>
17 #include <utility/tag/Tag.hh>
18 
19 #include <utility/vector0.hh>
20 #include <utility/vector1.hh>
21 
22 
23 namespace protocols {
24 namespace qsar {
25 namespace scoring_grid {
26 
27 GridFactory * GridFactory::instance_(0);
28 
30 {
31  //This isn't strictly necessary but I had an absolutely amazing bug involving
32  //The gridfactory being prematurely destroyed and then immediately recreated
33  //This was resulting in data corruption and it would have been 1000x easier to debug
34  //if I had this clear statement here, so I'm leaving it in for the next time.
35  grid_creator_map_.clear();
36 }
37 
39 { }
40 
43 {
44  if(!instance_)
45  {
46  GridFactory * instance_local = new GridFactory;
47  instance_ = instance_local;
48  }
49  return instance_;
50 }
51 
52 
53 ///@brief add a Grid prototype, using it's default type name as the map key
54 void
56 {
57  runtime_assert(creator);
58  std::string grid_type( creator->keyname());
59  //qsarType grid_enum = qsarTypeManager::qsar_type_from_name(grid_type);
60  if(grid_type == "UNDEFINED NAME")
61  {
62  utility_exit_with_message("can't map derived grid with undefined type name.");
63  }
64  if (grid_creator_map_.find(grid_type) != grid_creator_map_.end())
65  {
66  utility_exit_with_message("GridFactory::factory_register already has a move creator with name \"" + grid_type + "\". Conflicting grid names");
67  }
68  grid_creator_map_.insert(std::pair<std::string,GridCreatorOP>(grid_type,creator));
69  grid_creator_map_[grid_type] = creator;
70 }
71 
73 {
74  //string const name = tag->getName();
75  std::string const type = tag->getOption<std::string>("grid_type");
76 
77  GridMap::const_iterator iter(grid_creator_map_.find(type));
78  if( iter != grid_creator_map_.end())
79  {
80  if(!iter->second)
81  {
82  utility_exit_with_message("Error: GridCreatorOP prototype for "+type+ " is NULL!");
83  return NULL;
84  }
85  return iter->second->create_grid(tag);
86  }
87  else
88  {
89  utility_exit_with_message(type + " is not known to the GridFactory. Was it registered via a GridRegistrator in one of the init.cc files");
90  return NULL;
91  }
92 
93 }
94 
95 GridBaseOP GridFactory::new_grid(utility::json_spirit::mObject data ) const
96 {
97  //figure out what kind of grid to make. There will be a "type" tag either in the top level of the
98  //heirarchy or in the base_data level.
99 
100  std::string type;
101 
102  utility::json_spirit::mObject::iterator type_it(data.find("type"));
103  if(type_it != data.end())
104  {
105  //If this is a metagrid then we can find a type tag in the top level
106  type = type_it->second.get_str();
107 
108  }else
109  {
110  //OK, it's not a metagrid, Everything that inherits from SingleGrid has a "base_data" tag, and "type" is under that.
111  utility::json_spirit::mObject base_data(data["base_data"].get_obj());
112  type = base_data["type"].get_str();
113  }
114 
115  //make a new grid
117  GridMap::const_iterator iter(grid_creator_map_.find(type));
118  if( iter != grid_creator_map_.end())
119  {
120  if(!iter->second)
121  {
122  utility_exit_with_message("Error: GridCreatorOP prototype for "+type+ " is NULL!");
123  }
124  new_grid = iter->second->create_grid();
125  }
126  else
127  {
128  utility_exit_with_message(type + " is not known to the GridFactory. Was it registered via a GridRegistrator in one of the init.cc files");
129  }
130 
131  //deserialize object into new grid and return
132  assert(new_grid);
133  new_grid->deserialize(data);
134  return new_grid;
135 }
136 
137 }
138 }
139 }