0030169: Application Framework - Document format version management improvement
[occt.git] / src / BinMDataStd / BinMDataStd_IntPackedMapDriver.cxx
1 // Created on: 2007-08-01
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 2007-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16
17 #include <BinMDataStd.hxx>
18 #include <BinMDataStd_IntPackedMapDriver.hxx>
19 #include <BinMDF_ADriver.hxx>
20 #include <BinObjMgt_Persistent.hxx>
21 #include <BinObjMgt_RRelocationTable.hxx>
22 #include <BinObjMgt_SRelocationTable.hxx>
23 #include <Message_Messenger.hxx>
24 #include <Standard_Type.hxx>
25 #include <TCollection_ExtendedString.hxx>
26 #include <TColStd_HPackedMapOfInteger.hxx>
27 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
28 #include <TColStd_PackedMapOfInteger.hxx>
29 #include <TDataStd_IntPackedMap.hxx>
30 #include <TDF_Attribute.hxx>
31
32 IMPLEMENT_STANDARD_RTTIEXT(BinMDataStd_IntPackedMapDriver,BinMDF_ADriver)
33
34 //=======================================================================
35 //function : BinMDataStd_IntPackedMapDriver
36 //purpose  :
37 //=======================================================================
38 BinMDataStd_IntPackedMapDriver::BinMDataStd_IntPackedMapDriver
39                          (const Handle(Message_Messenger)& theMessageDriver)
40      : BinMDF_ADriver (theMessageDriver, STANDARD_TYPE(TDataStd_IntPackedMap)->Name())
41 {
42 }
43
44 //=======================================================================
45 //function : NewEmpty
46 //purpose  :
47 //=======================================================================
48
49 Handle(TDF_Attribute) BinMDataStd_IntPackedMapDriver::NewEmpty() const
50 {
51   return new TDataStd_IntPackedMap;
52 }
53
54 //=======================================================================
55 //function : Paste
56 //purpose  : persistent -> transient (retrieve)
57 //=======================================================================
58
59 Standard_Boolean BinMDataStd_IntPackedMapDriver::Paste
60                          (const BinObjMgt_Persistent&  Source,
61                           const Handle(TDF_Attribute)& Target,
62                           BinObjMgt_RRelocationTable&  RelocTable) const
63 {
64   Handle(TDataStd_IntPackedMap) aTagAtt = Handle(TDataStd_IntPackedMap)::DownCast(Target);
65   if(aTagAtt.IsNull()) {
66     myMessageDriver->Send ("IntPackedMapDriver:: The target attribute is Null.", Message_Fail);
67     return Standard_False;
68   }
69
70   Standard_Integer aSize = 0;
71   if (! (Source >> aSize)) {
72     myMessageDriver->Send ("Cannot retrieve size for IntPackedMap attribute.", Message_Fail);
73     return Standard_False;
74   }
75   if(aSize) {
76     Handle(TColStd_HPackedMapOfInteger) aHMap = new TColStd_HPackedMapOfInteger ();
77     Standard_Integer aKey;
78     for(Standard_Integer i = 0; i< aSize; i++) {
79       Standard_Boolean ok = Source >> aKey;
80       if (!ok) {
81         myMessageDriver->Send ("Cannot retrieve integer member for IntPackedMap attribute.", Message_Fail);
82         return Standard_False;
83       }
84       if(!aHMap->ChangeMap().Add( aKey )) return Standard_False;
85     }
86     aTagAtt->ChangeMap(aHMap);
87   }
88
89   Standard_Boolean aDelta(Standard_False);
90   if(RelocTable.GetHeaderData()->StorageVersion().IntegerValue() > 2) {
91     Standard_Byte aDeltaValue;
92     if (! (Source >> aDeltaValue))
93       return Standard_False;
94     else
95       aDelta = (aDeltaValue != 0);
96   }
97   aTagAtt->SetDelta(aDelta);
98   return Standard_True;
99 }
100
101 //=======================================================================
102 //function : Paste
103 //purpose  : transient -> persistent (store)
104 //=======================================================================
105
106 void BinMDataStd_IntPackedMapDriver::Paste
107                          (const Handle(TDF_Attribute)& Source,
108                           BinObjMgt_Persistent&        Target,
109                           BinObjMgt_SRelocationTable&  /*RelocTable*/) const
110 {
111   Handle(TDataStd_IntPackedMap) anAtt = Handle(TDataStd_IntPackedMap)::DownCast(Source);
112   if (anAtt.IsNull()) {
113     myMessageDriver->Send ("IntPackedMapDriver:: The source attribute is Null.", Message_Fail);
114     return;
115   }
116   Standard_Integer aSize = (anAtt->IsEmpty()) ? 0 : anAtt->Extent();
117   Target << aSize;
118   if(aSize) {
119     TColStd_MapIteratorOfPackedMapOfInteger anIt(anAtt->GetMap());
120     for(;anIt.More();anIt.Next())
121       Target << anIt.Key();
122   }
123   Target << (Standard_Byte)(anAtt->GetDelta() ? 1 : 0);
124 }