0024428: Implementation of LGPL license
[occt.git] / src / BinTObjDrivers / BinTObjDrivers_ObjectDriver.cxx
CommitLineData
b311480e 1// Created on: 2004-11-24
2// Created by: Edward AGAPOV
973c2be1 3// Copyright (c) 2004-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
973c2be1 7// This library is free software; you can redistribute it and / or modify it
8// under the terms of the GNU Lesser General Public 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
7fd59977 16// The original implementation Copyright: (C) RINA S.p.A
17
18
19#include <CDM_MessageDriver.hxx>
20#include <BinObjMgt_Persistent.hxx>
21#include <TDF_Tool.hxx>
22
23#include <BinTObjDrivers_ObjectDriver.hxx>
24#include <TObj_Assistant.hxx>
25#include <TObj_TObject.hxx>
26#include <TObj_Persistence.hxx>
27
28IMPLEMENT_STANDARD_HANDLE(BinTObjDrivers_ObjectDriver,BinMDF_ADriver)
29IMPLEMENT_STANDARD_RTTIEXT(BinTObjDrivers_ObjectDriver,BinMDF_ADriver);
30
31//=======================================================================
32//function : BinTObjDrivers_ObjectDriver
33//purpose : constructor
34//=======================================================================
35
36BinTObjDrivers_ObjectDriver::BinTObjDrivers_ObjectDriver
37 (const Handle(CDM_MessageDriver)& theMessageDriver)
38: BinMDF_ADriver( theMessageDriver, NULL)
39{
40}
41
42//=======================================================================
43//function : NewEmpty
44//purpose : Creates a new attribute
45//=======================================================================
46
47Handle(TDF_Attribute) BinTObjDrivers_ObjectDriver::NewEmpty() const
48{
49 return new TObj_TObject;
50}
51
52//=======================================================================
53//function : Paste
54//purpose : Translate the contents of <theSource> and put it
55// into <theTarget>.
56//=======================================================================
57
58Standard_Boolean BinTObjDrivers_ObjectDriver::Paste
59 (const BinObjMgt_Persistent& theSource,
60 const Handle(TDF_Attribute)& theTarget,
61 BinObjMgt_RRelocationTable&) const
62{
63 Standard_Integer aSavedPos = theSource.Position();
64
65 // first try to get the type as an integer ID
66 Standard_Integer anID;
67 if (! (theSource >> anID)) return Standard_False;
68 Handle(TObj_Object) anObject;
69 if ( (unsigned)anID > 0xffff)
70 {
71 // if we are here it means that the type was stored as an ascii string,
72 // so rewind theSource and reget
73 theSource.SetPosition(aSavedPos);
74 TCollection_AsciiString aName;
75 if (! (theSource >> aName)) return Standard_False;
76 anObject =
77 TObj_Persistence::CreateNewObject(aName.ToCString(),theTarget->Label());
78 if (anObject.IsNull())
79 {
80 TCollection_AsciiString anEntry;
81 TDF_Tool::Entry (theTarget->Label(), anEntry);
82 WriteMessage (TCollection_ExtendedString
83 ("TObj_TObject retrieval: wrong object type name ") +
84 aName + ", entry " + anEntry);
85 TObj_Assistant::BindType(0);
86 return Standard_False;
87 }
88 // register the type
89 TObj_Assistant::BindType(anObject->DynamicType());
90 }
91 else
92 {
93 // use anID to get the type from earlier registered ones
94 Handle(Standard_Type) aType = TObj_Assistant::FindType(anID);
95 if(!aType.IsNull())
96 anObject =
97 TObj_Persistence::CreateNewObject(aType->Name(), theTarget->Label());
98 else
99 {
100 return Standard_False;
101 }
102 }
103 Handle(TObj_TObject)::DownCast (theTarget) ->Set( anObject );
104 return Standard_True;
105}
106
107//=======================================================================
108//function : Paste
109//purpose : Translate the contents of <theSource> and put it
110// into <theTarget>.
111// anObject is stored as a Name of class derived from TObj_Object
112//=======================================================================
113
114void BinTObjDrivers_ObjectDriver::Paste
115 (const Handle(TDF_Attribute)& theSource,
116 BinObjMgt_Persistent& theTarget,
117 BinObjMgt_SRelocationTable&) const
118{
119 Handle(TObj_TObject) aTObj =
120 Handle(TObj_TObject)::DownCast( theSource );
121 Handle(TObj_Object) anIObject = aTObj->Get();
122 if (anIObject.IsNull()) return;
123
124 Handle(Standard_Type) aType = anIObject->DynamicType();
125
126 Standard_Integer anID = TObj_Assistant::FindTypeIndex(anIObject->DynamicType());
127
128 if(anID == 0)
129 {
130 // we first meet this type;
131 // register a type and store a type name as a string
132 TObj_Assistant::BindType(aType);
133 TCollection_AsciiString aName = aType->Name();
134 theTarget << aName;
135 }
136 else
137 {
138 // store the integer type ID
139 theTarget << anID;
140 }
141}