0027970: Improvement of standard attributes usability - containers.
[occt.git] / src / BinMDataStd / BinMDataStd_BooleanArrayDriver.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_BooleanArrayDriver.hxx>
18 #include <BinMDataStd.hxx>
19 #include <BinObjMgt_Persistent.hxx>
20 #include <CDM_MessageDriver.hxx>
21 #include <Standard_Type.hxx>
22 #include <TColStd_Array1OfInteger.hxx>
23 #include <TColStd_HArray1OfByte.hxx>
24 #include <TDataStd_BooleanArray.hxx>
25 #include <TDF_Attribute.hxx>
26
27 IMPLEMENT_STANDARD_RTTIEXT(BinMDataStd_BooleanArrayDriver,BinMDF_ADriver)
28
29 //=======================================================================
30 //function : BinMDataStd_BooleanArrayDriver
31 //purpose  : Constructor
32 //=======================================================================
33 BinMDataStd_BooleanArrayDriver::BinMDataStd_BooleanArrayDriver(const Handle(CDM_MessageDriver)& theMsgDriver)
34      : BinMDF_ADriver (theMsgDriver, STANDARD_TYPE(TDataStd_BooleanArray)->Name())
35 {
36
37 }
38
39 //=======================================================================
40 //function : NewEmpty
41 //purpose  : 
42 //=======================================================================
43 Handle(TDF_Attribute) BinMDataStd_BooleanArrayDriver::NewEmpty() const
44 {
45   return new TDataStd_BooleanArray();
46 }
47
48 //=======================================================================
49 //function : Paste
50 //purpose  : persistent -> transient (retrieve)
51 //=======================================================================
52 Standard_Boolean BinMDataStd_BooleanArrayDriver::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(0, (aLastInd - aFirstInd + 1) >> 3);
63   theSource.GetByteArray (&aTargetArray(0), aTargetArray.Length());
64
65   const Handle(TDataStd_BooleanArray) anAtt = Handle(TDataStd_BooleanArray)::DownCast(theTarget);
66   anAtt->Init(aFirstInd, aLastInd);
67   Handle(TColStd_HArray1OfByte) bytes = new TColStd_HArray1OfByte(aTargetArray.Lower(), aTargetArray.Upper());
68   Standard_Integer lower = bytes->Lower(), i = lower, upper = bytes->Upper();
69   for (; i <= upper; i++)
70   {
71     bytes->SetValue(i, aTargetArray.Value(i));
72   }
73   anAtt->SetInternalArray(bytes);
74   BinMDataStd::SetAttributeID(theSource, anAtt);
75   return Standard_True;
76 }
77
78 //=======================================================================
79 //function : Paste
80 //purpose  : transient -> persistent (store)
81 //=======================================================================
82 void BinMDataStd_BooleanArrayDriver::Paste(const Handle(TDF_Attribute)& theSource,
83                                            BinObjMgt_Persistent&        theTarget,
84                                            BinObjMgt_SRelocationTable&  ) const
85 {
86   Handle(TDataStd_BooleanArray) anAtt = Handle(TDataStd_BooleanArray)::DownCast(theSource);
87   const Standard_Integer aFirstInd = anAtt->Lower();
88   const Standard_Integer aLastInd  = anAtt->Upper();
89   if (aLastInd < aFirstInd)
90     return;
91   theTarget << aFirstInd << aLastInd;
92
93   const Handle(TColStd_HArray1OfByte)& bytes = anAtt->InternalArray();
94   TColStd_Array1OfByte aSourceArray(bytes->Lower(), bytes->Upper());
95   Standard_Integer lower = bytes->Lower(), i = lower, upper = bytes->Upper();
96   for (; i <= upper; i++)
97   {
98     aSourceArray.SetValue(i, bytes->Value(i));
99   }
100   Standard_Byte *aPtr = (Standard_Byte *) &aSourceArray(lower);
101   theTarget.PutByteArray(aPtr, upper - lower + 1);
102
103   // process user defined guid
104   if(anAtt->ID() != TDataStd_BooleanArray::GetID()) 
105     theTarget << anAtt->ID();
106 }