]> OCCT Git - occt-copy.git/commitdiff
0027111: Add generalized copy constructor in handle class for old compilers
authorabv <abv@opencascade.com>
Sat, 6 Feb 2016 12:26:26 +0000 (15:26 +0300)
committerabv <abv@opencascade.com>
Sat, 20 Feb 2016 07:10:12 +0000 (10:10 +0300)
Copy constructor and assignment operator from handle of derived type is added in handle class.
They are enabled only if macro OCC_HANDLE_NOCASTS is defined, and operators of cast of handle to reference to handle to base type are disabled in that case.

Useless type casts to handle to base type are removed in GC and GCE2d classes.
Code returning reference to handle from function is corrected to return it either by value or as reference to handle of actual type.

44 files changed:
dox/dev_guides/upgrade/upgrade.md
src/AIS/AIS_LocalContext_1.cxx
src/BinDrivers/BinDrivers.cxx
src/BinLDrivers/BinLDrivers.cxx
src/BinTObjDrivers/BinTObjDrivers.cxx
src/BinXCAFDrivers/BinXCAFDrivers.cxx
src/FWOSDriver/FWOSDriver.cxx
src/GC/GC_MakeArcOfCircle.hxx
src/GC/GC_MakeArcOfEllipse.hxx
src/GC/GC_MakeArcOfHyperbola.hxx
src/GC/GC_MakeArcOfParabola.hxx
src/GC/GC_MakeCircle.hxx
src/GC/GC_MakeConicalSurface.hxx
src/GC/GC_MakeCylindricalSurface.hxx
src/GC/GC_MakeEllipse.hxx
src/GC/GC_MakeHyperbola.hxx
src/GC/GC_MakeLine.hxx
src/GC/GC_MakePlane.hxx
src/GC/GC_MakeSegment.hxx
src/GC/GC_MakeTrimmedCone.hxx
src/GC/GC_MakeTrimmedCylinder.hxx
src/GCE2d/GCE2d_MakeArcOfCircle.hxx
src/GCE2d/GCE2d_MakeArcOfEllipse.hxx
src/GCE2d/GCE2d_MakeArcOfHyperbola.hxx
src/GCE2d/GCE2d_MakeArcOfParabola.hxx
src/GCE2d/GCE2d_MakeCircle.hxx
src/GCE2d/GCE2d_MakeEllipse.hxx
src/GCE2d/GCE2d_MakeHyperbola.hxx
src/GCE2d/GCE2d_MakeLine.hxx
src/GCE2d/GCE2d_MakeParabola.hxx
src/GCE2d/GCE2d_MakeSegment.hxx
src/OpenGl/OpenGl_Context.cxx
src/PrsMgr/PrsMgr_Presentation.cxx
src/PrsMgr/PrsMgr_Presentation.hxx
src/Standard/Standard_Handle.hxx
src/TopOpeBRepBuild/TopOpeBRepBuild_LoopSet.cxx
src/TopOpeBRepBuild/TopOpeBRepBuild_LoopSet.hxx
src/TopOpeBRepBuild/TopOpeBRepBuild_PaveSet.cxx
src/TopOpeBRepBuild/TopOpeBRepBuild_PaveSet.hxx
src/XSControl/XSControl_Controller.cxx
src/XmlDrivers/XmlDrivers.cxx
src/XmlLDrivers/XmlLDrivers.cxx
src/XmlTObjDrivers/XmlTObjDrivers.cxx
src/XmlXCAFDrivers/XmlXCAFDrivers.cxx

index cf7142974d24dcdb9a71eba5238da9a069a840bc..518f59179c1286715a8a0a1956bac301c3f89415 100644 (file)
@@ -219,6 +219,8 @@ Handle(Geom_TrimmedCurve) aCurve = new Geom_TrimmedCurve (...);
 func (aCurve); // ambiguity error in VC++ 10
 ~~~~~
 
+Note that this problem does not appear if macro *OCCT_HANDLE_NOCAST* is used, see @ref upgrade_occt700_cdl_nocast "below".
+
 To resolve this ambiguity, change your code so that argument type should correspond exactly to the function signature. 
 In some cases this can be done by using the relevant type for the corresponding variable, like in the example above:
 
@@ -262,6 +264,16 @@ or use variable of the appropriate type:
 Handle(Geom_TrimmedCurve) aC = GC_MakeLine (p, v); // ok
 ~~~~~
 
+With GCC compiler, similar problem appears when const handle to derived type is used to construct handle to base type via assignment (and in some cases in return statement), for instance:
+
+~~~~~
+  const Handle(Geom_Line) aLine;
+  Handle(Geom_Curve) c1 = aLine; // GCC error 
+  Handle(Geom_Curve) c2 (aLine); // ok
+~~~~~
+
+This problem is specific to GCC and it does not appear if macro *OCCT_HANDLE_NOCAST* is used, see @ref upgrade_occt700_cdl_nocast "below".
+
 #### Incorrect use of STANDARD_TYPE and Handle macros
 
 You might need to clean your code from incorrect use of macros *STANDARD_TYPE*() and *Handle*().
@@ -317,7 +329,8 @@ Here is the list of known possible problems at run time after the upgrade to OCC
 #### References to temporary objects
 
 In previous versions, the compiler was able to detect the situation when a local variable of a "reference to a Handle" type is initialized by temporary object, and ensured that lifetime of that object is longer than that of the variable. 
