summaryrefslogtreecommitdiff
path: root/inc/TObj_Persistence.hxx
blob: 21579124c5b26820e09e4f30fdb362c4274a0657 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// File:        TObj_Persistence.hxx
// Created:     Tue Nov  23 11:36:03 2004
// Author:      Pavel TELKOV
// Copyright:   Open CASCADE  2007
// The original implementation Copyright: (C) RINA S.p.A

#ifndef TObj_Persistence_HeaderFile
#define TObj_Persistence_HeaderFile

#include <TObj_Container.hxx>

class TDF_Label;
class Handle(TObj_Object);

/** This class is intended to be a root of tools (one per class)
*   to manage persistence of objects inherited from TObj_Object
*   It provides a mechanism to recover correctly typed
*   objects (subtypes of TObj_Object) out of their persistent names
*
*   This is a special kind of object, it automatically registers itself
*   in a global map when created, and the only thing it does is to
*   create a new object of the type that it manages, by request
*/

class TObj_Persistence
{
public:
  /**
  * Public methods, to be called externally
  */

  //! Creates and returns a new object of the registered type
  //! If the type is not registered, returns Null handle
  static Standard_EXPORT Handle(TObj_Object) CreateNewObject
                                (const Standard_CString theType,
                                 const TDF_Label& theLabel);

  //! Dumps names of all the types registered for persistence to the
  //! specified stream
  static Standard_EXPORT void DumpTypes (Standard_OStream& theOs);

protected:
  /**
  * Protected methods, to be used or defined by descendants
  */

  //! The constructor registers the object
  Standard_EXPORT TObj_Persistence (const Standard_CString theType);

  //! The destructor unregisters the object
  virtual Standard_EXPORT ~TObj_Persistence ();

  //! The method must be redefined in the derived class and return
  //! new object of the proper type
  virtual Standard_EXPORT Handle(TObj_Object) New
                                (const TDF_Label& theLabel) const = 0;

  //! Dictionary storing all the registered types. It is implemented as static
  //! variable inside member function in order to ensure initialization
  //!  at first call
  static Standard_EXPORT TObj_DataMapOfStringPointer& getMapOfTypes();

 private:
  Standard_CString myType;  //!< Name of managed type (recorded for unregistering)
};

//! Declare subclass and methods of the class inherited from TObj_Object
//! necessary for implementation of persistence
//! This declaration should be put inside class declaration, under 'protected' modifier
#ifdef SOLARIS
//! Workaround on SUN to avoid stupid warnings
#define _TOBJOCAF_PERSISTENCE_ACCESS_ public:
#else
#define _TOBJOCAF_PERSISTENCE_ACCESS_
#endif
#define DECLARE_TOBJOCAF_PERSISTENCE(name,ancestor)                                      \
  name (const TObj_Persistence *p,                                                   \
        const TDF_Label& aLabel) : ancestor(p,aLabel)                                    \
  { initFields(); } /* give the object a chance to initialize its fields */              \
                                                                                         \
    /* Creates an object of a proper type */                                             \
    /* First argument is used just to avoid possible conflict with other constructors */ \
  _TOBJOCAF_PERSISTENCE_ACCESS_                                                          \
  class Persistence_ : public TObj_Persistence {                                     \
    /* Friend private class of name, is a tool providing persistence */                  \
  public:                                                                                \
    Persistence_ () : TObj_Persistence(#name) {} /* register the tool */             \
    virtual Handle(TObj_Object) New (const TDF_Label& aLabel) const;                 \
      /* Creates an object of a proper type */                                           \
  };                                                                                     \
  friend class Persistence_;                                                             \
  static Persistence_ myPersistence_; /* Static field implementing persistsnce tool */

//! Implement mechanism for registration the type for persistence
//! This should not be used for abstract classes (while DECLARE should)
#define IMPLEMENT_TOBJOCAF_PERSISTENCE(name)                                             \
  name::Persistence_ name::myPersistence_;                                               \
  Handle(TObj_Object) name::Persistence_::New (const TDF_Label& aLabel) const {      \
    return new name((const TObj_Persistence*)0, aLabel);                             \
  }

#endif

#ifdef _MSC_VER
#pragma once
#endif