SCL  1.0
Standard Control Library : Control, dynamics, physics, and simulation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Groups Pages
CObjectHistory.hpp
1 /* This file is part of sutil, a random collection of utilities.
2 
3 sutil is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License as published by the Free Software Foundation; either
6 version 3 of the License, or (at your option) any later version.
7 
8 Alternatively, you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
12 
13 sutil is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public
19 License and a copy of the GNU General Public License along with
20 sutil. If not, see <http://www.gnu.org/licenses/>.
21  */
22 /* \file CObjectHistory.hpp
23  *
24  * Created on: Nov 18, 2011
25  *
26  * Copyright (C) 2011, Samir Menon <smenon@stanford.edu>
27  */
28 
29 #ifndef COBJECTHISTORY_HPP_
30 #define COBJECTHISTORY_HPP_
31 
32 #include <sutil/CMappedList.hpp>
33 #include <sutil/CSystemClock.hpp>
34 
35 #ifdef DEBUG
36 #include <iostream>
37 #endif
38 
39 namespace sutil
40 {
65  template<typename Idx, typename StoreObjectsAs /* = Idx //works best */>
67  {
68  protected:
73  Idx,
77  > data_;
78 
79  public:
81  bool saveObject(const Idx& arg_idx, const StoreObjectsAs& arg_obj)
82  {
83  bool flag;
84  CMappedList<double, StoreObjectsAs>* objl = data_.at(arg_idx);
85  if(NULL == objl)
86  {
87  flag = data_.create(arg_idx);
88  if(false == flag)
89  {
90 #ifdef DEBUG
91  std::cerr<<"\nCObjectHistory::addObject() : Could not create an entry for this object";
92 #endif
93  return false;
94  }
95  objl = data_.at(arg_idx);
96  if(NULL == objl)
97  {
98 #ifdef DEBUG
99  std::cerr<<"\nCObjectHistory::addObject() : Created an entry for this object but can't access it. Invalid state.";
100 #endif
101  return false;
102  }
103  }
104 
105  flag = objl->create(CSystemClock::getSysTime(), arg_obj);//Stores objects at the start
106  if(false == flag)
107  {
108 #ifdef DEBUG
109  std::cerr<<"\nCObjectHistory::addObject() : Could not timestamp and store this object";
110 #endif
111  return false;
112  }
113 
114  return true;
115  }
116 
119  getObjectTimeSeries(const Idx& arg_idx)
120  { return data_.at(arg_idx); }
121 
123  const StoreObjectsAs* getObject(const Idx& arg_idx,
125  double arg_time = -1.0)
126  {
127  CMappedList<double, StoreObjectsAs>* objl = data_.at(arg_idx);
128  if(NULL == objl)
129  {
130 #ifdef DEBUG
131  std::cerr<<"\nCObjectHistory::getObject() : Can't find object in history.";
132 #endif
133  return NULL;
134  }
135  //Now check for the time
136  if(arg_time > 0.0)
137  { return objl->at(arg_time); }
138  else
139  { return objl->at( std::size_t(0) ); }
140  }
141 
144  bool removeObjectTimeSeries(const Idx& arg_idx)
145  { return data_.erase(arg_idx); }
146 
148  bool removeObject(const Idx& arg_idx,
150  double arg_time = -1)
151  {
152  CMappedList<double, StoreObjectsAs>* objl = data_.at(arg_idx);
153  if(NULL == objl)
154  {
155 #ifdef DEBUG
156  std::cerr<<"\nCObjectHistory::removeObject() : Can't find object in history.";
157 #endif
158  return NULL;
159  }
160  //Now check for the time
161  if(arg_time > 0.0)
162  { return objl->erase(arg_time); }
163  else
164  { return objl->erase( objl->at(std::size_t(0)) ); }
165  }
166 
169 
172  };
173 
174 } /* namespace sutil */
175 #endif /* COBJECTHISTORY_HPP_ */
bool removeObjectTimeSeries(const Idx &arg_idx)
Definition: CObjectHistory.hpp:144
bool saveObject(const Idx &arg_idx, const StoreObjectsAs &arg_obj)
Definition: CObjectHistory.hpp:81
const StoreObjectsAs * getObject(const Idx &arg_idx, double arg_time=-1.0)
Definition: CObjectHistory.hpp:123
Definition: CMappedList.hpp:85
Definition: CObjectHistory.hpp:66
CMappedList< Idx, CMappedList< double, StoreObjectsAs > > data_
Definition: CObjectHistory.hpp:77
virtual T * at(const std::size_t arg_idx)
Definition: CMappedList.hpp:832
virtual bool erase(T *arg_t)
Definition: CMappedList.hpp:985
const CMappedList< double, StoreObjectsAs > * getObjectTimeSeries(const Idx &arg_idx)
Definition: CObjectHistory.hpp:119
CObjectHistory()
Definition: CObjectHistory.hpp:168
bool removeObject(const Idx &arg_idx, double arg_time=-1)
Definition: CObjectHistory.hpp:148
virtual T * create(const Idx &arg_idx, const bool insert_at_start=true)
Definition: CMappedList.hpp:714
~CObjectHistory()
Definition: CObjectHistory.hpp:171
static suClock getSysTime()
Definition: CSystemClock.hpp:95