-Since OCCT 7.0, it will not work if types of the temporary object and variable are different (due to involvement of user-defined type cast), thus such temporary object will be destroyed immediately.
+In OCCT 7.0 with default options, it will not work if types of the temporary object and variable are different (due to involvement of user-defined type cast), thus such temporary object will be destroyed immediately.
+This problem does not appear if macro *OCCT_HANDLE_NOCAST* is used during compilation, see below.
 
 Example:
 
@@ -328,6 +341,42 @@ Handle(Geom_TrimmedCurve)::DownCast(aCurve);
 aBC->Transform (T); // access violation in OCCT 7.0
 ~~~~~
 
+@subsubsection upgrade_occt700_cdl_nocast Option to avoid cast of handle to reference to base type
+
+In OCCT 6.x and earlier versions the handle classes formed a hierarchy echoing hierarchy of corresponding object classes.
+This automatically enabled possibility to use handle to derived class in all contexts where handle to base class was needed, e.g. pass it in function by reference without copying:
+
+~~~~
+Standard_Boolean GetCurve (Handle(Geom_Curve)& theCurve);
+....
+Handle(Geom_Line) aLine;
+if (GetCurve (aLine)) {
+  // use aLine, unsafe
+}
+~~~~
+
+This feature was used in multiple places in OCCT and dependent projects.
+However it is potentially unsafe: in the above example no checks are done at compile time or at run time to ensure that argument handle is assigned a type compatible with the type of handle passed as argument. 
+If object of incompatible type (e.g. Geom_Circle) is assigned to *theCurve*, the behavior will be unpredictable.
+
+For compatibility with existing code, by default OCCT 7.0 keeps this possibility, providing operators of type cast to handle to base type.
+Besides being unsafe, in specific situations this feature may cause compile-time or run-time errors as described above.
+
+In order to provide safer behavior, this feature can be disabled by defining a compile-time macro *OCCT_HANDLE_NOCAST*.
+When it is defined, constructors and assignment operators are defined (instead of type cast operators) to convert from handle to defived type to handle to base type.
+This implies creation of temporary objects and hence may be more expensive at run time in some circumstances, however this way is more standard, safer, and in general recommended.
+
+The code that relies on possibility of casting to base should be amended so that handle of argument type is always used in function call, and to use DownCast() to safely convert the result to desired type.
+For instance, the code from the example below can be changed as follows:
+
+~~~~~
+Handle(Geom_Line) aLine;
+Handle(Geom_Curve) aCurve;
+if (GetCurve (aCure) && !(aLine = Handle(Geom_Line)::DownCast (aCurve)).IsNull()) {
+  // use aLine safely
+}
+~~~~~
+
 @subsubsection upgrade_occt700_cdl_compat Preserving compatibility with OCCT 6.x
 
 If you like to preserve the compatibility of your application code with OCCT versions 6.x even after the upgrade to 7.0, consider the following suggestions:
index 28c3c400296a64da3275ab18d05c6dfcc8080379..68ecf0d8a30fa98acd14b834347054c62c740c85 100644 (file)
@@ -1350,12 +1350,12 @@ Standard_Boolean AIS_LocalContext::IsShape(const Standard_Integer Index) const
 
 Standard_Boolean AIS_LocalContext::IsValidForSelection(const Handle(AIS_InteractiveObject)& anIObj) const 
 {
-
+  const Handle(SelectMgr_SelectableObject)& aSelObj = anIObj; // to avoid ambiguity
   // Shape was not transfered from AIS_Shape to EntityOwner
   Handle(AIS_Shape) shape = Handle(AIS_Shape)::DownCast(anIObj);
   if( !shape.IsNull() ) 
-    return myFilters->IsOk(new StdSelect_BRepOwner(shape->Shape(),shape));
-  return myFilters->IsOk(new SelectMgr_EntityOwner((const Handle(SelectMgr_SelectableObject)&)anIObj));
+    return myFilters->IsOk(new StdSelect_BRepOwner(shape->Shape(), aSelObj));
+  return myFilters->IsOk(new SelectMgr_EntityOwner(aSelObj));
 }
 
 
index f4e38ec25f5f3c64ca2b90abe805a0253e52e4c6..e6112148989372401783d29d9ed9d6ae57f087ce 100644 (file)
@@ -46,7 +46,7 @@ const Handle(Standard_Transient)& BinDrivers::Factory(const Standard_GUID& theGU
 #ifdef OCCT_DEBUG
     cout << "BinDrivers : Storage Plugin" << endl;
 #endif
-    static Handle(BinDrivers_DocumentStorageDriver) model_sd =
+    static Handle(Standard_Transient) model_sd =
       new BinDrivers_DocumentStorageDriver;
     return model_sd;
   }
@@ -56,7 +56,7 @@ const Handle(Standard_Transient)& BinDrivers::Factory(const Standard_GUID& theGU
 #ifdef OCCT_DEBUG
     cout << "BinDrivers : Retrieval Plugin" << endl;
 #endif
-    static Handle(BinDrivers_DocumentRetrievalDriver) model_rd =
+    static Handle(Standard_Transient) model_rd =
       new BinDrivers_DocumentRetrievalDriver;
     return model_rd;
   }
index 6b597ac465920a09c053c888c128f231ae3603dd..5ed6d3916d44ac41d9efb004c13ce727b472a54d 100644 (file)
@@ -45,7 +45,7 @@ const Handle(Standard_Transient)& BinLDrivers::Factory(const Standard_GUID& theG
 #ifdef OCCT_DEBUG
     cout << "BinLDrivers : Storage Plugin" << endl;
 #endif
-    static Handle(BinLDrivers_DocumentStorageDriver) model_sd =
+    static Handle(Standard_Transient) model_sd =
       new BinLDrivers_DocumentStorageDriver;
     return model_sd;
   }
