COPASI API  4.16.103
CCopasiContainer.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2013 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 // Copyright (C) 2002 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 /*!
16  \file CCopasiContainer.cpp
17  \brief Implementation file of class CCopasiContainer
18  */
19 
20 /**
21  * Class CCopasiContainer
22  *
23  * This class is the is used to group CCopasiObjects logically. It inself is
24  * an object. Contained objects are still globally accessible.
25  *
26  * Copyright Stefan Hoops 2002
27  */
28 
29 #include "copasi/copasi.h"
35 
38 
39 const std::vector< CCopasiContainer * > CCopasiContainer::EmptyList;
40 
42  CCopasiObject(),
43  mObjects()
44 {addObjectReference("Name", *const_cast<std::string *>(&getObjectName()));}
45 
46 CCopasiContainer::CCopasiContainer(const std::string & name,
47  const CCopasiContainer * pParent,
48  const std::string & type,
49  const unsigned C_INT32 & flag):
50  CCopasiObject(name, pParent, type, flag | CCopasiObject::Container),
51  mObjects()
52 {addObjectReference("Name", *const_cast<std::string *>(&getObjectName()));}
53 
55  const CCopasiContainer * pParent):
56  CCopasiObject(src, pParent),
57  mObjects()
58 {addObjectReference("Name", *const_cast<std::string *>(&getObjectName()));}
59 
61 {
62  objectMap::iterator it = mObjects.begin();
63  objectMap::iterator end = mObjects.end();
64 
65  for (; it != end; it++)
66  if (it->second != NULL &&
67  it->second->getObjectParent() == this)
68  {
69  it->second->setObjectParent(NULL);
70  pdelete(it->second);
71  }
72 }
73 
75 {
76  if (cn == "")
77  {
78  if (isRoot())
79  return NULL;
80  else
81  return this;
82  }
83 
84  if (cn == "Property=DisplayName")
85  {
86  return CCopasiObject::getObject(cn);
87  }
88 
89  std::string Name = cn.getObjectName();
90  std::string Type = cn.getObjectType();
91 
92  if (getObjectName() == Name && getObjectType() == Type)
93  return getObject(cn.getRemainder());
94 
95  //check if the first part of the cn matches one of the children (by name and type)
96  std::pair< objectMap::const_iterator, objectMap::const_iterator > range =
97  mObjects.equal_range(Name);
98 
99  objectMap::const_iterator it = range.first;
100 
101  while (it != range.second && it->second->getObjectType() != Type) ++it;
102 
103  if (it == range.second) //not found in the list of children
104  {
105  if (Type == "String")
106  return new CCopasiStaticString(Name, this);
107  else if (Type == "Separator")
108  return new CCopasiReportSeparator(Name, this);
109  else
110  return NULL;
111  }
112 
113  const CObjectInterface * pObject = NULL;
114 
115  if (it->second->isNameVector() || it->second->isVector())
116  {
117  if (cn.getElementName(0, false) == "")
118  return it->second;
119 
120  pObject = it->second->getObject("[" + cn.getElementName(0, false) + "]");
121 
122  if (it->second->getObjectType() == "Reference" ||
123  !pObject ||
124  cn.getRemainder() == "")
125  return pObject;
126  else
127  return pObject->getObject(cn.getRemainder());
128  }
129 
130  //handle objects where the array flag is set. Currently this applies to the
131  //CArrayAnnotation object. Since this is also a container, we have to do this
132  //before handling general containers.
133  if (it->second->isArray())
134  {
135  //we need to call the getObject() method of the child array with the
136  //remainder of the cn, with the indices in square brackets, or with an empty string
137 
138  //if there are no indices there could still be a remainder (since the array can also be
139  //a container)
140  if (cn.getElementName(0, false) == "") //no indices
141  return it->second->getObject(cn.getRemainder());
142 
143  //get the indices from the CN
144  std::string indices;
145  std::string tmp;
146  C_INT32 ii = 0;
147 
148  while ((tmp = cn.getElementName(ii, false)) != "")
149  {
150  indices += "[" + tmp + "]";
151  ++ii;
152  }
153 
154  //try to get the array element from the indices
155  pObject = it->second->getObject(indices);
156 
157  //if the element could not be resolved, just return NULL. If there is no
158  //remainder, just return the array element.
159  //In all other cases we call getObject() on the array element with the remainder
160  //of the CN. The special treatment of the empty remainder is probably necessary
161  //since not all implementations of getObject(cn) handle the case of empty CN.
162  if (!pObject)
163  return NULL;
164 
165  if (cn.getRemainder() == "")
166  return pObject;
167  else
168  return pObject->getObject(cn.getRemainder());
169  }
170 
171  //handle generic containers.
172  if (it->second->isContainer())
173  return it->second->getObject(cn.getRemainder());
174 
175  if (it->second->isMatrix())
176  {
177  if (cn.getElementName(0, false) == "")
178  return it->second;
179 
180  pObject = it->second->getObject("[" + cn.getElementName(0, false) + "]" + //TODO really?
181  "[" + cn.getElementName(1, false) + "]");
182 
183  if (it->second->getObjectType() == "Reference" || !pObject)
184  return pObject;
185  else
186  return pObject->getObject(cn.getRemainder());
187  }
188 
189  return it->second->getObject(cn.getRemainder());
190 }
191 
193 {return mObjects;}
194 
196 {
197  void * ptr = getValuePointer();
198 
199  if (ptr == NULL) return NULL;
200 
201  objectMap::const_iterator it = mObjects.begin();
202  objectMap::const_iterator end = mObjects.end();
203 
204  for (; it != end; ++it)
205  if (ptr == it->second->getValuePointer()) return it->second;
206 
207  return NULL;
208 }
209 
211 
213  const bool & adopt)
214 {
215  /* We check whether we are already containing that object. */
216  std::pair< objectMap::iterator, objectMap::iterator > range =
217  mObjects.equal_range(pObject->getObjectName());
218  objectMap::iterator it;
219 
220  for (it = range.first; it != range.second; ++it)
221  if (it->second == pObject) break;
222 
223  if (it != range.second) return false;
224 
225  /* This object is not contained, so we can add it. */
226  mObjects.insert
227  (std::pair<const std::string, CCopasiObject * >(pObject->getObjectName(),
228  pObject));
229 
230  if (adopt) pObject->setObjectParent(this);
231 
232  return true;
233 }
234 
236 {
237  objectMap::iterator it = mObjects.begin();
238  objectMap::iterator end = mObjects.end();
239 
240  /*
241  std::pair< objectMap::iterator, objectMap::iterator > range =
242  mObjects.equal_range(pObject->getObjectName());
243  objectMap::iterator it;
244  */
245  for (; it != end; ++it)
246  if (it->second == pObject) break;
247 
248  if (it == end) return false;
249 
250  mObjects.erase(it);
251 
252  return true;
253 }
254 
255 // virtual
256 std::string CCopasiContainer::getUnits() const
257 {return "";}
258 
259 // virtual
260 std::string CCopasiContainer::getChildObjectUnits(const CCopasiObject * /* pObject */) const
261 {return "";}
virtual bool setObjectParent(const CCopasiContainer *pParent)
bool isRoot() const
#define pdelete(p)
Definition: copasi.h:215
const std::string & getObjectName() const
CCopasiObjectName getRemainder() const
virtual const CObjectInterface * getObject(const CCopasiObjectName &cn) const
virtual const objectMap & getObjects() const
const std::string & getObjectType() const
std::string getObjectType() const
#define C_INT32
Definition: copasi.h:90
std::multimap< std::string, CCopasiObject * > objectMap
virtual std::string getChildObjectUnits(const CCopasiObject *pObject) const
static const std::vector< CCopasiContainer * > EmptyList
virtual const CCopasiObject * getValueObject() const
std::string getElementName(const size_t &pos, const bool &unescape=true) const
virtual bool remove(CCopasiObject *pObject)
virtual ~CCopasiContainer()
Header file of class CCopasiContainer.
long int flag
Definition: f2c.h:52
virtual const CObjectInterface * getObject(const CCopasiObjectName &cn) const =0
virtual bool add(CCopasiObject *pObject, const bool &adopt=true)
virtual std::string getUnits() const
virtual void * getValuePointer() const
virtual const CObjectInterface * getObject(const CCopasiObjectName &cn) const
std::string getObjectName() const
CCopasiObject * addObjectReference(const std::string &name, CType &reference, const unsigned C_INT32 &flag=0)