0031682: Visualization - Prs3d_ShadingAspect::SetTransparency() has no effect with...
[occt.git] / src / StdLPersistent / StdLPersistent_HArray2.hxx
1 // Copyright (c) 2015 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14
15 #ifndef _StdLPersistent_HArray2_HeaderFile
16 #define _StdLPersistent_HArray2_HeaderFile
17
18 #include <Standard_NotImplemented.hxx>
19 #include <Standard_NullValue.hxx>
20
21 #include <StdObjMgt_Persistent.hxx>
22 #include <StdObjMgt_ReadData.hxx>
23 #include <StdObjMgt_WriteData.hxx>
24
25 #include <NCollection_DefineHArray2.hxx>
26
27 #include <TColStd_HArray2OfInteger.hxx>
28 #include <TColStd_HArray2OfReal.hxx>
29
30
31 DEFINE_HARRAY2 (StdLPersistent_HArray2OfPersistent,
32                 NCollection_Array2<Handle(StdObjMgt_Persistent)>)
33
34
35 class StdLPersistent_HArray2
36 {
37   class base : public StdObjMgt_Persistent
38   {
39   public:
40     //! Read persistent data from a file.
41     Standard_EXPORT virtual void Read (StdObjMgt_ReadData& theReadData);
42
43     //! Read persistent data from a file.
44     Standard_EXPORT virtual void Write (StdObjMgt_WriteData& theWriteData) const;
45
46   protected:
47     virtual void lowerBound(Standard_Integer& theRow, 
48                             Standard_Integer& theCol) const = 0;
49     virtual void upperBound(Standard_Integer& theRow, 
50                             Standard_Integer& theCol) const = 0;
51     virtual void createArray(
52       const Standard_Integer theLowerRow, const Standard_Integer theLowerCol,
53       const Standard_Integer theUpperRow, const Standard_Integer theUpperCol)
54         = 0;
55
56     virtual void readValue (StdObjMgt_ReadData&    theReadData,
57                             const Standard_Integer theRow,
58                             const Standard_Integer theCol) = 0;
59     virtual void writeValue(StdObjMgt_WriteData&   theWriteData,
60                             const Standard_Integer theRow,
61                             const Standard_Integer theCol) const = 0;
62   };
63
64 protected:
65   template <class ArrayClass>
66   class instance : public base
67   {
68     friend class StdLPersistent_HArray2;
69
70   public:
71     typedef Handle(ArrayClass) ArrayHandle;
72
73   public:
74     //! Get the array.
75     const Handle(ArrayClass)& Array() const  { return myArray; }
76
77   protected:
78     virtual void lowerBound(Standard_Integer& theRow,
79       Standard_Integer& theCol) const
80     {
81       theRow = myArray->LowerRow(); 
82       theCol = myArray->LowerCol();
83     }
84     virtual void upperBound(Standard_Integer& theRow,
85       Standard_Integer& theCol) const
86     { 
87       theRow = myArray->UpperRow(); 
88       theCol = myArray->UpperCol(); 
89     }
90     virtual void createArray(
91       const Standard_Integer theLowerRow, const Standard_Integer theLowerCol,
92       const Standard_Integer theUpperRow, const Standard_Integer theUpperCol)
93     {
94       myArray = new ArrayClass (theLowerRow, theUpperRow,
95                                 theLowerCol, theUpperCol);
96     }
97     virtual void readValue (StdObjMgt_ReadData&    theReadData,
98                             const Standard_Integer theRow,
99                             const Standard_Integer theCol)
100       { theReadData >> myArray->ChangeValue (theRow, theCol); }
101     virtual void writeValue(StdObjMgt_WriteData&   theWriteData,
102                             const Standard_Integer theRow,
103                             const Standard_Integer theCol) const
104     {
105       theWriteData << myArray->Value(theRow, theCol);
106     }
107     virtual void PChildren(StdObjMgt_Persistent::SequenceOfPersistent& theChildren) const
108       { return PChildrenT(theChildren); }
109     virtual Standard_CString PName() const
110       { return PNameT(); }
111     Standard_CString PNameT() const
112     {
113       Standard_NotImplemented::Raise("StdLPersistent_HArray2::instance::PName - not implemented");
114       return "";
115     }
116     void PChildrenT(StdObjMgt_Persistent::SequenceOfPersistent&) const {}
117
118   protected:
119     Handle(ArrayClass) myArray;
120   };
121
122   template <class ArrayClass>
123   class named_instance : public instance<ArrayClass>
124   {
125     friend class StdLPersistent_HArray2;
126
127   public:
128     virtual Standard_CString PName() const
129     {
130       Standard_NullValue_Raise_if(!myPName,
131         "StdLPersistent_HArray2::named_instance::PName - name not set");
132       return myPName;
133     }
134
135   protected:
136     named_instance(Standard_CString thePName) : myPName(thePName) {}
137
138     Standard_CString myPName;
139   };
140
141 public:
142   typedef instance<TColStd_HArray2OfInteger>           Integer;
143   typedef instance<TColStd_HArray2OfReal>              Real;
144   typedef instance<StdLPersistent_HArray2OfPersistent> Persistent;
145
146 public:
147   template <class ArrayClass>
148   static Handle(instance<ArrayClass>) Translate(const ArrayClass& theArray)
149   {
150     Handle(instance<ArrayClass>) aPArray = new instance<ArrayClass>;
151     aPArray->myArray = new ArrayClass(theArray.LowerRow(), theArray.UpperRow(),
152       theArray.LowerCol(), theArray.UpperCol());
153     for (Standard_Integer i = theArray.LowerRow(); i <= theArray.UpperRow(); ++i)
154       for (Standard_Integer j = theArray.LowerCol(); j <= theArray.UpperCol(); ++j)
155         aPArray->myArray->ChangeValue(i, j) = theArray.Value(i, j);
156     return aPArray;
157   }
158   template <class ArrayClass>
159   static Handle(instance<ArrayClass>) Translate(Standard_CString thePName, const ArrayClass& theArray)
160   {
161     Handle(named_instance<ArrayClass>) aPArray = new named_instance<ArrayClass>(thePName);
162     aPArray->myArray = new ArrayClass(theArray.LowerRow(), theArray.UpperRow(),
163       theArray.LowerCol(), theArray.UpperCol());
164     for (Standard_Integer i = theArray.LowerRow(); i <= theArray.UpperRow(); ++i)
165       for (Standard_Integer j = theArray.LowerCol(); j <= theArray.UpperCol(); ++j)
166         aPArray->myArray->ChangeValue(i, j) = theArray.Value(i, j);
167     return aPArray;
168   }
169 };
170
171 template<>
172 inline Standard_CString StdLPersistent_HArray2::instance<TColStd_HArray2OfInteger>::PNameT() const
173   { return "PColStd_HArray2OfInteger"; }
174
175 template<>
176 inline Standard_CString StdLPersistent_HArray2::instance<TColStd_HArray2OfReal>::PNameT() const
177   { return "PColStd_HArray2OfReal"; }
178
179 template<>
180 inline void StdLPersistent_HArray2::instance<StdLPersistent_HArray2OfPersistent>::PChildrenT
181   (StdObjMgt_Persistent::SequenceOfPersistent& theChildren) const
182   { 
183     for (Standard_Integer i = myArray->LowerRow(); i <= myArray->UpperRow(); ++i)
184       for (Standard_Integer j = myArray->LowerCol(); j <= myArray->UpperCol(); ++j)
185         theChildren.Append(myArray->Value(i, j));
186   }
187
188 #endif