Persistence.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. //-----------------------------------------------------------------------------
  24. /*!
  25. \file
  26. \brief Definition of interface IPersistScript and class CFeatureBag
  27. \ingroup GenApi_PublicUtilities
  28. */
  29. #ifndef _GENICAM_PERSISTENCE_H
  30. #define _GENICAM_PERSISTENCE_H
  31. #include <GenApi/Types.h>
  32. #include <GenApi/Pointer.h>
  33. #include <GenApi/GenApiDll.h>
  34. #include <list>
  35. #include <iostream>
  36. using namespace GENICAM_NAMESPACE;
  37. namespace GENAPI_NAMESPACE
  38. {
  39. //! Basic interface to persist values to
  40. interface GENAPI_DECL_ABSTRACT IPersistScript
  41. {
  42. //! sets information about the node map
  43. virtual void SetInfo(gcstring &Info) = 0;
  44. //! Stores a feature
  45. virtual void PersistFeature(IValue& item) = 0;
  46. };
  47. //! Bag holding streamable features of a nodetree
  48. class GENAPI_DECL CFeatureBag : public IPersistScript
  49. {
  50. public:
  51. //! sets information about the node map
  52. virtual void SetInfo(gcstring &Info);
  53. //! Stores a feature
  54. virtual void PersistFeature(IValue& item);
  55. //! Loads the features from the bag to the node tree
  56. //! \param pNodeMap The node map
  57. //! \param Verify If true, all streamable features are read back
  58. //! \param pErrorList If an error occurs during loading the error message is stored in the list and the loading continues
  59. /*! For Verify=true the list of names in the feature bag is replayed again.
  60. If a node is a selector it's value is set to the value from the feature bag
  61. If not the value is read from the camera and compared with the value from the feature bag.
  62. */
  63. bool LoadFromBag(INodeMap *pNodeMap, bool Verify = true, gcstring_vector *pErrorList = NULL);
  64. /*! \brief Stores the streamable nodes to this feature bag
  65. \param pNodeMap The node map to persist
  66. \param MaxNumPersistSkriptEntries The max number of entries in the container; -1 means unlimited
  67. \return number of entries in the bag
  68. */
  69. int64_t StoreToBag(INodeMap *pNodeMap, const int MaxNumPersistSkriptEntries = -1 );
  70. //! compares the content of two feature bags
  71. bool operator==(const CFeatureBag &FeatureBag) const;
  72. gcstring ToString();
  73. //! fills the bag from a stream
  74. friend std::istream& operator >>(std::istream &is, CFeatureBag &FeatureBag);
  75. //! puts the bag into a stream
  76. friend std::ostream& operator <<(std::ostream &os, const CFeatureBag &FeatureBag);
  77. private:
  78. //! The features are stored in a list of string pairs = {Name, Value}
  79. gcstring_vector m_Names;
  80. //! The features are stored in a list of string pairs = {Name, Value}
  81. gcstring_vector m_Values;
  82. //! String describing the node map
  83. gcstring m_Info;
  84. };
  85. //! the magic GUID which indicates that the file is a GenApi stream file Must be the first entry
  86. #define GENAPI_PERSISTENCE_MAGIC "{05D8C294-F295-4dfb-9D01-096BD04049F4}"
  87. //! Helper function ignoring lines starting with comment character '#'
  88. // note: this method must be inlined because it uses istream in the parameter list
  89. inline std::istream& EatComments(std::istream &is)
  90. {
  91. if( is.eof() )
  92. return is;
  93. char FirstCharacter;
  94. FirstCharacter = (char)is.peek();
  95. while( FirstCharacter == '#' )
  96. {
  97. is.ignore(1024, '\n');
  98. FirstCharacter = (char)is.peek();
  99. }
  100. return is;
  101. }
  102. //! reads in persistent data from a stream
  103. // note: this method must be inlined because it uses istream in the parameter list
  104. // note: May not be used as inline if called against a library where it is already compiled.
  105. inline std::istream& operator >>(std::istream &is, CFeatureBag &FeatureBag)
  106. {
  107. if( is.eof() )
  108. throw RUNTIME_EXCEPTION("The stream is eof");
  109. FeatureBag.m_Names.clear();
  110. FeatureBag.m_Values.clear();
  111. const int BufferSize = 1024;
  112. char Buffer[BufferSize] = {0};
  113. char Name[BufferSize] = {0};
  114. gcstring Value("");
  115. // Check the magic
  116. is.getline(Buffer, BufferSize, '\n');
  117. gcstring FirstLine(Buffer);
  118. gcstring MagicGUID(GENAPI_PERSISTENCE_MAGIC);
  119. if( gcstring::_npos() == FirstLine.find(MagicGUID) )
  120. throw RUNTIME_EXCEPTION("The stream is not a GenApi feature stream since it is missing the magic GUID in the first line");
  121. EatComments( is );
  122. while( !is.eof() )
  123. {
  124. is.getline(Name, BufferSize, '\t');
  125. if (is.fail()) // if reading from stream failed -> stop reading!
  126. break;
  127. GENICAM_NAMESPACE::getline(is, Value);
  128. if (is.fail()) // if reading from stream failed -> stop reading!
  129. break;
  130. FeatureBag.m_Names.push_back(Name);
  131. FeatureBag.m_Values.push_back(Value);
  132. Name[0] = '\0';
  133. Value = "";
  134. EatComments( is );
  135. }
  136. return is;
  137. }
  138. //! writes out persistent data to a stream
  139. // note: this method must be inlined because it uses ostream in the parameter list
  140. // note: May not be used as inline if called against a library where it is already compiled.
  141. inline std::ostream& operator <<(std::ostream &os, const CFeatureBag &FeatureBag)
  142. {
  143. os << "# " GENAPI_PERSISTENCE_MAGIC "\n";
  144. os << "# GenApi persistence file (version " << GENAPI_VERSION_MAJOR << "." << GENAPI_VERSION_MINOR << "." << GENAPI_VERSION_SUBMINOR << ")\n";
  145. os << "# " << FeatureBag.m_Info << "\n";
  146. gcstring_vector::const_iterator pName = FeatureBag.m_Names.begin();
  147. gcstring_vector::const_iterator pValue = FeatureBag.m_Values.begin();
  148. assert(FeatureBag.m_Names.size() == FeatureBag.m_Values.size());
  149. if (FeatureBag.m_Names.size() == FeatureBag.m_Values.size())
  150. {
  151. const gcstring_vector::const_iterator endNames = FeatureBag.m_Names.end();
  152. const gcstring_vector::const_iterator endValues = FeatureBag.m_Values.end();
  153. for ( /**/; pName != endNames; ++pName, ++pValue)
  154. {
  155. gcstring Name(*pName);
  156. gcstring Value(*pValue);
  157. os << Name << "\t" << Value << "\n";
  158. }
  159. }
  160. return os;
  161. }
  162. }
  163. #endif //_GENICAM_PERSISTENCE_H