0022651: Impossible to build OCC as static library due to using Standard_EXPORT inste...
[occt.git] / src / StdLPersistent / StdLPersistent_Data.cxx
CommitLineData
7ed7467d 1// Copyright (c) 2015 OPEN CASCADE SAS
2//
3// This file is part of Open CASCADE Technology software library.
4//
5// This library is free software; you can redistribute it and/or modify it under
6// the terms of the GNU Lesser General Public License version 2.1 as published
7// by the Free Software Foundation, with special exception defined in the file
8// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9// distribution for complete text of the license and disclaimer of any warranty.
10//
11// Alternatively, this file may be used under the terms of Open CASCADE
12// commercial license or contractual agreement.
13
ff205346 14#include <StdLPersistent_Data.hxx>
7ed7467d 15#include <StdObjMgt_ReadData.hxx>
ec964372 16#include <StdObjMgt_WriteData.hxx>
7ed7467d 17
18#include <TDF_Data.hxx>
19#include <TDF_Attribute.hxx>
20
21//! Create a transient label tree from persistent data
ff205346 22class StdLPersistent_Data::Parser
7ed7467d 23{
24public:
25 //! Start parsing a persistent data.
45d8465e 26 Parser (const TColStd_HArray1OfInteger& theLabels,
ff205346 27 const StdLPersistent_HArray1OfPersistent& theAttributes)
28 : myLabelsIter (theLabels)
29 , myAttribIter (theAttributes) {}
7ed7467d 30
31 //! Fill a transient label with data.
32 void FillLabel (TDF_Label theLabel)
33 {
34 Standard_Integer i;
35
ff205346 36 // Read attributes count
7ed7467d 37 myLabelsIter.Next();
38 Standard_Integer anAttribCount = myLabelsIter.Value();
39
40 // Add attributes to the label
ff205346 41 for (i = 0; i < anAttribCount; i++)
7ed7467d 42 {
43 // read persistent attribute
ff205346 44 Handle(StdObjMgt_Persistent)& aPAttrib = myAttribIter.ChangeValue();
7ed7467d 45 myAttribIter.Next();
7ed7467d 46 // create transient attribute and add it to the label
5a1271c8 47 if (aPAttrib) {
48 Handle (TDF_Attribute) anAtt = aPAttrib->CreateAttribute();
49 anAtt->SetID();
50 theLabel.AddAttribute (anAtt);
51 }
7ed7467d 52 }
53
ff205346 54 // Read child labels count
7ed7467d 55 myLabelsIter.Next();
56 Standard_Integer aSubLabelsCount = myLabelsIter.Value();
57
58 // Create child labels
ff205346 59 for (i = 0; i < aSubLabelsCount; i++)
7ed7467d 60 {
61 // read tag of child label
62 myLabelsIter.Next();
63 Standard_Integer aSubLabelTag = myLabelsIter.Value();
64
65 // create and fill child label
66 TDF_Label aSubLabel = theLabel.FindChild (aSubLabelTag, Standard_True);
67 FillLabel (aSubLabel);
68 }
69 }
70
71private:
45d8465e 72 TColStd_HArray1OfInteger ::Iterator myLabelsIter;
ff205346 73 StdLPersistent_HArray1OfPersistent::Iterator myAttribIter;
7ed7467d 74};
75
76//=======================================================================
77//function : Read
78//purpose : Read persistent data from a file
79//=======================================================================
ff205346 80void StdLPersistent_Data::Read (StdObjMgt_ReadData& theReadData)
7ed7467d 81{
82 theReadData >> myVersion >> myLabels >> myAttributes;
83}
84
ec964372 85//=======================================================================
86//function : Write
87//purpose : Write persistent data to a file
88//=======================================================================
89void StdLPersistent_Data::Write (StdObjMgt_WriteData& theWriteData) const
90{
91 theWriteData << myVersion << myLabels << myAttributes;
92}
93
7ed7467d 94//=======================================================================
95//function : Import
96//purpose : Import transient data from the persistent data
97//=======================================================================
ff205346 98Handle(TDF_Data) StdLPersistent_Data::Import() const
7ed7467d 99{
100 if (myLabels.IsNull() || myAttributes.IsNull())
101 return NULL;
102
ff205346 103 // Create tree of labels and add empty transient attributes to them
7ed7467d 104 Handle(TDF_Data) aData = new TDF_Data;
ff205346 105 Parser (*myLabels->Array(), *myAttributes->Array()).FillLabel (aData->Root());
106
107 // Import transient attribuites from persistent data
108 StdLPersistent_HArray1OfPersistent::Iterator anAttribIter (*myAttributes->Array());
109 for (; anAttribIter.More(); anAttribIter.Next())
110 {
111 Handle(StdObjMgt_Persistent)& aPAttrib = anAttribIter.ChangeValue();
112 if (aPAttrib)
113 aPAttrib->ImportAttribute();
114 }
115
7ed7467d 116 return aData;
117}