Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   Related Pages  

script.h

00001 //
00002 // OpenVRML
00003 //
00004 // Copyright (C) 1998  Chris Morley
00005 // Copyright (C) 2001  Braden McDaniel
00006 // 
00007 // This library is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 2.1 of the License, or (at your option) any later version.
00011 // 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 // Lesser General Public License for more details.
00016 // 
00017 // You should have received a copy of the GNU Lesser General Public
00018 // License along with this library; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //
00021 
00022 # ifndef OPENVRML_SCRIPT_H
00023 #   define OPENVRML_SCRIPT_H
00024 
00025 #   include "fieldvalueptr.h"
00026 #   include "node.h"
00027 
00028 namespace OpenVRML {
00029 
00030     class FieldValue;
00031     class ScriptNode;
00032 
00033     class OPENVRML_SCOPE Script {
00034     public:
00035         virtual ~Script() = 0;
00036         virtual void initialize(double timestamp) = 0;
00037         virtual void processEvent(const std::string & id,
00038                                   const FieldValue & value,
00039                                   double timestamp) = 0;
00040         virtual void eventsProcessed(double timestamp) = 0;
00041         virtual void shutdown(double timestamp) = 0;
00042 
00043     protected:
00044         ScriptNode & scriptNode;
00045 
00046         Script(ScriptNode & scriptNode);
00047 
00048     private:
00049         // non-copyable
00050         Script(const Script &);
00051         Script & operator=(const Script &);
00052     };
00053     
00054     
00055     class OPENVRML_SCOPE ScriptNodeClass : public NodeClass {
00056     public:
00057         ScriptNodeClass(Browser & browser);
00058         virtual ~ScriptNodeClass() throw ();
00059         
00060         virtual const NodeTypePtr createType(const std::string & id,
00061                                              const NodeInterfaceSet &)
00062                 throw ();
00063     };
00064     
00065     
00066     class OPENVRML_SCOPE ScriptNode : public ChildNode {
00067     public:
00068         typedef std::map<std::string, FieldValuePtr> FieldValueMap;
00069         typedef std::map<std::string, PolledEventOutValue> EventOutValueMap;
00070     
00071     private:
00072         class ScriptNodeType : public NodeType {
00073             NodeInterfaceSet interfaces;
00074 
00075         public:
00076             ScriptNodeType(ScriptNodeClass & nodeClass);
00077             virtual ~ScriptNodeType() throw ();
00078 
00079             void addInterface(const NodeInterface & interface)
00080                     throw (std::invalid_argument);
00081 
00082             virtual const NodeInterfaceSet & getInterfaces() const throw ();
00083             virtual const NodePtr createNode(const ScopePtr & scope) const
00084                     throw (std::bad_alloc);
00085         };
00086         
00087         friend class ScriptNodeType;
00088     
00089         ScriptNodeType scriptNodeType;
00090         SFBool directOutput;
00091         SFBool mustEvaluate;
00092         MFString url;
00093         FieldValueMap fieldValueMap;
00094         EventOutValueMap eventOutValueMap;
00095         Script * script;
00096         int eventsReceived;
00097     
00098     public:
00099         ScriptNode(ScriptNodeClass & nodeClass,
00100                    const ScopePtr & scope);
00101         virtual ~ScriptNode() throw ();
00102         
00103         void setUrl(const MFString & value, double timestamp);
00104         const MFString & getUrl() const;
00105         
00106         void addEventIn(FieldValue::Type type, const std::string & id)
00107                 throw (std::invalid_argument, std::bad_alloc);
00108         void addEventOut(FieldValue::Type type, const std::string & id)
00109                 throw (std::invalid_argument, std::bad_alloc);
00110         void addField(const std::string & id,
00111                       const FieldValuePtr & defaultValue)
00112                 throw (std::invalid_argument, std::bad_alloc);
00113         
00114         void initialize(double timestamp);
00115         void update(double timestamp);
00116         void shutdown(double timestamp);
00117         
00118         void setEventOut(const std::string & id, const FieldValue & value)
00119                 throw (UnsupportedInterface, std::bad_cast, std::bad_alloc);
00120         
00121         const FieldValueMap & getFieldValueMap() const throw ();
00122         const EventOutValueMap & getEventOutValueMap() const throw ();
00123         
00124         virtual const ScriptNode * toScript() const throw ();
00125         virtual ScriptNode * toScript() throw ();
00126     
00127     private:
00128         Script * createScript();
00129         
00130         void assignWithSelfRefCheck(const SFNode &, SFNode &) const throw ();
00131         void assignWithSelfRefCheck(const MFNode &, MFNode &) const throw ();
00132         
00133         virtual void initializeImpl(double timestamp) throw (std::bad_alloc);
00134         
00135         virtual void setFieldImpl(const std::string & id,
00136                                   const FieldValue & value)
00137                 throw (UnsupportedInterface, std::bad_cast, std::bad_alloc);
00138         
00139         virtual const FieldValue & getFieldImpl(const std::string & id) const
00140                 throw (UnsupportedInterface);
00141         
00142         virtual void processEventImpl(const std::string & id,
00143                                       const FieldValue & value,
00144                                       double timestamp)
00145                 throw (UnsupportedInterface, std::bad_cast, std::bad_alloc);
00146         
00147         virtual const FieldValue & getEventOutImpl(const std::string & id) const
00148                 throw (UnsupportedInterface);
00149     };
00150     
00151     inline const ScriptNode::FieldValueMap &
00152             ScriptNode::getFieldValueMap() const throw () {
00153         return this->fieldValueMap;
00154     }
00155     
00156     inline const ScriptNode::EventOutValueMap &
00157             ScriptNode::getEventOutValueMap() const throw () {
00158         return this->eventOutValueMap;
00159     }
00160 }
00161 
00162 # endif