1 // Created on: 2004-11-23
2 // Created by: Pavel TELKOV
3 // Copyright (c) 2004-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 // The original implementation Copyright: (C) RINA S.p.A
18 #ifndef TObj_Persistence_HeaderFile
19 #define TObj_Persistence_HeaderFile
21 #include <TObj_Container.hxx>
25 /** This class is intended to be a root of tools (one per class)
26 * to manage persistence of objects inherited from TObj_Object
27 * It provides a mechanism to recover correctly typed
28 * objects (subtypes of TObj_Object) out of their persistent names
30 * This is a special kind of object, it automatically registers itself
31 * in a global map when created, and the only thing it does is to
32 * create a new object of the type that it manages, by request
35 class TObj_Persistence
39 * Public methods, to be called externally
42 //! Creates and returns a new object of the registered type
43 //! If the type is not registered, returns Null handle
44 static Standard_EXPORT Handle(TObj_Object) CreateNewObject
45 (const Standard_CString theType,
46 const TDF_Label& theLabel);
48 //! Dumps names of all the types registered for persistence to the
50 static Standard_EXPORT void DumpTypes (Standard_OStream& theOs);
54 * Protected methods, to be used or defined by descendants
57 //! The constructor registers the object
58 Standard_EXPORT TObj_Persistence (const Standard_CString theType);
60 //! The destructor unregisters the object
61 virtual Standard_EXPORT ~TObj_Persistence ();
63 //! The method must be redefined in the derived class and return
64 //! new object of the proper type
65 virtual Standard_EXPORT Handle(TObj_Object) New
66 (const TDF_Label& theLabel) const = 0;
68 //! Dictionary storing all the registered types. It is implemented as static
69 //! variable inside member function in order to ensure initialization
71 static Standard_EXPORT TObj_DataMapOfStringPointer& getMapOfTypes();
74 Standard_CString myType; //!< Name of managed type (recorded for unregistering)
77 //! Declare subclass and methods of the class inherited from TObj_Object
78 //! necessary for implementation of persistence
79 //! This declaration should be put inside class declaration, under 'protected' modifier
81 //! Workaround on SUN to avoid stupid warnings
82 #define _TOBJOCAF_PERSISTENCE_ACCESS_ public:
84 #define _TOBJOCAF_PERSISTENCE_ACCESS_
86 #define DECLARE_TOBJOCAF_PERSISTENCE(name,ancestor) \
87 name (const TObj_Persistence *p, \
88 const TDF_Label& aLabel) : ancestor(p,aLabel) \
89 { initFields(); } /* give the object a chance to initialize its fields */ \
91 /* Creates an object of a proper type */ \
92 /* First argument is used just to avoid possible conflict with other constructors */ \
93 _TOBJOCAF_PERSISTENCE_ACCESS_ \
94 class Persistence_ : public TObj_Persistence { \
95 /* Friend private class of name, is a tool providing persistence */ \
97 Persistence_ () : TObj_Persistence(#name) {} /* register the tool */ \
98 virtual Handle(TObj_Object) New (const TDF_Label& aLabel) const; \
99 /* Creates an object of a proper type */ \
101 friend class Persistence_; \
102 static Persistence_ myPersistence_; /* Static field implementing persistsnce tool */
104 //! Implement mechanism for registration the type for persistence
105 //! This should not be used for abstract classes (while DECLARE should)
106 #define IMPLEMENT_TOBJOCAF_PERSISTENCE(name) \
107 name::Persistence_ name::myPersistence_; \
108 Handle(TObj_Object) name::Persistence_::New (const TDF_Label& aLabel) const { \
109 return new name((const TObj_Persistence*)0, aLabel); \