FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
TestJSAPI.h
1 /**********************************************************\
2 Original Author: Richard Bateman (taxilian)
3 
4 Created: Oct 1, 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 Richard Bateman, Firebreath development team
13 \**********************************************************/
14 
15 #ifndef H_TESTOBJECTJSAPI
16 #define H_TESTOBJECTJSAPI
17 
18 #include "JSAPISimple.h"
19 
20 #define TESTOBJECTJSAPI_ACCESSLISTLENGTH 15
21 
22 class TestObjectJSAPI : public FB::JSAPISimple
23 {
24 public:
25  TestObjectJSAPI()
26  {
27  registerMethod("setValue", (FB::CallMethodPtr)&TestObjectJSAPI::callSetValue);
28  registerMethod("getValue", (FB::CallMethodPtr)&TestObjectJSAPI::callGetValue);
29  registerMethod("invalidate", (FB::CallMethodPtr)&TestObjectJSAPI::callInvalidate);
30 
31  registerProperty("value", (FB::GetPropPtr)&TestObjectJSAPI::getValue,
32  (FB::SetPropPtr)&TestObjectJSAPI::setValue);
33  }
34 
35  virtual ~TestObjectJSAPI()
36  {
37  }
38 
39 public:
40  FB::variant callSetValue(const std::vector<FB::variant>& args)
41  {
42  if (args.size() != 2)
43  throw FB::invalid_arguments();
44 
45  try {
46  if (args[0].convert_cast<long>() >= 0
47  || args[0].convert_cast<long>() < TESTOBJECTJSAPI_ACCESSLISTLENGTH)
48  {
49  int i = args[0].convert_cast<long>();
50  m_accessList[i] = args[1];
51  std::string tmp = args[1].convert_cast<std::string>();
52  }
53  return true;
54  } catch (...) {
55  throw FB::script_error("Could not convert parameter");
56  }
57  }
58 
59  FB::variant callGetValue(const std::vector<FB::variant>& args)
60  {
61  if (args.size() != 1)
62  throw FB::invalid_arguments();
63 
64  try {
65  if (args[0].convert_cast<long>() >= 0
66  || args[0].convert_cast<long>() < TESTOBJECTJSAPI_ACCESSLISTLENGTH)
67  {
68  int i = args[0].convert_cast<long>();
69  return m_accessList[i];
70  }
71  } catch (...) {
72  throw FB::script_error("Could not convert parameter");
73  }
74 
75  throw FB::invalid_arguments();
76  }
77 
78  FB::variant callInvalidate(const std::vector<FB::variant>& args)
79  {
80  invalidate();
81  return FB::variant();
82  }
83 
84  FB::variant getValue()
85  {
86  return propValue;
87  }
88 
89  void setValue(const FB::variant& value)
90  {
91  propValue = value;
92  }
93 
94 protected:
95  FB::variant m_accessList[TESTOBJECTJSAPI_ACCESSLISTLENGTH];
96  FB::variant propValue;
97 };
98 
99 #endif
100 
Simple JSAPI implementation for those who for whatever reason don't want to use JSAPIAuto.
Definition: JSAPISimple.h:31
virtual void registerMethod(const std::string &name, CallMethodPtr func)
Register a method to be exposed to javascript.
Definition: JSAPISimple.cpp:80
void invalidate()
Invalidates this object.
Definition: JSAPIImpl.cpp:42
Accepts any datatype, used in all interactions with javascript. Provides tools for getting back out t...
Definition: variant.h:198
Thrown by a JSAPI object when the argument(s) provided to a SetProperty or Invoke call are found to b...
Definition: JSExceptions.h:47
variant(JSAPI::* CallMethodPtr)(const std::vector< variant > &)
Defines an alias representing a function ptr for a method on a FB::JSAPISimple object.
Definition: APITypes.h:242
void(JSAPI::* SetPropPtr)(const variant &value)
Defines an alias representing a function pointer for a property setter on a FB::JSAPISimple object...
Definition: APITypes.h:260
Exception type; when thrown in a JSAPI method, a javascript exception will be thrown.
Definition: JSExceptions.h:28
variant(JSAPI::* GetPropPtr)()
Defines an alias representing a function pointer for a property getter on a FB::JSAPISimple object...
Definition: APITypes.h:258
virtual void registerProperty(const std::string &name, GetPropPtr getFunc, SetPropPtr setFunc)
Register a property to be exposed to javascript.
Definition: JSAPISimple.cpp:85