HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
VOP_TypeDefinitionFile.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: VOP_TypeDefinitionFile.h ( VOP Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __VOP_TypeDefinitionFile__
12 #define __VOP_TypeDefinitionFile__
13 
14 #include "VOP_API.h"
16 #include <OP/OP_OperatorTable.h> // for base classes
17 #include <UT/UT_ValArray.h>
18 #include <UT/UT_StringMap.h>
19 #include <UT/UT_Functor.h>
21 
22 // ============================================================================
23 /// Function that creates an instance of a type definition subclass,
24 /// and a dictionary that maps a definition metatype names to such functions.
27 
28 // ============================================================================
29 /// Represents a source of vop type definitions saved in files or inside HDAs.
31 {
32 public:
33  /// Constructor.
34  /// Note, still need to invoke load() before the object can provide info
35  ///about the types defined in that file.
36  VOP_TypeDefinitionFile( const char *file_path );
37 
38  /// Gets the definition source file path.
39  virtual void getSourcePath( UT_String &path ) const;
40  const UT_String & getFilePath() const;
41 
42  /// Loads the necessary data from the file before this class provide
43  /// information about type definitions from that file.
44  bool load( const VOP_TypeDefinitionFactoryMap &factory_map,
45  UT_String &errors );
46 
47  /// Saves the current data into the file.
48  bool save( UT_String &errors ) const;
49 
50  /// Returns a type definition object found in the file at load time.
52  getTypeDefinition( const char *type_name );
53 
54 
55  /// Obtains the list of type definition names provided by this source.
56  void getTypeDefinitionNames( UT_StringArray &type_names ) const;
57 
58  /// Add a new definition to this source.
59  void addDefinition( VOP_TypeDefinitionHandle type_handle );
60 
61  /// Update an old or add a new definition to this source.
62  void updateOrAddDefinition( const char *type_name,
63  VOP_TypeDefinitionHandle type_handle );
64 
65  /// Delete the definition from the source.
66  bool removeDefinition( const char *type_name );
67 
68  /// @{ Tests some characteristics of the disk file this source represents.
69  bool exists() const;
70  bool canRead() const;
71  bool canWrite() const;
72  /// @}
73 
74  /// @{ Allows editing of types it provides in the default editor.
75  void setAllowsEditingOfTypes( bool flag );
76  bool allowsEditingOfTypes() const;
77  /// @}
78 
79 private:
80  bool loadJSON( const VOP_TypeDefinitionFactoryMap &factory_map,
82  UT_String &errors );
83  bool saveJSON( const UT_Array<VOP_TypeDefinitionHandle> &
84  typedefs ) const;
85 
86  /// Find a handle pointer within the definition handle list.
87  VOP_TypeDefinitionHandle * findTypeHandle( const char *type_name );
88  int findTypeHandleIndex( const char *type_name );
89 
90 private:
91  /// The file path.
92  UT_String myFilePath;
93 
94  /// Allows editor to modify the types and their source.
95  bool myAllowEditingOfTypes;
96 
97  /// The list of type definition this source provides.
98  UT_Array<VOP_TypeDefinitionHandle> myTypeDefinitions;
99 };
100 
101 
102 // ============================================================================
103 /// A class for handling files and HDAs as sources of vop type definitions.
105  public OP_UpdateTemplatesSink
106 {
107 public:
109  virtual ~VOP_TypeDefinitionFileLoader();
110 
111  /// Gets the file path where the vop types are saved by default.
112  const char * getDefaultSourceFile() const;
113 
114  /// Looks for the definition files in the search path and loads them.
115  void loadDefinitionFilesInSearchPath();
116 
117  /// Loads the definitions from the specified file.
118  bool loadFile( const char *file_path, UT_String &errors );
119 
120  /// Sets a new type definition for a given type name.
121  /// @param old_type_name The original name of the type to redefine.
122  /// This should be NULL, if it's a brand new type.
123  /// If the new definition has a new type name, this
124  /// parameter contains the original (old) type name
125  /// that is being renamed.
126  /// @param type_handle The handle to the type definition object.
127  /// Manager will use it as the new definition for the type.
128  /// The definition must have a valid non-empty type name string.
129  /// @param source_path The file where to save the type definition.
130  /// If the old definition resided in another file, it is moved
131  /// to this new file.
132  /// If NULL, a default file will be used.
133  /// @param errors An outgoing string parameter that will contain
134  /// error messages if manger encounters any problems.
135  void createOrUpdateType( const char *old_type_name,
136  VOP_TypeDefinitionHandle type_handle,
137  const char *source_path,
138  UT_String &errors );
139 
140  /// Deletes the definition from the source.
141  bool deleteType( const char *type_name );
142 
143  /// @{ Registers a functor that creates a data type definition object
144  /// associated with the given meta type name, when such meta type
145  /// is encountered when reading and parsing a JSON file.
146  /// After that the created object is given a chance to load itself from
147  /// the definition JSON value.
148  void registerFactory( const UT_StringHolder &metatype_name,
149  const VOP_TypeDefinitionFactory &factory);
150  void unregisterFactory(const UT_StringHolder &metatype_name);
151  /// @}
152 
153  /// Adds default factories to the map (eg, for "struct" metatype)
154  void registerDefaultFactories();
155 
156  /// Registers interests in tables and ops to monitor HDAs for their
157  /// section that may define vop structs.
158  void registerOperatorInterests();
159 
160  /// @{ Starts and ends a block of code in which the op table updates
161  /// should not be handled. All the pending actions are performed only
162  /// at the time the last endDeferHandlingUpdates() is called.
163  void beginDeferUpdates();
164  void endDeferUpdates();
165  /// @}
166 
167 protected:
168  /// @{ Virtual overrides from the base class. Respectively: monitoring
169  /// for any new HDAs that appear in a table, keeping an eye on existing HDAs
170  /// for anu updates, and making sure we handle deleted HDA operator types.
171  virtual void tableUpdated(OP_OperatorTable *table);
172  virtual void templatesUpdated( OP_Operator *op );
173  virtual void operatorDeleted( OP_Operator *op );
174  /// @}
175 
176 private:
177  /// Helper method that handles an update or removal of operator.
178  void updateDefinitionsAndSourcesFromOp( const char *op_name,
179  VOP_TypeDefinitionFile *old_src,
180  VOP_TypeDefinitionFile *new_src );
181 
182 
183  /// Scans the search path and loads all the definition files found there.
184  void loadDefinitionFiles( const char *file_ext );
185 
186  /// Create info objects for the definitions provided by the loaded source.
187  void registerLoadedDefinitions(
189  bool takes_precedence );
190 
191  /// @{
192  /// Helper methods to find and create a vop type definition srouce
193  /// corresponding to a disk file.
194  /// When takes_precedence is true, any newly loaded type definitions
195  /// that conflict with existing types will take precedence; otherwise,
196  /// the existing definitions will remain the ones that define the type.
197  bool hasSource( VOP_TypeDefinitionFile *src ) const;
198  VOP_TypeDefinitionFile * findSource( const char *path );
199  VOP_TypeDefinitionFile * createAndLoadSource(
200  const char *path,
201  bool takes_precedence,
202  UT_String *errors = NULL );
203  /// @}
204 
205 private:
206  /// The manager in which we register types defined in files.
207  VOP_LanguageManager & myOwnerMgr;
208 
209  /// The default source file path.
210  UT_String myDefaultFileSource;
211 
212  /// The known sources of type definitions.
214 
215  /// Dictionary that maps an op to a definitions source managed by loader.
217 
218  /// The map from a definition metatype name (eg "struct") to a function
219  /// callback that creates an instance of VOP_TypeDefinition subclass
220  /// implementing that type.
221  VOP_TypeDefinitionFactoryMap myTypeDefinitionFactoryMap;
222 
223  /// List of deferred op tables with pending update.
224  UT_Set<OP_OperatorTable*> myDeferredUpdateTables;
225 
226  /// Nesting level of deferred update handling.
227  int myDeferUpdateCounter;
228 };
229 
230 
231 // ============================================================================
232 /// A class for to postpone reloading of VOP type definitions.
233 /// Sometimes (eg, on startup) there is a slew of HDA load events, each may
234 /// try to reload and update definitions, but reloading can be safely
235 /// performed only once, on the last update, after all HDAs are available.
236 /// An instance of this class postpones updates until it goes out of scope.
237 /// When the last instance goes out of scope, all pending updates are performed.
239 {
240 public:
243 };
244 
245 #endif
246 
virtual void templatesUpdated(OP_Operator *op)=0
UT_Functor< VOP_TypeDefinition * > VOP_TypeDefinitionFactory
GLsizei const GLchar *const * path
Definition: glcorearb.h:3340
virtual void getSourcePath(UT_String &path) const
A class for managing the languages and custom data types in VOPs.
A generic source of the vop (connector wire) type definitions.
UT_StringMap< VOP_TypeDefinitionFactory > VOP_TypeDefinitionFactoryMap
#define VOP_API
Definition: VOP_API.h:10
virtual VOP_TypeDefinitionHandle getTypeDefinition(const char *type_name)=0
Returns a type definition provided by this source.
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:802
virtual void tableUpdated(OP_OperatorTable *table)=0
A class for handling files and HDAs as sources of vop type definitions.
virtual void operatorDeleted(OP_Operator *op)
Definition: OP_Operator.h:904
Represents a source of vop type definitions saved in files or inside HDAs.
png_infop png_uint_32 flag
Definition: png.h:2242
UT_SharedPtr< const VOP_TypeDefinition > VOP_TypeDefinitionHandle
Define a handle for the type definition objects.
GLenum src
Definition: glcorearb.h:1792