SCL  1.0
Standard Control Library : Control, dynamics, physics, and simulation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Groups Pages
CMemCopier.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 CMemCopier.hpp
23  *
24  * Created on: Jun 28, 2010
25  *
26  * Copyright (C) 2010, Samir Menon <smenon@stanford.edu>
27  */
28 
29 #ifndef CMEMCOPIER_HPP_
30 #define CMEMCOPIER_HPP_
31 
32 #include <vector>
33 #include <string.h>
34 
35 namespace sutil
36 {
37 
41  template <typename T>
42  class CMemCopier
43  {
44  private:
46  const T* mem_;
47 
49  std::vector<T*> buf_;
50 
53 
54  public:
55  CMemCopier();
56  virtual ~CMemCopier(){}
57 
59  bool init(const T* arg_mem);
60 
64  bool addBuf(T* arg_buf);
65 
70  T* removeBuf(T* arg_buf);
71 
75  bool copy();
76  };
77 
78  template <typename T>
79  CMemCopier<T>::CMemCopier()
80  {
81  mem_ = NULL;
82  initialized_ = false;
83  }
84 
85  template <typename T>
86  bool CMemCopier<T>::init(const T* arg_mem)
87  {
88  if(NULL == arg_mem)
89  { return false; }
90  else
91  {
92  mem_ = arg_mem;
93 
94  //Set init variable
95  if(buf_.size() > 0)
96  { initialized_ = true; }
97 
98  return true;
99  }
100  }
101 
102  template <typename T>
103  bool CMemCopier<T>::addBuf(T* arg_buf)
104  {
105  if(NULL!= arg_buf)
106  {
107  //Check if the buffer is already there.
108  typename std::vector<T*>::iterator it, ite;
109  ite = buf_.end();
110  for(it= buf_.begin(); it!=ite; ++it)
111  {
112  //In case the pointed to memory has been
113  //deallocated.
114  if(arg_buf == (*it))
115  {
116  return false;
117  }
118  }
119 
120  //Add the buffer (new) to the vector
121  buf_.push_back(arg_buf);
122 
123  if(NULL != mem_)
124  { initialized_ = true; }
125 
126  return true;
127  }
128  else
129  { return false; }
130  }
131 
132  template <typename T>
134  {
135  typename std::vector<T*>::iterator it, ite;
136  ite = buf_.end();
137  for(it= buf_.begin(); it!=ite; ++it)
138  {
139  //In case the pointed to memory has been
140  //deallocated.
141  if(arg_buf == (*it))
142  {
143  buf_.erase(it);
144 
145  //Set init variable
146  if(buf_.size() <= 0)
147  { initialized_ = false; }
148 
149  return arg_buf;
150  }
151  }
152  return NULL;
153  }
154 
155  template <typename T>
157  {
158  if(!initialized_)
159  { return false; }
160  else
161  {
162  bool flag = true;
163  typename std::vector<T*>::iterator it, ite;
164  ite = buf_.end();
165  for(it= buf_.begin(); it!=ite; ++it)
166  {
167  //In case the pointed to memory has been
168  //deallocated.
169  if(NULL == (*it))
170  { flag = false; continue; }
171  else
172  { memcpy((void*)(*it),mem_,sizeof(T)); }
173  }
174  return flag;
175  }
176  }
177 
178 }
179 
180 #endif /* CMEMCOPIER_HPP_ */
bool copy()
Definition: CMemCopier.hpp:156
std::vector< T * > buf_
Definition: CMemCopier.hpp:49
Definition: CMemCopier.hpp:42
bool init(const T *arg_mem)
Definition: CMemCopier.hpp:86
const T * mem_
Definition: CMemCopier.hpp:46
T * removeBuf(T *arg_buf)
Definition: CMemCopier.hpp:133
bool initialized_
Definition: CMemCopier.hpp:52
bool addBuf(T *arg_buf)
Definition: CMemCopier.hpp:103