H3D API  2.4.1
ProtoDeclaration.h
Go to the documentation of this file.
1 // Copyright 2004-2019, SenseGraphics AB
3 //
4 // This file is part of H3D API.
5 //
6 // H3D API is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // H3D API is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with H3D API; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // A commercial license is also available. Please contact us at
21 // www.sensegraphics.com for more information.
22 //
23 //
27 //
29 
30 #ifndef __PROTODECLARATION_H__
31 #define __PROTODECLARATION_H__
32 
34 #include <H3D/PrototypeInstance.h>
35 #include <H3D/DEFNodes.h>
36 #include <list>
37 
38 namespace H3D {
39  namespace X3D {
40  class PrototypeVector;
41  }
42 
53  class H3DAPI_API ProtoDeclaration : public RefCountedClass {
54  public:
57  class H3DAPI_API FieldDeclaration {
58  public:
59  FieldDeclaration( const string &_name = "",
60  const X3DTypes::X3DType &_type = X3DTypes::UNKNOWN_X3D_TYPE,
61  const Field::AccessType &_access_type = Field::INPUT_OUTPUT,
62  const string &_value = "" ) :
63  name( _name ),
64  type( _type ),
65  access_type( _access_type ),
66  value( _value ),
67  external( false ) {
68 
69  }
70 
71  string name;
72  X3DTypes::X3DType type;
73  Field::AccessType access_type;
74  string value;
75  bool external;
76  };
77 
85  ProtoDeclaration( const string &_name,
86  const string &_body = "",
87  const vector<string > &_body_extra = vector<string>(),
88  X3D::PrototypeVector *_existing_protos = NULL );
89 
90  // destructor
91  virtual ~ProtoDeclaration();
92 
94  const string &getProtoBody() {
95  return body;
96  }
97 
99  void setProtoBody( const string &_body ) {
100  body = _body;
101  }
102 
105  void addProtoBodyExtra( const string &_body ) {
106  body_extra.push_back( _body );
107  }
108 
110  const vector<string> & getProtoBodyExtra( ) {
111  return body_extra;
112  }
113 
115  const string& getName() {
116  return name;
117  }
118 
120  void setName( const string &_name ) {
121  name = _name;
122  }
123 
131  string addFieldDeclaration( const string &_name,
132  const X3DTypes::X3DType &type,
133  const Field::AccessType &access_type,
134  const string &value = "" ) {
135  FieldDeclaration *field_declaration = getFieldDeclaration( _name );
136  if( field_declaration ) {
137  if( field_declaration->external ) {
138  string error_message = "";
139  if( field_declaration->type != type )
140  error_message =
141  "Warning: Value for \"type\" attribute of \"field\" element with name"
142  + _name + " does not match the value in found ExternProto. Attribute is ignored.";
143  if( error_message == "" &&
144  field_declaration->access_type != access_type )
145  error_message =
146  "Warning: Value for \"access_type\" attribute of \"field\" element with name"
147  + _name + " does not match the value in found ExternProto. Attribute is ignored.";
148  if( value != "" )
149  field_declaration->value = "";
150  // No need to add field declaration. Just indicate that it is no
151  // longer defined in external proto.
152  field_declaration->external = false;
153  return error_message;
154  } else {
155  return "\"field\" element with name " + _name
156  + " already exists. The field will not be added.";
157  }
158  }
159  field_declarations.push_back( FieldDeclaration(_name, type, access_type, value ) );
160  return "";
161  }
162 
165  FieldDeclaration *getFieldDeclaration( const string& _name ) {
166  for( list< FieldDeclaration >::iterator i = field_declarations.begin();
167  i != field_declarations.end(); ++i ) {
168  if( (*i).name == _name ) {
169  return &(*i);
170  }
171  }
172  return NULL;
173  }
174 
179  void setFieldDeclarationsExternal( bool is_external ) {
180  for( list< FieldDeclaration >::iterator i = field_declarations.begin();
181  i != field_declarations.end(); ++i ) {
182  (*i).external = is_external;
183  }
184  }
185 
187  X3DPrototypeInstance *newProtoInstance();
188  protected:
189  string name;
190  // The main body string, i.e. the first node in the proto body.
191  string body;
192 
193  // The extra body string, i.e. all nodes on the same level
194  // as the main body but defined after it.
195  vector< string > body_extra;
196  std::list< FieldDeclaration > field_declarations;
197 
198  // Contains ProtoDeclarations that existed when this protodeclaration was
199  // created.
200  X3D::PrototypeVector *existing_protos;
201 
202  AutoRef< Node > createProtoInstanceNodeX3D( PrototypeInstance *proto,
203  X3D::DEFNodes *dn,
204  const string &body_string );
205 
206  AutoRef< Node > createProtoInstanceNodeVRML( PrototypeInstance *proto,
207  X3D::DEFNodes *dn,
208  const string &body_string );
209  };
210 
211 }
212 
213 #endif
Contains the DEFNodes class.
Header file for PrototypeInstance, X3D scene-graph node.
Header file for X3DPrototypeInstance, X3D scene-graph node.
AccessType
The different access types that a field can have.
Definition: Field.h:53
@ INPUT_OUTPUT
No restrictions on access to the field values or routes.
Definition: Field.h:69
Class that contains information about a field that is part of the prototype declaration.
Definition: ProtoDeclaration.h:57
The ProtoDeclaration class contains information about a PROTO node from the X3D standard.
Definition: ProtoDeclaration.h:53
FieldDeclaration * getFieldDeclaration(const string &_name)
Get the field declaration of the given name.
Definition: ProtoDeclaration.h:165
const vector< string > & getProtoBodyExtra()
Get the proto body extras.
Definition: ProtoDeclaration.h:110
const string & getName()
Get the name of the prototype.
Definition: ProtoDeclaration.h:115
void addProtoBodyExtra(const string &_body)
Add one part of proto body that is not the main proto body(i.e.
Definition: ProtoDeclaration.h:105
void setName(const string &_name)
Get the name of the prototype.
Definition: ProtoDeclaration.h:120
void setProtoBody(const string &_body)
Set the string with the internal scenegraph of the prototype.
Definition: ProtoDeclaration.h:99
string addFieldDeclaration(const string &_name, const X3DTypes::X3DType &type, const Field::AccessType &access_type, const string &value="")
Add a field to the prototype.
Definition: ProtoDeclaration.h:131
const string & getProtoBody()
Get the string with the internal scenegraph of the prototype.
Definition: ProtoDeclaration.h:94
void setFieldDeclarationsExternal(bool is_external)
Call this to set the external property for all contained field declarations.
Definition: ProtoDeclaration.h:179
The PrototypeInstance is an instance of a prototyped node.
Definition: PrototypeInstance.h:43
This abstract node type is the base type for all prototype instances in the X3D system.
Definition: X3DPrototypeInstance.h:65
Provides a mapping between defined DEF names in X3D and the nodes they refer to.
Definition: DEFNodes.h:92
H3D API namespace.
Definition: Anchor.h:38