Chaste  Build::
ParameterCollection.cpp
1 /*
2 Copyright (c) 2005-2016, University of Oxford.
3 All rights reserved.
4 
5 University of Oxford means the Chancellor, Masters and Scholars of the
6 University of Oxford, having an administrative office at Wellington
7 Square, Oxford OX1 2JD, UK.
8 
9 This file is part of Chaste.
10 
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions are met:
13  * Redistributions of source code must retain the above copyright notice,
14  this list of conditions and the following disclaimer.
15  * Redistributions in binary form must reproduce the above copyright notice,
16  this list of conditions and the following disclaimer in the documentation
17  and/or other materials provided with the distribution.
18  * Neither the name of the University of Oxford nor the names of its
19  contributors may be used to endorse or promote products derived from this
20  software without specific prior written permission.
21 
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
28 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 */
34 
35 #include <cassert>
36 #include <cmath>
37 #include <iostream>
38 #include <fstream>
39 #include "PetscTools.hpp"
40 
41 #include "ParameterCollection.hpp"
42 
44 boost::shared_ptr<ParameterCollection> ParameterCollection::mpInstance = boost::shared_ptr<ParameterCollection>();
45 
47 {
48  if (!mpInstance)
49  {
50  mpInstance = boost::shared_ptr<ParameterCollection>(new ParameterCollection);
51  std::atexit(Destroy);
52  }
53  return mpInstance.get();
54 }
55 
56 boost::shared_ptr<ParameterCollection> ParameterCollection::SharedInstance()
57 {
58  if (!mpInstance)
59  {
60  mpInstance = boost::shared_ptr<ParameterCollection>(new ParameterCollection);
61  std::atexit(Destroy);
62  }
63  return mpInstance;
64 }
65 
67  : mParameters()
68 {
69  // Make sure there's only one instance - enforces correct serialization
70  assert(!mpInstance);
71 
72 }
73 
74 void ParameterCollection::DumpToFile(const std::string& rFilename)
75 {
76  if(PetscTools::AmMaster())
77  {
78  std::ofstream myfile;
79  myfile.open(rFilename.c_str());
80  myfile << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
81  myfile << "<parameter_collection>" << std::endl;
82 
83  typedef std::map<std::string, std::pair<std::string, boost::shared_ptr<BaseParameterInstance> > >::iterator it_type;
84  for(it_type iterator = mParameters.begin(); iterator != mParameters.end(); iterator++)
85  {
86  myfile << "<parameter>" << std::endl;
87  myfile << "<added_by>" << (iterator->second).first << "</added_by>"<< std::endl;
88  myfile << (iterator->second).second;
89  myfile << "</parameter>" << std::endl;
90  }
91  myfile << "</parameter_collection>" << std::endl;
92  myfile.close();
93  }
94 }
95 
96 boost::shared_ptr<BaseParameterInstance> ParameterCollection::GetParameter(const std::string& rName)
97 {
98  return mParameters[rName].second;
99 }
100 
101 void ParameterCollection::AddParameter(boost::shared_ptr<BaseParameterInstance> pParameter, const std::string& rFirstInstantiated)
102 {
103  // Check if the parameter already exists in the map
104  std::map<std::string, std::pair<std::string, boost::shared_ptr<BaseParameterInstance> > >::iterator it = mParameters.find(pParameter->GetName());
105  if(it != mParameters.end() && rFirstInstantiated != it->first)
106  {
107  it->second = std::pair<std::string, boost::shared_ptr<BaseParameterInstance> >(rFirstInstantiated, pParameter);
108  }
109  else
110  {
111  mParameters[pParameter->GetName()] = std::pair<std::string, boost::shared_ptr<BaseParameterInstance> >(rFirstInstantiated, pParameter);
112  }
113 }
114 
116 {
117  if (mpInstance)
118  {
119  mpInstance = boost::shared_ptr<ParameterCollection>();
120  }
121 }
122 
static boost::shared_ptr< ParameterCollection > SharedInstance()
static boost::shared_ptr< ParameterCollection > mpInstance
std::map< std::string, std::pair< std::string, boost::shared_ptr< BaseParameterInstance > > > mParameters
boost::shared_ptr< BaseParameterInstance > GetParameter(const std::string &rName)
void AddParameter(boost::shared_ptr< BaseParameterInstance > pParameter, const std::string &rFirstInstantiated)
void DumpToFile(const std::string &rFilename)
static ParameterCollection * Instance()