- Page restrictions apply
- Added by Automation System, last edited by Automation System on Oct 11, 2012 (view change)
JSAPISimple.cpp
00001 /**********************************************************\ 00002 Original Author: Richard Bateman (taxilian) 00003 00004 Created: Oct 29, 2009 00005 License: Dual license model; choose one of two: 00006 New BSD License 00007 http://www.opensource.org/licenses/bsd-license.php 00008 - or - 00009 GNU Lesser General Public License, version 2.1 00010 http://www.gnu.org/licenses/lgpl-2.1.html 00011 00012 Copyright 2009 Richard Bateman, Firebreath development team 00013 \**********************************************************/ 00014 00015 #include "precompiled_headers.h" // On windows, everything above this line in PCH 00016 #include "JSAPISimple.h" 00017 00018 using namespace FB; 00019 JSAPISimple::JSAPISimple(void) : m_allowRemoveProperty(false) 00020 { 00021 registerMethod( "toString", (CallMethodPtr)&JSAPISimple::callToString ); 00022 registerMethod( "testEvent", (CallMethodPtr)&JSAPISimple::callFireEvent ); 00023 00024 registerProperty( "valid", (GetPropPtr)&JSAPISimple::getValid, NULL ); 00025 00026 00027 } 00028 00029 JSAPISimple::~JSAPISimple(void) 00030 { 00031 } 00032 00033 00034 variant JSAPISimple::callFireEvent(const std::vector<FB::variant>& args_in) 00035 { 00036 std::vector<FB::variant> args(args_in); 00037 00038 try { 00039 std::string event = args[0].convert_cast<std::string>(); 00040 args.erase(args.begin()); 00041 this->FireEvent(event, args); 00042 return event; 00043 } catch (...) { 00044 throw invalid_arguments(); 00045 } 00046 } 00047 00048 // Example function call and read-only property; override these if desired in derived classes 00049 variant JSAPISimple::callToString(const std::vector<FB::variant>& args) 00050 { 00051 return "JSAPI Javascript Object"; 00052 } 00053 00054 variant JSAPISimple::getValid() 00055 { 00056 return m_valid; 00057 } 00058 00059 void JSAPISimple::getMemberNames(std::vector<std::string> &nameVector) const 00060 { 00061 nameVector.clear(); 00062 00063 for (FB::MethodMap::const_iterator it = m_methodMap.begin(); 00064 it != m_methodMap.end(); ++it) { 00065 nameVector.push_back(it->first); 00066 } 00067 for (FB::PropertyMap::const_iterator it = m_propertyMap.begin(); 00068 it != m_propertyMap.end(); ++it) { 00069 nameVector.push_back(it->first); 00070 } 00071 } 00072 00073 size_t JSAPISimple::getMemberCount() const 00074 { 00075 return m_methodMap.size() 00076 + m_propertyMap.size(); 00077 } 00078 00079 // Methods for registering properties and functions to the auto-table 00080 void JSAPISimple::registerMethod(const std::string& name, CallMethodPtr func) 00081 { 00082 m_methodMap[name].callFunc = func; 00083 } 00084 00085 void JSAPISimple::registerProperty(const std::string& name, GetPropPtr getFunc, SetPropPtr setFunc) 00086 { 00087 m_propertyMap[name].getFunc = getFunc; 00088 m_propertyMap[name].setFunc = setFunc; 00089 } 00090 00091 // Methods to query existance of members on the API 00092 bool JSAPISimple::HasMethod(const std::string& methodName) const 00093 { 00094 if (!m_valid) 00095 return false; 00096 00097 MethodMap::const_iterator fnd = m_methodMap.find(methodName); 00098 if (fnd != m_methodMap.end()) { 00099 return true; 00100 } else { 00101 return false; 00102 } 00103 } 00104 00105 bool JSAPISimple::HasProperty(const std::string& propertyName) const 00106 { 00107 if (!m_valid) 00108 return false; 00109 00110 PropertyMap::const_iterator fnd = m_propertyMap.find(propertyName); 00111 if (fnd != m_propertyMap.end()) { 00112 return true; 00113 } else { 00114 return false; 00115 } 00116 return false; 00117 } 00118 00119 00120 // Methods to manage properties on the API 00121 variant JSAPISimple::GetProperty(const std::string& propertyName) 00122 { 00123 if (!m_valid) 00124 throw object_invalidated(); 00125 00126 PropertyMap::const_iterator fnd = m_propertyMap.find(propertyName); 00127 if (fnd != m_propertyMap.end() && fnd->second.getFunc != NULL) { 00128 return (this->*fnd->second.getFunc)(); 00129 } else { 00130 throw invalid_member(propertyName); 00131 } 00132 } 00133 00134 void JSAPISimple::SetProperty(const std::string& propertyName, const variant& value) 00135 { 00136 if (!m_valid) 00137 throw object_invalidated(); 00138 00139 PropertyMap::const_iterator fnd = m_propertyMap.find(propertyName); 00140 if (fnd->second.setFunc != NULL) { 00141 (this->*fnd->second.setFunc)(value); 00142 } else { 00143 throw invalid_member(propertyName); 00144 } 00145 } 00146 00147 void JSAPISimple::RemoveProperty(const std::string& propertyName) 00148 { 00149 if (!m_valid) 00150 throw object_invalidated(); 00151 00152 m_propertyMap.erase(propertyName); 00153 } 00154 00155 bool JSAPISimple::HasProperty(int idx) const 00156 { 00157 if (!m_valid) 00158 throw object_invalidated(); 00159 00160 // By default do not support indexing 00161 // To use array style access, override this method in your API object 00162 return false; 00163 } 00164 00165 variant JSAPISimple::GetProperty(int idx) 00166 { 00167 if (!m_valid) 00168 throw object_invalidated(); 00169 00170 // By default do not support indexing 00171 // To use array style access, override this method in your API object 00172 throw invalid_member("Array index: " + variant(idx).convert_cast<std::string>()); 00173 } 00174 00175 void JSAPISimple::SetProperty(int idx, const variant& value) 00176 { 00177 if (!m_valid) 00178 throw object_invalidated(); 00179 00180 // By default do not support indexing 00181 // To use array style access, override this method in your API object 00182 throw invalid_member("Array index: " + variant(idx).convert_cast<std::string>()); 00183 } 00184 00185 void JSAPISimple::RemoveProperty(int idx) 00186 { 00187 if (!m_valid) 00188 throw object_invalidated(); 00189 00190 // By default do not support indexing 00191 // To use array style access, override this method in your API object 00192 throw invalid_member("Array index: " + variant(idx).convert_cast<std::string>()); 00193 } 00194 00195 00196 // Methods to manage methods on the API 00197 variant JSAPISimple::Invoke(const std::string& methodName, const std::vector<FB::variant>& args) 00198 { 00199 if (!m_valid) 00200 throw object_invalidated(); 00201 00202 MethodMap::iterator fnd = m_methodMap.find(methodName); 00203 if (fnd != m_methodMap.end() && fnd->second.callFunc != NULL) { 00204 return (this->*fnd->second.callFunc)(args); 00205 } else { 00206 throw invalid_member(methodName); 00207 } 00208 } 00209 00210 variant JSAPISimple::Construct(const std::vector<FB::variant>& args) 00211 { 00212 if (!m_valid) 00213 throw object_invalidated(); 00214 00215 throw invalid_member("constructor"); 00216 } 00217
Generated on 19 Jun 2013 for FireBreath by
1.6.1
Labels
