Persistence.h
Go to the documentation of this file.
00001 //-----------------------------------------------------------------------------
00002 //  (c) 2007 by National Instruments
00003 //  Project: GenApi
00004 //  Author:  Eric Gross
00005 //  $Header$
00006 //
00007 //  License: This file is published under the license of the EMVA GenICam  Standard Group.
00008 //  A text file describing the legal terms is included in  your installation as 'GenICam_license.pdf'.
00009 //  If for some reason you are missing  this file please contact the EMVA or visit the website
00010 //  (http://www.genicam.org) for a full copy.
00011 //
00012 //  THIS SOFTWARE IS PROVIDED BY THE EMVA GENICAM STANDARD GROUP "AS IS"
00013 //  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
00014 //  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00015 //  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE EMVA GENICAM STANDARD  GROUP
00016 //  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,  SPECIAL,
00017 //  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT  LIMITED TO,
00018 //  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  DATA, OR PROFITS;
00019 //  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  THEORY OF LIABILITY,
00020 //  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)
00021 //  ARISING IN ANY WAY OUT OF THE USE  OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00022 //  POSSIBILITY OF SUCH DAMAGE.
00023 //-----------------------------------------------------------------------------
00030 #ifndef _GENICAM_PERSISTENCE_H
00031 #define _GENICAM_PERSISTENCE_H
00032 
00033 #include <GenApi/Types.h>
00034 #include <GenApi/Pointer.h>
00035 #include <GenApi/GenApiDll.h>
00036 #include <list>
00037 #include <iostream>
00038 
00039 namespace GENAPI_NAMESPACE
00040 {
00041 
00043     interface GENAPI_DECL_ABSTRACT IPersistScript
00044     {
00046         virtual void SetInfo(const GENICAM_NAMESPACE::gcstring &Info) = 0;
00047 
00049         virtual void PersistFeature(IValue& item) = 0;
00050     };
00051 
00053     class GENAPI_DECL CFeatureBag : public IPersistScript
00054     {
00055     public:
00057         virtual void SetInfo(const GENICAM_NAMESPACE::gcstring &Info);
00058 
00060         virtual void PersistFeature(IValue& item);
00061 
00066 
00070         bool LoadFromBag(INodeMap *pNodeMap, bool Verify = true, GENICAM_NAMESPACE::gcstring_vector *pErrorList = NULL);
00071 
00078         int64_t StoreToBag(INodeMap *pNodeMap, const int MaxNumPersistSkriptEntries = -1, GENICAM_NAMESPACE::gcstring_vector *pFeatureFilter = NULL);
00079 
00081         bool operator==(const CFeatureBag &FeatureBag) const;
00082 
00083         GENICAM_NAMESPACE::gcstring ToString();
00084 
00085         virtual void Destroy();
00086 
00087         virtual const GENICAM_NAMESPACE::gcstring& GetBagName( void ) const;
00088         virtual void SetBagName(const GENICAM_NAMESPACE::gcstring& bagName);
00089 
00091         friend std::istream& operator >>(std::istream &is, CFeatureBag &FeatureBag);
00092 
00094         friend std::ostream& operator <<(std::ostream &os, const CFeatureBag &FeatureBag);
00095 
00096     private:
00098         GENICAM_NAMESPACE::gcstring m_BagName;
00099         GENICAM_NAMESPACE::gcstring_vector m_Names;
00100         GENICAM_NAMESPACE::gcstring_vector m_Values;
00101 
00103         GENICAM_NAMESPACE::gcstring m_Info;
00104 
00105         bool LoadFromBagInternal(INodeMap *pNodeMap, bool Verify /* = true */, GENICAM_NAMESPACE::gcstring_vector *pErrorList = NULL);
00106         int64_t StoreToBagInternal(INodeMap *pNodeMap, const int MaxNumPersistSkriptEntries = -1, GENICAM_NAMESPACE::gcstring_vector *pFeatureFilter = NULL);
00107 
00108         friend class CFeatureBagger;
00109     };
00110 
00112     #define GENAPI_PERSISTENCE_MAGIC "{05D8C294-F295-4dfb-9D01-096BD04049F4}"
00113 
00115     #define GENAPI_PERSISTENCE_MAGIC_FEATUREBAGGER "{4709CB3C-7322-4460-84C3-DA11DDA09939}"
00116 
00118     // note: this method must be inlined because it uses istream in the parameter list
00119     inline std::istream& EatComments(std::istream &is)
00120     {
00121         if( is.eof() )
00122         {
00123             return is;
00124         }
00125 
00126         char FirstCharacter = static_cast<char>(is.peek());
00127         while( FirstCharacter == '#' )
00128         {
00129             is.ignore(1024, '\n');
00130             FirstCharacter = static_cast<char>(is.peek());
00131         }
00132         return is;
00133     }
00134 
00135 #ifndef GENAPI_DONT_USE_DEFAULT_PERSISTENCE_FILE_FORMAT
00136 
00137     // note: this method must be inlined because it uses istream in the parameter list
00138     // note: May not be used as inline if called against a library where it is already compiled.
00139     inline std::istream& operator >>(std::istream &is, CFeatureBag &FeatureBag)
00140     {
00141         if( is.eof() )
00142         {
00143             throw RUNTIME_EXCEPTION("The stream is eof");
00144         }
00145 
00146         FeatureBag.m_Names.clear();
00147         FeatureBag.m_Values.clear();
00148 
00149         const int BufferSize = 1024;
00150         char Buffer[BufferSize] = {0};
00151 
00152         // Check the magic
00153         is.getline(Buffer, BufferSize, '\n');
00154         GENICAM_NAMESPACE::gcstring FirstLine(Buffer);
00155         GENICAM_NAMESPACE::gcstring MagicGUID(GENAPI_PERSISTENCE_MAGIC);
00156         if( GENICAM_NAMESPACE::gcstring::_npos() == FirstLine.find(MagicGUID) )
00157         {
00158             MagicGUID = GENICAM_NAMESPACE::gcstring(GENAPI_PERSISTENCE_MAGIC_FEATUREBAGGER);
00159             if( GENICAM_NAMESPACE::gcstring::_npos() == FirstLine.find(MagicGUID) )
00160             {
00161                 throw RUNTIME_EXCEPTION("The stream is not a GenApi feature stream since it is missing the magic GUID in the first line");
00162             }
00163             throw RUNTIME_EXCEPTION("The stream has been created using the CFeatureBagger class thus must be restored using the CFeatureBagger class as well");
00164         }
00165 
00166         EatComments( is );
00167         char Name[BufferSize] = {0};
00168         GENICAM_NAMESPACE::gcstring Value("");
00169         while( !is.eof() )
00170         {
00171             is.getline(Name, BufferSize, '\t');
00172             if (is.fail())  // if reading from stream failed -> stop reading!
00173             {
00174                 break;
00175             }
00176             GENICAM_NAMESPACE::getline(is, Value);
00177             if (is.fail())  // if reading from stream failed -> stop reading!
00178             {
00179                 break;
00180             }
00181             FeatureBag.m_Names.push_back(Name);
00182             FeatureBag.m_Values.push_back(Value);
00183 
00184             Name[0] = '\0';
00185             Value = "";
00186             EatComments( is );
00187         }
00188         return is;
00189     }
00190 
00192     // note: this method must be inlined because it uses ostream in the parameter list
00193     // note: May not be used as inline if called against a library where it is already compiled.
00194     inline std::ostream& operator <<(std::ostream &os, const CFeatureBag &FeatureBag)
00195     {
00196         os << "# " GENAPI_PERSISTENCE_MAGIC "\n";
00197         if( !FeatureBag.m_Info.empty() )
00198         {
00199             os << "# GenApi persistence file (version " << GENAPI_VERSION_MAJOR << "." << GENAPI_VERSION_MINOR << "." << GENAPI_VERSION_SUBMINOR << ")\n";
00200             os << "# " << FeatureBag.m_Info << "\n";
00201         }
00202 
00203         // This can actually never happen the way the code is written right now, but...
00204         assert(FeatureBag.m_Names.size() == FeatureBag.m_Values.size());
00205 
00206         GENICAM_NAMESPACE::gcstring_vector::const_iterator pName = FeatureBag.m_Names.begin();
00207         GENICAM_NAMESPACE::gcstring_vector::const_iterator pValue = FeatureBag.m_Values.begin();
00208         const GENICAM_NAMESPACE::gcstring_vector::const_iterator endNames = FeatureBag.m_Names.end();
00209         for( ; pName != endNames; ++pName, ++pValue )
00210         {
00211             const GENICAM_NAMESPACE::gcstring Name(*pName);
00212             const GENICAM_NAMESPACE::gcstring Value(*pValue);
00213             os << Name << "\t" << Value << "\n";
00214         }
00215 
00216         return os;
00217     }
00218 #endif // #ifndef GENAPI_DONT_USE_DEFAULT_PERSISTENCE_FILE_FORMAT
00219 
00221     class GENAPI_DECL CFeatureBagger
00222     {
00223     public:
00224         class GENAPI_DECL const_iterator
00225         {
00226             // Ctor / Dtor
00227             // -------------------------------------------------------------------------
00228         public:
00229             const_iterator(CFeatureBag **ppBag = NULL);
00230 
00231             // Operators
00232             // -------------------------------------------------------------------------
00233         public:
00234             const CFeatureBag &     operator *        (void)                  const;
00235             const CFeatureBag *     operator ->       (void)                  const;
00236             const_iterator &        operator ++       (void);
00237             const_iterator          operator ++       (int);
00238             const_iterator &        operator +=       (intptr_t iInc);
00239             const_iterator          operator +        (intptr_t iInc)         const;
00240             intptr_t                operator -        (const const_iterator &iter) const;
00241             bool                    operator ==       (const const_iterator &iter) const;
00242             bool                    operator !=       (const const_iterator &iter) const;
00243 
00244             // Member
00245             // -------------------------------------------------------------------------
00246         protected:
00247             CFeatureBag **_ppb;
00248         };
00249         explicit CFeatureBagger();
00250         virtual ~CFeatureBagger();
00251 
00260         size_t Bag(INodeMap *pNodeMap, bool handleDefaultNodeMap = true, bool handleUserSets = false, bool handleSequencerSets = false, const int MaxNumPersistSkriptEntries = -1);
00261 
00265 
00269         bool UnBag(INodeMap *pNodeMap, bool Verify = true, GENICAM_NAMESPACE::gcstring_vector *pErrorList = NULL);
00270 
00272         virtual void SetInfo(const GENICAM_NAMESPACE::gcstring &Info);
00273 
00274         // Element access
00275         // ---------------------------------------------------------------------------
00276         virtual const_iterator      begin(void)                 const;
00277         virtual const_iterator      end(void)                   const;
00278         virtual const CFeatureBag&  at(size_t uiIndex)          const;
00279         virtual size_t              size(void)                  const;
00280 
00282         friend std::ostream& operator <<(std::ostream &os, const CFeatureBagger &featureBagger);
00283 
00285         friend std::istream& operator >>(std::istream &is, CFeatureBagger &featureBagger);
00286 
00287     private:
00288         CFeatureBag& AddBag(const GENICAM_NAMESPACE::gcstring &bagName);
00289         void DeleteAllBags( void );
00290         template<class _Ty>
00291         void UnBagCustomAction(INodeMap *pNodeMap, _Ty setNodePtr, const GENICAM_NAMESPACE::gcstring& setNodeValue, CCommandPtr saveNodePtr );
00292         void *m_pBags;
00294         GENICAM_NAMESPACE::gcstring m_Info;
00295     };
00296 
00297 #ifndef GENAPI_DONT_USE_DEFAULT_PERSISTENCE_FILE_FORMAT
00298 
00299     // note: this method must be inlined because it uses ostream in the parameter list
00300     // note: May not be used as inline if called against a library where it is already compiled.
00301     inline std::ostream& operator <<(std::ostream &os, const CFeatureBagger &featureBagger)
00302     {
00303         os << "# " GENAPI_PERSISTENCE_MAGIC_FEATUREBAGGER "\n";
00304         if( !featureBagger.m_Info.empty() )
00305         {
00306             os << "# GenApi CFeatureBagger persistence file (version " << GENAPI_VERSION_MAJOR << "." << GENAPI_VERSION_MINOR << "." << GENAPI_VERSION_SUBMINOR << ")\n";
00307             os << "# " << featureBagger.m_Info << "\n";
00308         }
00309 
00310         CFeatureBagger::const_iterator it;
00311         for (it = featureBagger.begin(); it != featureBagger.end(); it++)
00312         {
00313             os << "[" << (*it).GetBagName() << "]\n";
00314             os << (*it);
00315         }
00316 
00317         return os;
00318     }
00319 
00321     // note: this method must be inlined because it uses istream in the parameter list
00322     // note: May not be used as inline if called against a library where it is already compiled.
00323     inline std::istream& operator >> (std::istream &is, CFeatureBagger &featureBagger)
00324     {
00325         if (is.eof())
00326         {
00327             throw RUNTIME_EXCEPTION("The stream is eof");
00328         }
00329 
00330         // Check the magic
00331         const int BufferSize = 1024;
00332         char Buffer[BufferSize] = { 0 };
00333         is.getline(Buffer, BufferSize, '\n');
00334         GENICAM_NAMESPACE::gcstring FirstLine(Buffer);
00335         GENICAM_NAMESPACE::gcstring MagicGUID(GENAPI_PERSISTENCE_MAGIC_FEATUREBAGGER);
00336         bool boCFeatureBagFormatDetected = false;
00337         if( GENICAM_NAMESPACE::gcstring::_npos() == FirstLine.find(MagicGUID) )
00338         {
00339             MagicGUID = GENICAM_NAMESPACE::gcstring(GENAPI_PERSISTENCE_MAGIC);
00340             if( GENICAM_NAMESPACE::gcstring::_npos() == FirstLine.find(MagicGUID) )
00341             {
00342                 throw RUNTIME_EXCEPTION("The stream is not a GenApi feature stream since it is missing the magic GUID in the first line");
00343             }
00344             boCFeatureBagFormatDetected = true;
00345         }
00346 
00347         std::stringstream currentBagData;
00348         if (boCFeatureBagFormatDetected)
00349         {
00350             currentBagData << FirstLine;
00351         }
00352         else
00353         {
00354             EatComments( is );
00355         }
00356 
00357         featureBagger.DeleteAllBags();
00358         // Allow to digest the 'CFeatureBag' format using the 'CFeatureBagger' class!
00359         CFeatureBag *pBag = boCFeatureBagFormatDetected ? &featureBagger.AddBag("All") : NULL;
00360 
00361         while (!is.eof())
00362         {
00363             GENICAM_NAMESPACE::gcstring line;
00364             GENICAM_NAMESPACE::getline(is, line);
00365             if (is.fail())  // if reading from stream failed -> stop reading!
00366             {
00367                 break;
00368             }
00369             if (!line.empty() && (line[0] == '['))
00370             {
00371                 if (!currentBagData.str().empty())
00372                 {
00373                     if (pBag)
00374                     {
00375                         currentBagData >> (*pBag);
00376                     }
00377                     currentBagData.str("");
00378                     currentBagData.clear();
00379                     pBag = NULL;
00380                 }
00381                 // this is the beginning of a new section
00382                 const size_t pos = line.find_first_of("]");
00383                 const GENICAM_NAMESPACE::gcstring bagName(line.substr(1, pos - 1));
00384                 if( !bagName.empty() )
00385                 {
00386                     pBag = &featureBagger.AddBag(bagName);
00387                 }
00388             }
00389             else
00390             {
00391                 currentBagData << line << "\n";
00392             }
00393         }
00394         if (!currentBagData.str().empty() && pBag)
00395         {
00396             currentBagData >> (*pBag);
00397         }
00398         return is;
00399     }
00400 #endif // #ifndef GENAPI_DONT_USE_DEFAULT_PERSISTENCE_FILE_FORMAT
00401 }
00402 
00403 #endif //_GENICAM_PERSISTENCE_H


rc_genicam_api
Author(s): Heiko Hirschmueller
autogenerated on Thu Jun 6 2019 18:42:47