@@ -55,7 +55,7 @@ const Handle(Standard_Transient)& BinLDrivers::Factory(const Standard_GUID& theG
 #ifdef OCCT_DEBUG
     cout << "BinLDrivers : Retrieval Plugin" << endl;
 #endif
-    static Handle(BinLDrivers_DocumentRetrievalDriver) model_rd =
+    static Handle(Standard_Transient) model_rd =
       new BinLDrivers_DocumentRetrievalDriver;
     return model_rd;
   }
index 6385c8a5055bc4264a9037e4c3185fbebe508cdf..a3181cbfb726d3e84f5cfb51f6b88fea19a9d6df 100644 (file)
@@ -37,7 +37,7 @@ const Handle(Standard_Transient)& BinTObjDrivers::Factory(const Standard_GUID& a
 #ifdef OCCT_DEBUG
     cout << "BinTObjDrivers : Storage Plugin" << endl;
 #endif
-    static Handle(BinTObjDrivers_DocumentStorageDriver) model_sd
+    static Handle(Standard_Transient) model_sd
       = new BinTObjDrivers_DocumentStorageDriver;
     return model_sd;
   }
@@ -47,7 +47,7 @@ const Handle(Standard_Transient)& BinTObjDrivers::Factory(const Standard_GUID& a
 #ifdef OCCT_DEBUG
     cout << "BinTObjDrivers : Retrieval Plugin" << endl;
 #endif
-    static Handle (BinTObjDrivers_DocumentRetrievalDriver) model_rd
+    static Handle (Standard_Transient) model_rd
       = new BinTObjDrivers_DocumentRetrievalDriver;
     return model_rd;
   }
index 5edd58b3ec79aca0a2e0d5c6ad3650b8c33c0ade..3d1d36a64f8669918bc437b84ff3364c9fde081d 100644 (file)
@@ -40,7 +40,7 @@ const Handle(Standard_Transient)& BinXCAFDrivers::Factory(const Standard_GUID& t
 #ifdef OCCT_DEBUG
     cout << "BinXCAFDrivers : Storage Plugin" << endl;
 #endif
-    static Handle(BinXCAFDrivers_DocumentStorageDriver) model_sd =
+    static Handle(Standard_Transient) model_sd =
       new BinXCAFDrivers_DocumentStorageDriver;
     return model_sd;
   }
@@ -50,7 +50,7 @@ const Handle(Standard_Transient)& BinXCAFDrivers::Factory(const Standard_GUID& t
 #ifdef OCCT_DEBUG
     cout << "BinXCAFDrivers : Retrieval Plugin" << endl;
 #endif
-    static Handle(BinXCAFDrivers_DocumentRetrievalDriver) model_rd =
+    static Handle(Standard_Transient) model_rd =
       new BinXCAFDrivers_DocumentRetrievalDriver;
     return model_rd;
   }
index 21807732ef50d710ac3d608552516c0be97f9a63..c3076bcc662ef395af149ee3aa2b1197beb827b5 100644 (file)
@@ -24,7 +24,7 @@
 PLUGIN(FWOSDriver)
 
 const Handle(Standard_Transient)& FWOSDriver::Factory(const Standard_GUID& /*aGUID*/) {
-  static Handle(FWOSDriver_DriverFactory) f;
+  static Handle(Standard_Transient) f;
   if(f.IsNull()) f = new FWOSDriver_DriverFactory;
   return f;
 }
index 7d4d45aa07aee32579531bc8b926891c106e6cd0..5aba70b7438a7a163247f8c292e1be7c771c40b9 100644 (file)
@@ -88,7 +88,6 @@ public:
   Standard_EXPORT const Handle(Geom_TrimmedCurve)& Value() const;
 
   operator const Handle(Geom_TrimmedCurve)& () const { return Value(); }
-  operator const Handle(Geom_Curve)& () const { return Value(); }
 
 private:
   Handle(Geom_TrimmedCurve) TheArc;
index 6f1f302c8214e6c8e52b84d7fed1a66b65663aa6..e9e93e76aae4ed4768a96adfc8924f9198f97c11 100644 (file)
@@ -66,7 +66,6 @@ public:
   Standard_EXPORT const Handle(Geom_TrimmedCurve)& Value() const;
 
   operator const Handle(Geom_TrimmedCurve)& () const { return Value(); }
-  operator const Handle(Geom_Curve)& () const { return Value(); }
 
 private:
   Handle(Geom_TrimmedCurve) TheArc;
index 12263ad81997bbdb57b788e65e5611179039605e..0eca295811e86266296813ef2f7023237e6ccd92 100644 (file)
@@ -64,7 +64,6 @@ public:
   Standard_EXPORT const Handle(Geom_TrimmedCurve)& Value() const;
 
   operator const Handle(Geom_TrimmedCurve)& () const { return Value(); }
-  operator const Handle(Geom_Curve)& () const { return Value(); }
 
 private:
   Handle(Geom_TrimmedCurve) TheArc;
index 34973ca88d36e8a4d145696892b5c51a0b2b9030..b4d0bb50d67f57d552b15067317f7d419b63d8f1 100644 (file)
@@ -61,7 +61,6 @@ public:
   Standard_EXPORT const Handle(Geom_TrimmedCurve)& Value() const;
 
   operator const Handle(Geom_TrimmedCurve)& () const { return Value(); }
-  operator const Handle(Geom_Curve)& () const { return Value(); }
 
 private:
   Handle(Geom_TrimmedCurve) TheArc;
