FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
ScriptingCore/DOM/Node.h
1 /**********************************************************\
2 Original Author: Richard Bateman (taxilian)
3 
4 Created: Dec 9, 2009
5 License: Dual license model; choose one of two:
6  New BSD License
7  http://www.opensource.org/licenses/bsd-license.php
8  - or -
9  GNU Lesser General Public License, version 2.1
10  http://www.gnu.org/licenses/lgpl-2.1.html
11 
12 Copyright 2009 PacketPass, Inc and the Firebreath development team
13 \**********************************************************/
14 
15 #pragma once
16 #ifndef H_FB_DOM_NODE
17 #define H_FB_DOM_NODE
18 
19 #include <string>
20 #include <boost/enable_shared_from_this.hpp>
21 #include "JSObject.h"
22 
23 namespace FB { namespace DOM {
29  class Node;
30  typedef boost::shared_ptr<Node> NodePtr;
31 
39  class Node : public boost::enable_shared_from_this<Node>
40  {
41  public:
42  Node(const JSObjectPtr& element) : m_element(element) { }
43  virtual ~Node() { }
44 
52  virtual FB::JSObjectPtr getJSObject() const { return m_element; }
53 
61  NodePtr node() { return shared_from_this(); }
62 
74  static NodePtr create(const FB::JSObjectPtr &api) {
75  if (!api) { return NodePtr(); }
76  return api->getHost()->_createNode(api);
77  }
78 
79  public:
95  template <class T>
96  T callMethod(const std::string& name, const VariantList& args) const
97  {
98  FB::variant tmp = m_element->Invoke(name, args);
99  return tmp.convert_cast<T>();
100  }
104  template <class T>
105  T callMethod(const std::wstring& name, const VariantList& args) const
106  {
107  return callMethod<T>(FB::wstring_to_utf8(name), args);
108  }
109 
120  bool hasProperty(const std::string& propertyName) const {
121  return m_element->HasProperty(propertyName);
122  }
123 
125  template <class T>
126  T getProperty(const std::wstring& name) const
127  {
128  return getProperty<T>(FB::wstring_to_utf8(name));
129  }
144  template <class T>
145  T getProperty(const std::string& name) const
146  {
147  FB::variant tmp = m_element->GetProperty(name);
148  return tmp.convert_cast<T>();
149  }
150 
161  template <class T>
162  T getProperty(const int idx) const
163  {
164  FB::variant tmp = m_element->GetProperty(idx);
165  return tmp.convert_cast<T>();
166  }
167 
169  virtual NodePtr getNode(const std::wstring& name) const;
179  virtual NodePtr getNode(const std::string& name) const;
180 
190  virtual NodePtr getNode(const int idx) const;
191 
193  virtual void setProperty(const std::wstring& name, const variant& val) const;
204  virtual void setProperty(const std::string& name, const variant& val) const;
205 
207  virtual void setProperty(const int idx, const variant& val) const;
208 
209  virtual NodePtr appendChild(NodePtr node);
210 
211  protected:
212  JSObjectPtr m_element; // The JSObject that this object wraps
213  };
214 
215 }; };
216 
217 #endif // H_FB_DOM_NODE
218 
DOM Node wrapper.
static NodePtr create(const FB::JSObjectPtr &api)
Creates a FB::DOM::Node object from a JSObjectPtr representing a DOM node. This will probably throw a...
boost::shared_ptr< FB::JSObject > JSObjectPtr
Defines an alias representing a JSObject shared_ptr (you should never use a JSObject* directly) ...
Definition: APITypes.h:109
virtual NodePtr getNode(const std::wstring &name) const
const T convert_cast() const
Converts the stored value to the requested type if possible and returns the resulting value...
Definition: variant.h:667
boost::shared_ptr< Node > NodePtr
shared_ptr for a FB::DOM::Node
Accepts any datatype, used in all interactions with javascript. Provides tools for getting back out t...
Definition: variant.h:198
virtual void setProperty(const std::wstring &name, const variant &val) const
std::vector< variant > VariantList
Defines an alias representing list of variants.
Definition: APITypes.h:64
std::string wstring_to_utf8(const std::wstring &src)
Accepts a std::wstring and returns a UTF8-encoded std::string.
Definition: utf8_tools.cpp:37
virtual FB::JSObjectPtr getJSObject() const
Returns the FB::JSObjectPtr that this object is wrapping.
NodePtr node()
Gets the NodePtr for the current node.
T getProperty(const std::wstring &name) const