0029220: Application Framework - replace CDM_MessageDriver interface by Message_Messe...
[occt.git] / src / BinMDataStd / BinMDataStd_ByteArrayDriver.cxx
1 // Created on: 2007-05-29
2 // Created by: Vlad Romashko
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_ByteArrayDriver.hxx>
19 #include <BinObjMgt_Persistent.hxx>
20 #include <Message_Messenger.hxx>
21 #include <Standard_Type.hxx>
22 #include <TColStd_Array1OfInteger.hxx>
23 #include <TColStd_HArray1OfByte.hxx>
24 #include <TDataStd_ByteArray.hxx>
25 #include <TDF_Attribute.hxx>
26
27 IMPLEMENT_STANDARD_RTTIEXT(BinMDataStd_ByteArrayDriver,BinMDF_ADriver)
28
29 //=======================================================================
30 //function : BinMDataStd_ByteArrayDriver
31 //purpose  : Constructor
32 //=======================================================================
33 BinMDataStd_ByteArrayDriver::BinMDataStd_ByteArrayDriver(const Handle(Message_Messenger)& theMsgDriver)
34      : BinMDF_ADriver (theMsgDriver, STANDARD_TYPE(TDataStd_ByteArray)->Name())
35 {
36
37 }
38
39 //=======================================================================
40 //function : NewEmpty
41 //purpose  : 
42 //=======================================================================
43 Handle(TDF_Attribute) BinMDataStd_ByteArrayDriver::NewEmpty() const
44 {
45   return new TDataStd_ByteArray();
46 }
47
48 //=======================================================================
49 //function : Paste
50 //purpose  : persistent -> transient (retrieve)
51 //=======================================================================
52 Standard_Boolean BinMDataStd_ByteArrayDriver::Paste(const BinObjMgt_Persistent&  theSource,
53                                                     const Handle(TDF_Attribute)& theTarget,
54                                                     BinObjMgt_RRelocationTable&  ) const
55 {
56   Standard_Integer aFirstInd, aLastInd;
57   if (! (theSource >> aFirstInd >> aLastInd))
58     return Standard_False;
59   if (aLastInd < aFirstInd)
60     return Standard_False;
61
62   TColStd_Array1OfByte aTargetArray(aFirstInd, aLastInd);
63   theSource.GetByteArray (&aTargetArray(aFirstInd), aTargetArray.Length());
64
65   const Handle(TDataStd_ByteArray) anAtt = Handle(TDataStd_ByteArray)::DownCast(theTarget);
66   Handle(TColStd_HArray1OfByte) bytes = new TColStd_HArray1OfByte(aFirstInd, aLastInd);
67   for (Standard_Integer i = aFirstInd; i <= aLastInd; i++)
68   {
69     bytes->SetValue(i, aTargetArray.Value(i));
70   }
71   anAtt->ChangeArray(bytes);
72
73   Standard_Boolean aDelta(Standard_False); 
74   if(BinMDataStd::DocumentVersion() > 2) {
75     Standard_Byte aDeltaValue;
76     if (! (theSource >> aDeltaValue))
77       return Standard_False;
78     else
79       aDelta = (aDeltaValue != 0);
80   }
81   anAtt->SetDelta(aDelta);
82
83   BinMDataStd::SetAttributeID(theSource, anAtt);
84   return Standard_True;
85 }
86
87 //=======================================================================
88 //function : Paste
89 //purpose  : transient -> persistent (store)
90 //=======================================================================
91 void BinMDataStd_ByteArrayDriver::Paste(const Handle(TDF_Attribute)& theSource,
92                                         BinObjMgt_Persistent&        theTarget,
93                                         BinObjMgt_SRelocationTable&  ) const
94 {
95   Handle(TDataStd_ByteArray) anAtt = Handle(TDataStd_ByteArray)::DownCast(theSource);
96   const Standard_Integer aFirstInd = anAtt->Lower();
97   const Standard_Integer aLastInd  = anAtt->Upper();
98   if (aLastInd < aFirstInd)
99     return;
100   theTarget << aFirstInd << aLastInd;
101
102   const Handle(TColStd_HArray1OfByte)& bytes = anAtt->InternalArray();
103   Standard_Integer lower = bytes->Lower(), i = lower, upper = bytes->Upper();
104   TColStd_Array1OfByte aSourceArray(lower, upper);
105   for (; i <= upper; i++)
106   {
107     aSourceArray.SetValue(i, bytes->Value(i));
108   }
109   Standard_Byte *aPtr = (Standard_Byte *) &aSourceArray(lower);
110   theTarget.PutByteArray(aPtr, bytes->Length());
111   theTarget << (Standard_Byte)(anAtt->GetDelta() ? 1 : 0);
112   
113   // process user defined guid
114   if(anAtt->ID() != TDataStd_ByteArray::GetID()) 
115     theTarget << anAtt->ID();
116 }