index 77915ccd96fa44fc283664e13b76e2737efe223d..71419995afb9da3e0eef579db9d20fbc6bd20769 100644 (file)
@@ -101,7 +101,6 @@ public:
   Standard_EXPORT const Handle(Geom_Circle)& Value() const;
 
   operator const Handle(Geom_Circle)& () const { return Value(); }
-  operator const Handle(Geom_Curve)& () const { return Value(); }
 
 private:
   Handle(Geom_Circle) TheCircle;
index 82477d85ffb2aadc4d9c576e47c2b0f78c372211..db5c35d493f49151eff3a1f60618768550f3a8b1 100644 (file)
@@ -125,7 +125,6 @@ public:
   Standard_EXPORT const Handle(Geom_ConicalSurface)& Value() const;
 
   operator const Handle(Geom_ConicalSurface)& () const { return Value(); }
-  operator const Handle(Geom_Surface)& () const { return Value(); }
 
 private:
   Handle(Geom_ConicalSurface) TheCone;
index ba0f3d8fb5ab3a232d92da309959abb00a33c1f2..a29e75e60da11e815af3c5e2ccaf119b63973a4b 100644 (file)
@@ -113,7 +113,6 @@ public:
   Standard_EXPORT const Handle(Geom_CylindricalSurface)& Value() const;
 
   operator const Handle(Geom_CylindricalSurface)& () const { return Value(); }
-  operator const Handle(Geom_Surface)& () const { return Value(); }
 
 private:
   Handle(Geom_CylindricalSurface) TheCylinder;
index 4e201dc329eb88696cb190645aac9e7aa9570c07..48c56985ecfbd1fc9d6266fad341b7b6fd1876bd 100644 (file)
@@ -79,7 +79,6 @@ public:
   Standard_EXPORT const Handle(Geom_Ellipse)& Value() const;
 
   operator const Handle(Geom_Ellipse)& () const { return Value(); }
-  operator const Handle(Geom_Curve)& () const { return Value(); }
 
 private:
   Handle(Geom_Ellipse) TheEllipse;
index 6bdf8bfe1638bfb1a9923fc9d78d08c4a65be073..7c0f8e2d7bff7349d41616b87a60eb502d03bcd0 100644 (file)
@@ -89,7 +89,6 @@ public:
   Standard_EXPORT const Handle(Geom_Hyperbola)& Value() const;
 
   operator const Handle(Geom_Hyperbola)& () const { return Value(); }
-  operator const Handle(Geom_Curve)& () const { return Value(); }
 
 private:
   Handle(Geom_Hyperbola) TheHyperbola;
index 6cdf3a15e933ef136773a2a21550ff87e416c850..44bd5b0190ea39e8278cb4276e37774a982f80c2 100644 (file)
@@ -78,7 +78,6 @@ public:
   Standard_EXPORT const Handle(Geom_Line)& Value() const;
 
   operator const Handle(Geom_Line)& () const { return Value(); }
-  operator const Handle(Geom_Curve)& () const { return Value(); }
 
 private:
   Handle(Geom_Line) TheLine;
index dfe23399c3cabb5092b9a4ffe283370d9180bc29..87644cb71cfd0c5219efa1a204533c1bde474518 100644 (file)
@@ -102,7 +102,6 @@ public:
   Standard_EXPORT const Handle(Geom_Plane)& Value() const;
 
   operator const Handle(Geom_Plane)& () const { return Value(); }
-  operator const Handle(Geom_Surface)& () const { return Value(); }
 
 private:
   Handle(Geom_Plane) ThePlane;
index 1f927238f3a6fd255336a8f2653e6e1775a4b19b..82f23ea408c59bfa5f535ab9cb732e793b1fd4a1 100644 (file)
@@ -68,7 +68,6 @@ public:
   Standard_EXPORT const Handle(Geom_TrimmedCurve)& Value() const;
 
   operator const Handle(Geom_TrimmedCurve)& () const { return Value(); }
-  operator const Handle(Geom_Curve)& () const { return Value(); }
 
 private:
   Handle(Geom_TrimmedCurve) TheSegment;
index 65483086c455d1aa9a1e3410ee21ae1609e12746..db068b7457bb9dbf7942e3187b0690c95c9e4d1a 100644 (file)
@@ -77,7 +77,6 @@ public:
   Standard_EXPORT const Handle(Geom_RectangularTrimmedSurface)& Value() const;
 
   operator const Handle(Geom_RectangularTrimmedSurface)& () const { return Value(); }
-  operator const Handle(Geom_Surface)& () const { return Value(); }
 
 private:
   Handle(Geom_RectangularTrimmedSurface) TheCone;
index 437aa80c8bd5235636834112ca1996ff1f704a9c..50811ea9aeaa0941996df6650fc30e669cf74dba 100644 (file)
@@ -95,7 +95,6 @@ public:
   Standard_EXPORT const Handle(Geom_RectangularTrimmedSurface)& Value() const;
 
   operator const Handle(Geom_RectangularTrimmedSurface)& () const { return Value(); }
-  operator const Handle(Geom_Surface)& () const { return Value(); }
 
 private:
   Handle(Geom_RectangularTrimmedSurface) TheCyl;
index 34ca3efc1941e2310596db19edd560896ea60127..77471e52f0742941565871768598a86c66663c5c 100644 (file)
@@ -74,7 +74,6 @@ public:
   Standard_EXPORT const Handle(Geom2d_TrimmedCurve)& Value() const;
 
   operator const Handle(Geom2d_TrimmedCurve)& () const { return Value(); }
-  operator const Handle(Geom2d_Curve)& () const { return Value(); }
 
 private:
   Handle(Geom2d_TrimmedCurve) TheArc;
