Warnings on vc14 were eliminated
[occt.git] / src / TObj / TObj_Persistence.hxx
CommitLineData
b311480e 1// Created on: 2004-11-23
2// Created by: Pavel TELKOV
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//
d5f74e42 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
973c2be1 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#ifndef TObj_Persistence_HeaderFile
19#define TObj_Persistence_HeaderFile
20
21#include <TObj_Container.hxx>
22
23class TDF_Label;
7fd59977 24
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
29*
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
33*/
34
35class TObj_Persistence
36{
37public:
38 /**
39 * Public methods, to be called externally
40 */
41
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);
47
48 //! Dumps names of all the types registered for persistence to the
49 //! specified stream
50 static Standard_EXPORT void DumpTypes (Standard_OStream& theOs);
51
52protected:
53 /**
54 * Protected methods, to be used or defined by descendants
55 */
56
57 //! The constructor registers the object
58 Standard_EXPORT TObj_Persistence (const Standard_CString theType);
59
60 //! The destructor unregisters the object
61 virtual Standard_EXPORT ~TObj_Persistence ();
62
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;
67
68 //! Dictionary storing all the registered types. It is implemented as static
69 //! variable inside member function in order to ensure initialization
70 //! at first call
71 static Standard_EXPORT TObj_DataMapOfStringPointer& getMapOfTypes();
72
73 private:
74 Standard_CString myType; //!< Name of managed type (recorded for unregistering)
75};
76
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
80#ifdef SOLARIS
81//! Workaround on SUN to avoid stupid warnings
82#define _TOBJOCAF_PERSISTENCE_ACCESS_ public:
83#else
84#define _TOBJOCAF_PERSISTENCE_ACCESS_
85#endif
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 */ \
90 \
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 */ \
96 public: \
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 */ \
100 }; \
101 friend class Persistence_; \
102 static Persistence_ myPersistence_; /* Static field implementing persistsnce tool */
103
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); \
110 }
111
112#endif
113
114#ifdef _MSC_VER
115#pragma once
116#endif