Persistence.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 // (c) 2007 by National Instruments
3 // Project: GenApi
4 // Author: Eric Gross
5 // $Header$
6 //
7 // License: This file is published under the license of the EMVA GenICam Standard Group.
8 // A text file describing the legal terms is included in your installation as 'GenICam_license.pdf'.
9 // If for some reason you are missing this file please contact the EMVA or visit the website
10 // (http://www.genicam.org) for a full copy.
11 //
12 // THIS SOFTWARE IS PROVIDED BY THE EMVA GENICAM STANDARD GROUP "AS IS"
13 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
14 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
15 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE EMVA GENICAM STANDARD GROUP
16 // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
17 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
18 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
19 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
20 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
21 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22 // POSSIBILITY OF SUCH DAMAGE.
23 //-----------------------------------------------------------------------------
30 #ifndef _GENICAM_PERSISTENCE_H
31 #define _GENICAM_PERSISTENCE_H
32 
33 #include <GenApi/Types.h>
34 #include <GenApi/Pointer.h>
35 #include <GenApi/GenApiDll.h>
36 #include <list>
37 #include <iostream>
38 
39 namespace GENAPI_NAMESPACE
40 {
41 
43  interface GENAPI_DECL_ABSTRACT IPersistScript
44  {
46  virtual void SetInfo(const GENICAM_NAMESPACE::gcstring &Info) = 0;
47 
49  virtual void PersistFeature(IValue& item) = 0;
50  };
51 
54  {
55  public:
57  virtual void SetInfo(const GENICAM_NAMESPACE::gcstring &Info);
58 
60  virtual void PersistFeature(IValue& item);
61 
66 
70  bool LoadFromBag(INodeMap *pNodeMap, bool Verify = true, GENICAM_NAMESPACE::gcstring_vector *pErrorList = NULL);
71 
78  int64_t StoreToBag(INodeMap *pNodeMap, const int MaxNumPersistSkriptEntries = -1, GENICAM_NAMESPACE::gcstring_vector *pFeatureFilter = NULL);
79 
81  bool operator==(const CFeatureBag &FeatureBag) const;
82 
84 
85  virtual void Destroy();
86 
87  virtual const GENICAM_NAMESPACE::gcstring& GetBagName( void ) const;
88  virtual void SetBagName(const GENICAM_NAMESPACE::gcstring& bagName);
89 
91  friend std::istream& operator >>(std::istream &is, CFeatureBag &FeatureBag);
92 
94  friend std::ostream& operator <<(std::ostream &os, const CFeatureBag &FeatureBag);
95 
96  private:
99  GENICAM_NAMESPACE::gcstring_vector m_Names;
100  GENICAM_NAMESPACE::gcstring_vector m_Values;
101 
104 
105  bool LoadFromBagInternal(INodeMap *pNodeMap, bool Verify /* = true */, GENICAM_NAMESPACE::gcstring_vector *pErrorList = NULL);
106  int64_t StoreToBagInternal(INodeMap *pNodeMap, const int MaxNumPersistSkriptEntries = -1, GENICAM_NAMESPACE::gcstring_vector *pFeatureFilter = NULL);
107 
108  friend class CFeatureBagger;
109  };
110 
112  #define GENAPI_PERSISTENCE_MAGIC "{05D8C294-F295-4dfb-9D01-096BD04049F4}"
113 
115  #define GENAPI_PERSISTENCE_MAGIC_FEATUREBAGGER "{4709CB3C-7322-4460-84C3-DA11DDA09939}"
116 
118  // note: this method must be inlined because it uses istream in the parameter list
119  inline std::istream& EatComments(std::istream &is)
120  {
121  if( is.eof() )
122  {
123  return is;
124  }
125 
126  char FirstCharacter = static_cast<char>(is.peek());
127  while( FirstCharacter == '#' )
128  {
129  is.ignore(1024, '\n');
130  FirstCharacter = static_cast<char>(is.peek());
131  }
132  return is;
133  }
134 
135 #ifndef GENAPI_DONT_USE_DEFAULT_PERSISTENCE_FILE_FORMAT
136  // note: this method must be inlined because it uses istream in the parameter list
138  // note: May not be used as inline if called against a library where it is already compiled.
139  inline std::istream& operator >>(std::istream &is, CFeatureBag &FeatureBag)
140  {
141  if( is.eof() )
142  {
143  throw RUNTIME_EXCEPTION("The stream is eof");
144  }
145 
146  FeatureBag.m_Names.clear();
147  FeatureBag.m_Values.clear();
148 
149  const int BufferSize = 1024;
150  char Buffer[BufferSize] = {0};
151 
152  // Check the magic
153  is.getline(Buffer, BufferSize, '\n');
154  GENICAM_NAMESPACE::gcstring FirstLine(Buffer);
156  if( GENICAM_NAMESPACE::gcstring::_npos() == FirstLine.find(MagicGUID) )
157  {
159  if( GENICAM_NAMESPACE::gcstring::_npos() == FirstLine.find(MagicGUID) )
160  {
161  throw RUNTIME_EXCEPTION("The stream is not a GenApi feature stream since it is missing the magic GUID in the first line");
162  }
163  throw RUNTIME_EXCEPTION("The stream has been created using the CFeatureBagger class thus must be restored using the CFeatureBagger class as well");
164  }
165 
166  EatComments( is );
167  char Name[BufferSize] = {0};
168  GENICAM_NAMESPACE::gcstring Value("");
169  while( !is.eof() )
170  {
171  is.getline(Name, BufferSize, '\t');
172  if (is.fail()) // if reading from stream failed -> stop reading!
173  {
174  break;
175  }
176  GENICAM_NAMESPACE::getline(is, Value);
177  if (is.fail()) // if reading from stream failed -> stop reading!
178  {
179  break;
180  }
181  FeatureBag.m_Names.push_back(Name);
182  FeatureBag.m_Values.push_back(Value);
183 
184  Name[0] = '\0';
185  Value = "";
186  EatComments( is );
187  }
188  return is;
189  }
190 
192  // note: this method must be inlined because it uses ostream in the parameter list
193  // note: May not be used as inline if called against a library where it is already compiled.
194  inline std::ostream& operator <<(std::ostream &os, const CFeatureBag &FeatureBag)
195  {
196  os << "# " GENAPI_PERSISTENCE_MAGIC "\n";
197  if( !FeatureBag.m_Info.empty() )
198  {
199  os << "# GenApi persistence file (version " << GENAPI_VERSION_MAJOR << "." << GENAPI_VERSION_MINOR << "." << GENAPI_VERSION_SUBMINOR << ")\n";
200  os << "# " << FeatureBag.m_Info << "\n";
201  }
202 
203  // This can actually never happen the way the code is written right now, but...
204  assert(FeatureBag.m_Names.size() == FeatureBag.m_Values.size());
205 
206  GENICAM_NAMESPACE::gcstring_vector::const_iterator pName = FeatureBag.m_Names.begin();
207  GENICAM_NAMESPACE::gcstring_vector::const_iterator pValue = FeatureBag.m_Values.begin();
208  const GENICAM_NAMESPACE::gcstring_vector::const_iterator endNames = FeatureBag.m_Names.end();
209  for( ; pName != endNames; ++pName, ++pValue )
210  {
211  const GENICAM_NAMESPACE::gcstring Name(*pName);
212  const GENICAM_NAMESPACE::gcstring Value(*pValue);
213  os << Name << "\t" << Value << "\n";
214  }
215 
216  return os;
217  }
218 #endif // #ifndef GENAPI_DONT_USE_DEFAULT_PERSISTENCE_FILE_FORMAT
219 
222  {
223  public:
225  {
226  // Ctor / Dtor
227  // -------------------------------------------------------------------------
228  public:
229  const_iterator(CFeatureBag **ppBag = NULL);
230 
231  // Operators
232  // -------------------------------------------------------------------------
233  public:
234  const CFeatureBag & operator * (void) const;
235  const CFeatureBag * operator -> (void) const;
236  const_iterator & operator ++ (void);
237  const_iterator operator ++ (int);
238  const_iterator & operator += (intptr_t iInc);
239  const_iterator operator + (intptr_t iInc) const;
240  intptr_t operator - (const const_iterator &iter) const;
241  bool operator == (const const_iterator &iter) const;
242  bool operator != (const const_iterator &iter) const;
243 
244  // Member
245  // -------------------------------------------------------------------------
246  protected:
248  };
249  explicit CFeatureBagger();
250  virtual ~CFeatureBagger();
251 
260  size_t Bag(INodeMap *pNodeMap, bool handleDefaultNodeMap = true, bool handleUserSets = false, bool handleSequencerSets = false, const int MaxNumPersistSkriptEntries = -1);
261 
265 
269  bool UnBag(INodeMap *pNodeMap, bool Verify = true, GENICAM_NAMESPACE::gcstring_vector *pErrorList = NULL);
270 
272  virtual void SetInfo(const GENICAM_NAMESPACE::gcstring &Info);
273 
274  // Element access
275  // ---------------------------------------------------------------------------
276  virtual const_iterator begin(void) const;
277  virtual const_iterator end(void) const;
278  virtual const CFeatureBag& at(size_t uiIndex) const;
279  virtual size_t size(void) const;
280 
282  friend std::ostream& operator <<(std::ostream &os, const CFeatureBagger &featureBagger);
283 
285  friend std::istream& operator >>(std::istream &is, CFeatureBagger &featureBagger);
286 
287  private:
288  CFeatureBag& AddBag(const GENICAM_NAMESPACE::gcstring &bagName);
289  void DeleteAllBags( void );
290  template<class _Ty>
291  void UnBagCustomAction(INodeMap *pNodeMap, _Ty setNodePtr, const GENICAM_NAMESPACE::gcstring& setNodeValue, CCommandPtr saveNodePtr );
292  void *m_pBags;
295  };
296 
297 #ifndef GENAPI_DONT_USE_DEFAULT_PERSISTENCE_FILE_FORMAT
298  // note: this method must be inlined because it uses ostream in the parameter list
300  // note: May not be used as inline if called against a library where it is already compiled.
301  inline std::ostream& operator <<(std::ostream &os, const CFeatureBagger &featureBagger)
302  {
304  if( !featureBagger.m_Info.empty() )
305  {
306  os << "# GenApi CFeatureBagger persistence file (version " << GENAPI_VERSION_MAJOR << "." << GENAPI_VERSION_MINOR << "." << GENAPI_VERSION_SUBMINOR << ")\n";
307  os << "# " << featureBagger.m_Info << "\n";
308  }
309 
311  for (it = featureBagger.begin(); it != featureBagger.end(); it++)
312  {
313  os << "[" << (*it).GetBagName() << "]\n";
314  os << (*it);
315  }
316 
317  return os;
318  }
319 
321  // note: this method must be inlined because it uses istream in the parameter list
322  // note: May not be used as inline if called against a library where it is already compiled.
323  inline std::istream& operator >> (std::istream &is, CFeatureBagger &featureBagger)
324  {
325  if (is.eof())
326  {
327  throw RUNTIME_EXCEPTION("The stream is eof");
328  }
329 
330  // Check the magic
331  const int BufferSize = 1024;
332  char Buffer[BufferSize] = { 0 };
333  is.getline(Buffer, BufferSize, '\n');
334  GENICAM_NAMESPACE::gcstring FirstLine(Buffer);
336  bool boCFeatureBagFormatDetected = false;
337  if( GENICAM_NAMESPACE::gcstring::_npos() == FirstLine.find(MagicGUID) )
338  {
340  if( GENICAM_NAMESPACE::gcstring::_npos() == FirstLine.find(MagicGUID) )
341  {
342  throw RUNTIME_EXCEPTION("The stream is not a GenApi feature stream since it is missing the magic GUID in the first line");
343  }
344  boCFeatureBagFormatDetected = true;
345  }
346 
347  std::stringstream currentBagData;
348  if (boCFeatureBagFormatDetected)
349  {
350  currentBagData << FirstLine;
351  }
352  else
353  {
354  EatComments( is );
355  }
356 
357  featureBagger.DeleteAllBags();
358  // Allow to digest the 'CFeatureBag' format using the 'CFeatureBagger' class!
359  CFeatureBag *pBag = boCFeatureBagFormatDetected ? &featureBagger.AddBag("All") : NULL;
360 
361  while (!is.eof())
362  {
364  GENICAM_NAMESPACE::getline(is, line);
365  if (is.fail()) // if reading from stream failed -> stop reading!
366  {
367  break;
368  }
369  if (!line.empty() && (line[0] == '['))
370  {
371  if (!currentBagData.str().empty())
372  {
373  if (pBag)
374  {
375  currentBagData >> (*pBag);
376  }
377  currentBagData.str("");
378  currentBagData.clear();
379  pBag = NULL;
380  }
381  // this is the beginning of a new section
382  const size_t pos = line.find_first_of("]");
383  const GENICAM_NAMESPACE::gcstring bagName(line.substr(1, pos - 1));
384  if( !bagName.empty() )
385  {
386  pBag = &featureBagger.AddBag(bagName);
387  }
388  }
389  else
390  {
391  currentBagData << line << "\n";
392  }
393  }
394  if (!currentBagData.str().empty() && pBag)
395  {
396  currentBagData >> (*pBag);
397  }
398  return is;
399  }
400 #endif // #ifndef GENAPI_DONT_USE_DEFAULT_PERSISTENCE_FILE_FORMAT
401 }
402 
403 #endif //_GENICAM_PERSISTENCE_H
virtual const_iterator begin(void) const
Common types used in the public GenApi interface.
CFeatureBag & AddBag(const GENICAM_NAMESPACE::gcstring &bagName)
bool operator!=(const MyAlloc< T1 > &, const MyAlloc< T2 > &)
Definition: gcmemory.h:103
Bag holding streamable features of a nodetree.
Definition: Persistence.h:53
std::istream & getline(std::istream &is, GENICAM_NAMESPACE::gcstring &str)
STL getline.
Definition: GCString.h:188
static size_t _npos(void)
virtual size_t find_first_of(const gcstring &str, size_t offset=0) const
virtual bool empty(void) const
std::istream & operator>>(std::istream &is, CFeatureBag &FeatureBag)
reads in persistent data from a stream
Definition: Persistence.h:139
GENICAM_NAMESPACE::gcstring m_BagName
The name of the feature bag.
Definition: Persistence.h:98
__int64 int64_t
Definition: config-win32.h:21
GENICAM_NAMESPACE::gcstring m_Info
String describing the node map.
Definition: Persistence.h:294
bool operator==(const MyAlloc< T1 > &, const MyAlloc< T2 > &)
Definition: gcmemory.h:98
interface GENAPI_DECL_ABSTRACT INodeMap
Interface to access the node map.
Definition: INodeMap.h:56
GENICAM_NAMESPACE::gcstring m_Info
String describing the node map.
Definition: Persistence.h:103
virtual size_t find(char ch, size_t offset=0) const
virtual gcstring substr(size_t offset=0, size_t count=GCSTRING_NPOS) const
#define GENAPI_DECL
Definition: GenApiDll.h:55
virtual const_iterator end(void) const
#define GENAPI_PERSISTENCE_MAGIC_FEATUREBAGGER
the magic GUID which indicates that the file is a GenApi stream file created by the CFeatureBagger cl...
Definition: Persistence.h:115
#define RUNTIME_EXCEPTION
Fires a runtime exception, e.g. throw RUNTIME_EXCEPTION("buh!")
Definition: GCException.h:246
#define GENAPI_VERSION_MAJOR
Definition: GenApiVersion.h:38
GENICAM_NAMESPACE::gcstring_vector m_Names
Definition: Persistence.h:99
GENICAM_NAMESPACE::gcstring_vector m_Values
Definition: Persistence.h:100
virtual GENICAM_NAMESPACE::gcstring ToString()=0
Returns a string representation of the digit.
Encapsulates a GenApi pointer dealing with the dynamic_cast automatically.
Definition: Pointer.h:51
std::istream & EatComments(std::istream &is)
Helper function ignoring lines starting with comment character &#39;#&#39;.
Definition: Persistence.h:119
interface GENAPI_DECL_ABSTRACT IValue
Interface for value properties.
Definition: IValue.h:61
interface GENAPI_DECL_ABSTRACT IPersistScript
Basic interface to persist values to.
Definition: Persistence.h:44
interface GENAPI_DECL_ABSTRACT bool Verify
Definition: IBoolean.h:61
#define GENAPI_VERSION_SUBMINOR
Definition: GenApiVersion.h:40
Class use to bag features.
Definition: Persistence.h:221
Definition of template CPointer.
virtual GENICAM_NAMESPACE::gcstring operator*()=0
Get string node value.
A string class which is a clone of std::string.
Definition: GCString.h:52
declspec&#39;s to be used for GenApi Windows dll
std::ostream & operator<<(std::ostream &os, const CFeatureBag &FeatureBag)
writes out persistent data to a stream
Definition: Persistence.h:194
#define GENAPI_VERSION_MINOR
Definition: GenApiVersion.h:39
virtual void PersistFeature(IValue &item)=0
Stores a feature.
Part of the generic device API.
Definition: Autovector.h:48
#define GENAPI_PERSISTENCE_MAGIC
the magic GUID which indicates that the file or buffer is a GenApi stream created by the CFeatureBag ...
Definition: Persistence.h:112


rc_genicam_api
Author(s): Heiko Hirschmueller
autogenerated on Thu Jun 6 2019 19:10:54