index a2c8fd27ab4715269cb6e1609d2f04cf5ff34a13..bf63890e06516c656dbe8035c2164f562ef61209 100644 (file)
@@ -66,7 +66,6 @@ public:
   Standard_EXPORT const Handle(Geom2d_TrimmedCurve)& Value() const;
 
   operator const Handle(Geom2d_TrimmedCurve)& () const { return Value(); }
-  operator const Handle(Geom2d_Curve)& () const { return Value(); }
 
 private:
   Handle(Geom2d_TrimmedCurve) TheArc;
index 2393933597669ab0bb505e35b16ab74e598352b4..155bac5eadf3e80c1e230d3b7849e0ad04fc5c3c 100644 (file)
@@ -65,7 +65,6 @@ public:
   Standard_EXPORT const Handle(Geom2d_TrimmedCurve)& Value() const;
 
   operator const Handle(Geom2d_TrimmedCurve)& () const { return Value(); }
-  operator const Handle(Geom2d_Curve)& () const { return Value(); }
 
 private:
   Handle(Geom2d_TrimmedCurve) TheArc;
index 854f103706004e0679e2e7042328bbc30b5b91fe..0f0613de31fd500796337fec3b8dc23515f1b455 100644 (file)
@@ -65,7 +65,6 @@ public:
   Standard_EXPORT const Handle(Geom2d_TrimmedCurve)& Value() const;
 
   operator const Handle(Geom2d_TrimmedCurve)& () const { return Value(); }
-  operator const Handle(Geom2d_Curve)& () const { return Value(); }
 
 private:
   Handle(Geom2d_TrimmedCurve) TheArc;
index d51dcaa94f1814ddf30bd9930f08730da40a8af5..ce073285a527778dfeb5176a83d872b25aa4f6e8 100644 (file)
@@ -103,7 +103,6 @@ public:
   Standard_EXPORT const Handle(Geom2d_Circle)& Value() const;
 
   operator const Handle(Geom2d_Circle)& () const { return Value(); }
-  operator const Handle(Geom2d_Curve)& () const { return Value(); }
 
 private:
   Handle(Geom2d_Circle) TheCircle;
index 849100f8f63e483f65f20fdcc3e5a39541315415..350bb0b3dd8ba4ac5bd03fc326cdd0c9b331fb87 100644 (file)
@@ -88,7 +88,6 @@ public:
   Standard_EXPORT const Handle(Geom2d_Ellipse)& Value() const;
 
   operator const Handle(Geom2d_Ellipse)& () const { return Value(); }
-  operator const Handle(Geom2d_Curve)& () const { return Value(); }
 
 private:
   Handle(Geom2d_Ellipse) TheEllipse;
index e9936c8a38be29d8068c09eb714fb249fbd5937b..74485c78120534261c0737cb43078c69e7afe240 100644 (file)
@@ -108,7 +108,6 @@ public:
   Standard_EXPORT const Handle(Geom2d_Hyperbola)& Value() const;
 
   operator const Handle(Geom2d_Hyperbola)& () const { return Value(); }
-  operator const Handle(Geom2d_Curve)& () const { return Value(); }
 
 private:
   Handle(Geom2d_Hyperbola) TheHyperbola;
index a13d9d6e1ee1c91ed3054ee5959d004d1ca568fc..68c88902c1376e2f18b72267bb3d88a89c3a176d 100644 (file)
@@ -77,7 +77,6 @@ public:
   Standard_EXPORT const Handle(Geom2d_Line)& Value() const;
 
   operator const Handle(Geom2d_Line)& () const { return Value(); }
-  operator const Handle(Geom2d_Curve)& () const { return Value(); }
 
 private:
   Handle(Geom2d_Line) TheLine;
index 0111c22f6a455f66a07f9a0897e5cb1514769fa3..0687d7d8c43a4a4accde88b3e4411e1a2763832f 100644 (file)
@@ -106,7 +106,6 @@ public:
   Standard_EXPORT const Handle(Geom2d_Parabola)& Value() const;
 
   operator const Handle(Geom2d_Parabola)& () const { return Value(); }
-  operator const Handle(Geom2d_Curve)& () const { return Value(); }
 
 private:
   Handle(Geom2d_Parabola) TheParabola;
index 679bc1456428c736255bf34cbb24dc57e49cde4a..e2498196d849a996fb4767e2588fd9b51d693e29 100644 (file)
@@ -82,7 +82,6 @@ public:
   Standard_EXPORT const Handle(Geom2d_TrimmedCurve)& Value() const;
 
   operator const Handle(Geom2d_TrimmedCurve)& () const { return Value(); }
-  operator const Handle(Geom2d_Curve)& () const { return Value(); }
 
 private:
   Handle(Geom2d_TrimmedCurve) TheSegment;
index 3762d4b2fc81d57ca0888e6c994a815d9f6306ed..ab026e979a7c13d129603706ab58a0f54f56c9ef 100644 (file)
@@ -210,7 +210,7 @@ OpenGl_Context::~OpenGl_Context()
   }
 
   // release shared resources if any
