0024129: Eliminate remaining compiler warnings in MSVC++ 2008 32 bit with warning...
[occt.git] / src / BinTObjDrivers / BinTObjDrivers_ObjectDriver.cxx
1 // Created on: 2004-11-24
2 // Created by: Edward AGAPOV
3 // Copyright (c) 2004-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20 // The original implementation Copyright: (C) RINA S.p.A
21
22
23 #include <CDM_MessageDriver.hxx>
24 #include <BinObjMgt_Persistent.hxx>
25 #include <TDF_Tool.hxx>
26
27 #include <BinTObjDrivers_ObjectDriver.hxx>
28 #include <TObj_Assistant.hxx>
29 #include <TObj_TObject.hxx>
30 #include <TObj_Persistence.hxx>
31
32 IMPLEMENT_STANDARD_HANDLE(BinTObjDrivers_ObjectDriver,BinMDF_ADriver)
33 IMPLEMENT_STANDARD_RTTIEXT(BinTObjDrivers_ObjectDriver,BinMDF_ADriver);
34
35 //=======================================================================
36 //function : BinTObjDrivers_ObjectDriver
37 //purpose  : constructor
38 //=======================================================================
39
40 BinTObjDrivers_ObjectDriver::BinTObjDrivers_ObjectDriver
41                          (const Handle(CDM_MessageDriver)& theMessageDriver)
42 : BinMDF_ADriver( theMessageDriver, NULL)
43 {
44 }
45
46 //=======================================================================
47 //function : NewEmpty
48 //purpose  : Creates a new attribute
49 //=======================================================================
50
51 Handle(TDF_Attribute) BinTObjDrivers_ObjectDriver::NewEmpty() const
52 {
53   return new TObj_TObject;
54 }
55
56 //=======================================================================
57 //function : Paste
58 //purpose  : Translate the contents of <theSource> and put it
59 //           into <theTarget>.
60 //=======================================================================
61
62 Standard_Boolean BinTObjDrivers_ObjectDriver::Paste
63                          (const BinObjMgt_Persistent&  theSource,
64                           const Handle(TDF_Attribute)& theTarget,
65                           BinObjMgt_RRelocationTable&) const
66 {
67   Standard_Integer aSavedPos = theSource.Position();
68
69   // first try to get the type as an integer ID
70   Standard_Integer anID;
71   if (! (theSource >> anID)) return Standard_False;
72   Handle(TObj_Object) anObject;
73   if ( (unsigned)anID > 0xffff)
74   {
75     // if we are here it means that the type was stored as an ascii string,
76     // so rewind theSource and reget
77     theSource.SetPosition(aSavedPos);
78     TCollection_AsciiString aName;
79     if (! (theSource >> aName)) return Standard_False;
80     anObject =
81       TObj_Persistence::CreateNewObject(aName.ToCString(),theTarget->Label());
82     if (anObject.IsNull())
83     {
84       TCollection_AsciiString anEntry;
85       TDF_Tool::Entry (theTarget->Label(), anEntry);
86       WriteMessage (TCollection_ExtendedString
87                     ("TObj_TObject retrieval: wrong object type name ") +
88                     aName + ", entry " + anEntry);
89       TObj_Assistant::BindType(0);
90       return Standard_False;
91     }
92     // register the type
93     TObj_Assistant::BindType(anObject->DynamicType());
94   }
95   else 
96   {
97     // use anID to get the type from earlier registered ones
98     Handle(Standard_Type) aType = TObj_Assistant::FindType(anID);
99     if(!aType.IsNull())
100       anObject =
101         TObj_Persistence::CreateNewObject(aType->Name(), theTarget->Label());
102     else 
103     {
104       return Standard_False;
105     }
106   }
107   Handle(TObj_TObject)::DownCast (theTarget) ->Set( anObject );
108   return Standard_True;
109 }
110
111 //=======================================================================
112 //function : Paste
113 //purpose  : Translate the contents of <theSource> and put it
114 //           into <theTarget>.
115 //           anObject is stored as a Name of class derived from TObj_Object
116 //=======================================================================
117
118 void BinTObjDrivers_ObjectDriver::Paste
119                          (const Handle(TDF_Attribute)& theSource,
120                           BinObjMgt_Persistent&        theTarget,
121                           BinObjMgt_SRelocationTable&) const
122 {
123   Handle(TObj_TObject) aTObj =
124     Handle(TObj_TObject)::DownCast( theSource );
125   Handle(TObj_Object) anIObject = aTObj->Get();
126   if (anIObject.IsNull()) return;
127
128   Handle(Standard_Type) aType = anIObject->DynamicType();
129
130   Standard_Integer anID = TObj_Assistant::FindTypeIndex(anIObject->DynamicType());
131
132   if(anID == 0) 
133   {
134     // we first meet this type;
135     // register a type and store a type name as a string
136     TObj_Assistant::BindType(aType);
137     TCollection_AsciiString aName = aType->Name();
138     theTarget << aName;
139   }
140   else 
141   {
142     // store the integer type ID
143     theTarget << anID;
144   }
145 }