-  if (((const Handle(Standard_Transient)& )mySharedResources)->GetRefCount() <= 1)
+  if (mySharedResources->GetRefCount() <= 1)
   {
     myShaderManager.Nullify();
     for (NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)>::Iterator anIter (*mySharedResources);
@@ -2384,7 +2384,7 @@ void OpenGl_Context::ReleaseResource (const TCollection_AsciiString& theKey,
   {
     return;
   }
-  const Handle(OpenGl_Resource)& aRes = mySharedResources->Find (theKey);
+  auto& aRes = mySharedResources->Find (theKey);
   if (aRes->GetRefCount() > 1)
   {
     return;
@@ -2432,7 +2432,7 @@ void OpenGl_Context::ReleaseDelayed()
       continue;
     }
 
-    Handle(OpenGl_Resource)& aRes = mySharedResources->ChangeFind (aKey);
+    auto& aRes = mySharedResources->ChangeFind (aKey);
     if (aRes->GetRefCount() > 1)
     {
       // should be only 1 instance in mySharedResources
index 22ee0e2fe9a3377f80a98fcaf39548728730c3ee..75f46a5cdbe54c91ca29d280f79bb07aff6bb7e6 100644 (file)
@@ -40,7 +40,7 @@ namespace
     State_Visible
   };
 
-  static BeforeHighlightState StructureState(const Handle(PrsMgr_Prs)& theStructure)
+  static BeforeHighlightState StructureState(const Handle(Prs3d_Presentation)& theStructure)
   {
     return !theStructure->IsDisplayed() ?
       State_Empty : !theStructure->IsVisible() ?
index e93f122a3a3dd2fa1239fce9eb05067226f082fd..fd2879d058e0aec04dea02e8f915182a92d783f3 100644 (file)
@@ -133,7 +133,7 @@ private:
   Standard_EXPORT static Handle(Prs3d_Projector) Projector (const Handle(Graphic3d_DataStructureManager)& theProjector);
 
   Handle(PrsMgr_PresentationManager) myPresentationManager;
-  Handle(PrsMgr_Prs) myStructure;
+  Handle(Prs3d_Presentation) myStructure;
   PrsMgr_PresentableObjectPointer myPresentableObject;
   Standard_Boolean myMustBeUpdated;
   Standard_Integer myBeforeHighlightState;
index c037e608e2b109324eb577d8926f1d2ad6b8936e..23b075bc147c7d17e2f0d4d35f5f8c80df8785cc 100644 (file)
@@ -24,13 +24,27 @@ class Standard_Transient;
 
 namespace opencascade {
 
+  //! Trait yielding true if class T1 is base of T2 but not the same
+  template <class T1, class T2, class Dummy = void>
+  struct is_base_but_not_same : std::is_base_of <T1, T2> {};
+
+  //! Explicit specialization of is_base_of trait to workaround the
+  //! requirement of type to be complete when T1 and T2 are the same.
+  template <class T1, class T2>
+  struct is_base_but_not_same <T1, T2, typename std::enable_if <std::is_same <T1, T2>::value>::type> : std::false_type {};
+
   //! Intrusive smart pointer for use with Standard_Transient class and its descendants.
   //!
   //! This class is similar to boost::intrusive_ptr<>, with additional
   //! feature historically supported by Handles in OCCT:
   //! it has type conversion to const reference to handle to the base types,
   //! which allows it to be passed by reference
-  //! in functions accepring reference to handle to base class.
+  //! in functions accepting reference to handle to base class.
+  //!
+  //! These casts (potentially unsafe) can be disabled by defining macro
+  //! OCCT_HANDLE_NOCAST; if it is defined, generalized copy constructor
+  //! and assignment operators are defined allowing to initialize handle
+  //! of base type from handle to derived type.
   template <class T>
   class handle
   {
@@ -48,14 +62,7 @@ namespace opencascade {
     {
       BeginScope();
     }
-/* TODO: uncomment and remove const from method above
-    //! Constructor from const pointer to new object;
-    //! will raise exception if object's reference counter is zero 
-    explicit handle (const T *thePtr) : entity(thePtr->This()) 
-    {
-      BeginScope();
-    }
-*/
+
     //! Copy constructor
     handle (const handle& theHandle) : entity(theHandle.entity)
     {
@@ -96,14 +103,7 @@ namespace opencascade {
       Assign (const_cast<T*>(thePtr));
       return *this;
     }
-/* uncomment along with constructor 
-    //! Assignment to pointer to const object
-    handle& operator= (const T* thePtr)
-    {
-      Assign (thePtr->This());
-      return *this;
-    }
-*/
+
     //! STL-like cast to pointer to referred object
     const T* get () const { return static_cast<const T*>(this->entity); }
 
@@ -202,13 +202,36 @@ namespace opencascade {
 
 #endif
 
-    //! Upcast to const reference to base type.
+    // Support of conversions to handle of base type:
+    // - copy and move constructors and assignment operators if OCCT_HANDLE_NOCAST is defined
+    // - operators of upcast to const reference to base type otherwise
 #if (defined(__clang__)) || (defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 1206) || \
     (defined(_MSC_VER) && _MSC_VER >= 1800) || \
     (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)))
 
+#ifdef OCCT_HANDLE_NOCAST
+
+    //! Generalized copy constructor.
+    //! Constructs handle holding entity of base type (T) from the one which holds entity of derived type (T2).
+    template <class T2, typename = typename std::enable_if <is_base_but_not_same <T, T2>::value>::type>
+    handle (const handle<T2>& theHandle) :
+      entity(theHandle.entity)
+    {
+      BeginScope();
+    }
+
+    //! Generalized assignment operator
+    template <class T2, typename = typename std::enable_if <is_base_but_not_same <T, T2>::value>::type>
+    handle operator = (const handle<T2>& theHandle)
+    {
+      Assign (theHandle.entity);
+      return *this;
+    }
+
+#else
+
     //! Upcast to const reference to base type.
-    template <class T2, typename = typename std::enable_if<std::is_base_of<T2, T>::value>::type>
+    template <class T2, typename = typename std::enable_if<is_base_but_not_same<T2, T>::value>::type>
     operator const handle<T2>& () const
     {
       return reinterpret_cast<const handle<T2>&>(*this);
@@ -216,14 +239,39 @@ namespace opencascade {
 
     //! Upcast to non-const reference to base type.
     //! NB: this cast can be dangerous, but required for legacy code; see #26377
-    template <class T2, typename = typename std::enable_if<std::is_base_of<T2, T>::value>::type>
+    template <class T2, typename = typename std::enable_if<is_base_but_not_same<T2, T>::value>::type>
     operator handle<T2>& ()
     {
       return reinterpret_cast<handle<T2>&>(*this);
     }
 
+#endif /* OCCT_HANDLE_NOCAST */
+
 #else /* fallback version for compilers not supporting default arguments of function templates (VC10, VC11, GCC below 4.3) */
 
+#ifdef OCCT_HANDLE_NOCAST
+
+    //! Generalized copy constructor.
+    //! Constructs handle holding entity of base type (T) from the one which holds entity of derived type (T2).
+    template <class T2>
+    handle (const handle<T2>& theHandle, typename std::enable_if <is_base_but_not_same <T, T2>::value>::type* = nullptr) :
+      entity(theHandle.entity)
+    {
+      BeginScope();
+    }
+
+    //! Generalized assignment operator.
+    template <class T2>
+    handle operator = (const handle<T2>& theHandle)
+    {
+      std::enable_if <is_base_but_not_same <T, T2>::value, void*>::type aTypeCheckHelperVar;
+      (void)aTypeCheckHelperVar;
+      Assign (theHandle.entity);
+      return *this;
+    }
+
+#else
+
     //! Upcast to const reference to base type.
     //! NB: this implementation will cause ambiguity errors on calls to overloaded
     //! functions accepting handles to different types, since compatibility is 
@@ -233,7 +281,7 @@ namespace opencascade {
     {
       // error "type is not a member of enable_if" will be generated if T2 is not sub-type of T
       // (handle is being cast to const& to handle of non-base type)
-      return reinterpret_cast<typename std::enable_if<std::is_base_of<T2, T>::value, const handle<T2>&>::type>(*this);
+      return reinterpret_cast<typename std::enable_if<is_base_but_not_same<T2, T>::value, const handle<T2>&>::type>(*this);
     }
 
     //! Upcast to non-const reference to base type.
@@ -244,10 +292,12 @@ namespace opencascade {
     {
       // error "type is not a member of enable_if" will be generated if T2 is not sub-type of T
       // (handle is being cast to const& to handle of non-base type)
-      return reinterpret_cast<typename std::enable_if<std::is_base_of<T2, T>::value, handle<T2>&>::type>(*this);
+      return reinterpret_cast<typename std::enable_if<is_base_but_not_same<T2, T>::value, handle<T2>&>::type>(*this);
     }
 
-#endif
+#endif /* OCCT_HANDLE_NOCAST */
+
+#endif /* compiler switch */
 
   private:
 
index b6cec72f41bd1340e277cbd5ffc8c60b598f0639..29ce2239c9dcc20787c3b4d3079d6dabcd697324 100644 (file)
@@ -68,7 +68,7 @@ void TopOpeBRepBuild_LoopSet::NextLoop()
 //purpose  : 
 //=======================================================================
 
-const Handle(TopOpeBRepBuild_Loop)& TopOpeBRepBuild_LoopSet::Loop() const
+Handle(TopOpeBRepBuild_Loop) TopOpeBRepBuild_LoopSet::Loop() const
 {
   const Handle(TopOpeBRepBuild_Loop)& L = myLoopIterator.Value();
   return L;
index c2c933327f5e83ca1ea8b6df4d86192452d24383..527a4061c434bf5eab9b9e24500147653b696907 100644 (file)
@@ -48,7 +48,7 @@ public:
   
   Standard_EXPORT virtual void NextLoop();
   
-  Standard_EXPORT virtual const Handle(TopOpeBRepBuild_Loop)& Loop() const;
+  Standard_EXPORT virtual Handle(TopOpeBRepBuild_Loop) Loop() const;
 
 
 
index 028026b5bdc04303bd603ea4bee68cd96e3311b1..91e855996f6409ef2bc0a54462478fa9ca814979 100644 (file)
@@ -337,10 +337,9 @@ void  TopOpeBRepBuild_PaveSet::NextLoop()
 //purpose  : 
 //=======================================================================
 
-const Handle(TopOpeBRepBuild_Loop)& TopOpeBRepBuild_PaveSet::Loop()const 
+Handle(TopOpeBRepBuild_Loop) TopOpeBRepBuild_PaveSet::Loop()const 
 {
-  const Handle(TopOpeBRepBuild_Loop)& L = myVerticesIt.Value();
-  return L;
+  return Handle(TopOpeBRepBuild_Loop)(myVerticesIt.Value());
 }
 
 
index 18149b76784ad6009a74e2cdb5d3fc8a4ce3cab2..8915cd1df7604d176c39e9096a3c040bb8ab1813 100644 (file)
@@ -57,7 +57,7 @@ public:
   
   Standard_EXPORT virtual void NextLoop() Standard_OVERRIDE;
   
-  Standard_EXPORT virtual const Handle(TopOpeBRepBuild_Loop)& Loop() const Standard_OVERRIDE;
+  Standard_EXPORT virtual Handle(TopOpeBRepBuild_Loop) Loop() const Standard_OVERRIDE;
   
   Standard_EXPORT const TopoDS_Edge& Edge() const;
   
index 3df9402d3fe2476aea7bea8dbc2e85d919629b16..cc9e0b516ffd2c7809c3fe022d930fd8a92f3e49 100644 (file)
@@ -659,7 +659,7 @@ static IFSelect_ReturnStatus TransferFinder
   dispfiles->SetFinalSelection(slr);
   WS->AddNamedItem ("xst-disp-files",dispfiles);
   Handle(IFSelect_DispPerSignature) dispsign = new IFSelect_DispPerSignature;
-  dispsign->SetSignCounter(new IFSelect_SignCounter(stc));
+  dispsign->SetSignCounter(new IFSelect_SignCounter(Handle(IFSelect_Signature)(stc)));
   dispsign->SetFinalSelection(slr);
   WS->AddNamedItem ("xst-disp-sign",dispsign);
 
index 34d858dcc62c9644a831fdaa2153f0cebc58adcb..45436059d96f28a5726069b32b77a3ac82e155ab 100644 (file)
@@ -45,7 +45,7 @@ const Handle(Standard_Transient)& XmlDrivers::Factory(const Standard_GUID& theGU
 #ifdef OCCT_DEBUG
     cout << "XmlDrivers : Storage Plugin" << endl;
 #endif
-    static Handle(XmlDrivers_DocumentStorageDriver) model_sd =
+    static Handle(Standard_Transient) model_sd =
       new XmlDrivers_DocumentStorageDriver
         ("Copyright: Open Cascade, 2001-2002"); // default copyright
     return model_sd;
@@ -56,7 +56,7 @@ const Handle(Standard_Transient)& XmlDrivers::Factory(const Standard_GUID& theGU
 #ifdef OCCT_DEBUG
     cout << "XmlDrivers : Retrieval Plugin" << endl;
 #endif
-    static Handle (XmlDrivers_DocumentRetrievalDriver) model_rd =
+    static Handle (Standard_Transient) model_rd =
       new XmlDrivers_DocumentRetrievalDriver ();
     return model_rd;
   }
index 4f365b83c20dbe079e113701df3ee1a252354401..dac1ecb1ac3c4cd362e639112237df26a9ed1431 100644 (file)
@@ -45,7 +45,7 @@ const Handle(Standard_Transient)& XmlLDrivers::Factory(const Standard_GUID& theG
 #ifdef OCCT_DEBUG
     cout << "XmlLDrivers : Storage Plugin" << endl;
 #endif
-    static Handle(XmlLDrivers_DocumentStorageDriver) model_sd =
+    static Handle(Standard_Transient) model_sd =
       new XmlLDrivers_DocumentStorageDriver
         ("Copyright: Open Cascade, 2001-2002"); // default copyright
     return model_sd;
@@ -56,7 +56,7 @@ const Handle(Standard_Transient)& XmlLDrivers::Factory(const Standard_GUID& theG
 #ifdef OCCT_DEBUG
     cout << "XmlLDrivers : Retrieval Plugin" << endl;
 #endif
-    static Handle (XmlLDrivers_DocumentRetrievalDriver) model_rd =
+    static Handle (Standard_Transient) model_rd =
       new XmlLDrivers_DocumentRetrievalDriver ();
     return model_rd;
   }
index 443f1f4bfccd63d5867a7fda59d412eb3e41fa9e..e3a42b3ffba4b86afef8c7a2461252285bc26933 100644 (file)
@@ -38,7 +38,7 @@ const Handle(Standard_Transient)& XmlTObjDrivers::Factory(const Standard_GUID& a
 #ifdef OCCT_DEBUG
     cout << "XmlTObjDrivers : Storage Plugin" << endl;
 #endif
-    static Handle(XmlTObjDrivers_DocumentStorageDriver) model_sd
+    static Handle(Standard_Transient) model_sd
       = new XmlTObjDrivers_DocumentStorageDriver
         ("Copyright: Open CASCADE 2004"); // default copyright
     return model_sd;
@@ -49,7 +49,7 @@ const Handle(Standard_Transient)& XmlTObjDrivers::Factory(const Standard_GUID& a
 #ifdef OCCT_DEBUG
     cout << "XmlTObjDrivers : Retrieval Plugin" << endl;
 #endif
-    static Handle (XmlTObjDrivers_DocumentRetrievalDriver) model_rd
+    static Handle (Standard_Transient) model_rd
       = new XmlTObjDrivers_DocumentRetrievalDriver;
     return model_rd;
   }
index 719bdca43af3f2183feae835469619a92ba03348..7cc199c806913647e490a292535ee42fe1a482c2 100644 (file)
@@ -33,7 +33,7 @@ const Handle(Standard_Transient)& XmlXCAFDrivers::Factory(const Standard_GUID& a
 #ifdef OCCT_DEBUG
     cout << "XmlXCAFDrivers : Storage Plugin" << endl;
 #endif
-    static Handle(XmlXCAFDrivers_DocumentStorageDriver) model_sd 
+    static Handle(Standard_Transient) model_sd 
       = new XmlXCAFDrivers_DocumentStorageDriver
         ("Copyright: Open Cascade, 2001-2002"); // default copyright
     return model_sd;
@@ -43,7 +43,7 @@ const Handle(Standard_Transient)& XmlXCAFDrivers::Factory(const Standard_GUID& a
 #ifdef OCCT_DEBUG
     cout << "XmlXCAFDrivers : Retrieval Plugin" << endl;
 #endif
-    static Handle (XmlXCAFDrivers_DocumentRetrievalDriver) model_rd 
+    static Handle (Standard_Transient) model_rd 
       = new XmlXCAFDrivers_DocumentRetrievalDriver;
     return model_rd;
   }