Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef NAN_MAYBE_PRE_43_INL_H_
00010 #define NAN_MAYBE_PRE_43_INL_H_
00011
00012 template<typename T>
00013 class MaybeLocal {
00014 public:
00015 inline MaybeLocal() : val_(v8::Local<T>()) {}
00016
00017 template<typename S>
00018 # if NODE_MODULE_VERSION >= NODE_0_12_MODULE_VERSION
00019 inline MaybeLocal(v8::Local<S> that) : val_(that) {}
00020 # else
00021 inline MaybeLocal(v8::Local<S> that) :
00022 val_(*reinterpret_cast<v8::Local<T>*>(&that)) {}
00023 # endif
00024
00025 inline bool IsEmpty() const { return val_.IsEmpty(); }
00026
00027 template<typename S>
00028 inline bool ToLocal(v8::Local<S> *out) const {
00029 *out = val_;
00030 return !IsEmpty();
00031 }
00032
00033 inline v8::Local<T> ToLocalChecked() const {
00034 #if defined(V8_ENABLE_CHECKS)
00035 assert(!IsEmpty() && "ToLocalChecked is Empty");
00036 #endif // V8_ENABLE_CHECKS
00037 return val_;
00038 }
00039
00040 template<typename S>
00041 inline v8::Local<S> FromMaybe(v8::Local<S> default_value) const {
00042 return IsEmpty() ? default_value : val_;
00043 }
00044
00045 private:
00046 v8::Local<T> val_;
00047 };
00048
00049 template<typename T>
00050 class Maybe {
00051 public:
00052 inline bool IsNothing() const { return !has_value_; }
00053 inline bool IsJust() const { return has_value_; }
00054
00055 inline T FromJust() const {
00056 #if defined(V8_ENABLE_CHECKS)
00057 assert(IsJust() && "FromJust is Nothing");
00058 #endif // V8_ENABLE_CHECKS
00059 return value_;
00060 }
00061
00062 inline T FromMaybe(const T& default_value) const {
00063 return has_value_ ? value_ : default_value;
00064 }
00065
00066 inline bool operator==(const Maybe &other) const {
00067 return (IsJust() == other.IsJust()) &&
00068 (!IsJust() || FromJust() == other.FromJust());
00069 }
00070
00071 inline bool operator!=(const Maybe &other) const {
00072 return !operator==(other);
00073 }
00074
00075 private:
00076 Maybe() : has_value_(false) {}
00077 explicit Maybe(const T& t) : has_value_(true), value_(t) {}
00078 bool has_value_;
00079 T value_;
00080
00081 template<typename U>
00082 friend Maybe<U> Nothing();
00083 template<typename U>
00084 friend Maybe<U> Just(const U& u);
00085 };
00086
00087 template<typename T>
00088 inline Maybe<T> Nothing() {
00089 return Maybe<T>();
00090 }
00091
00092 template<typename T>
00093 inline Maybe<T> Just(const T& t) {
00094 return Maybe<T>(t);
00095 }
00096
00097 inline
00098 MaybeLocal<v8::String> ToDetailString(v8::Handle<v8::Value> val) {
00099 return MaybeLocal<v8::String>(val->ToDetailString());
00100 }
00101
00102 inline
00103 MaybeLocal<v8::Uint32> ToArrayIndex(v8::Handle<v8::Value> val) {
00104 return MaybeLocal<v8::Uint32>(val->ToArrayIndex());
00105 }
00106
00107 inline
00108 Maybe<bool> Equals(v8::Handle<v8::Value> a, v8::Handle<v8::Value>(b)) {
00109 return Just<bool>(a->Equals(b));
00110 }
00111
00112 inline
00113 MaybeLocal<v8::Object> NewInstance(v8::Handle<v8::Function> h) {
00114 return MaybeLocal<v8::Object>(h->NewInstance());
00115 }
00116
00117 inline
00118 MaybeLocal<v8::Object> NewInstance(
00119 v8::Local<v8::Function> h
00120 , int argc
00121 , v8::Local<v8::Value> argv[]) {
00122 return MaybeLocal<v8::Object>(h->NewInstance(argc, argv));
00123 }
00124
00125 inline
00126 MaybeLocal<v8::Object> NewInstance(v8::Handle<v8::ObjectTemplate> h) {
00127 return MaybeLocal<v8::Object>(h->NewInstance());
00128 }
00129
00130 inline
00131 MaybeLocal<v8::Function> GetFunction(v8::Handle<v8::FunctionTemplate> t) {
00132 return MaybeLocal<v8::Function>(t->GetFunction());
00133 }
00134
00135 inline Maybe<bool> Set(
00136 v8::Handle<v8::Object> obj
00137 , v8::Handle<v8::Value> key
00138 , v8::Handle<v8::Value> value) {
00139 return Just<bool>(obj->Set(key, value));
00140 }
00141
00142 inline Maybe<bool> Set(
00143 v8::Handle<v8::Object> obj
00144 , uint32_t index
00145 , v8::Handle<v8::Value> value) {
00146 return Just<bool>(obj->Set(index, value));
00147 }
00148
00149 inline Maybe<bool> ForceSet(
00150 v8::Handle<v8::Object> obj
00151 , v8::Handle<v8::Value> key
00152 , v8::Handle<v8::Value> value
00153 , v8::PropertyAttribute attribs = v8::None) {
00154 return Just<bool>(obj->ForceSet(key, value, attribs));
00155 }
00156
00157 inline MaybeLocal<v8::Value> Get(
00158 v8::Handle<v8::Object> obj
00159 , v8::Handle<v8::Value> key) {
00160 return MaybeLocal<v8::Value>(obj->Get(key));
00161 }
00162
00163 inline MaybeLocal<v8::Value> Get(
00164 v8::Handle<v8::Object> obj
00165 , uint32_t index) {
00166 return MaybeLocal<v8::Value>(obj->Get(index));
00167 }
00168
00169 inline Maybe<v8::PropertyAttribute> GetPropertyAttributes(
00170 v8::Handle<v8::Object> obj
00171 , v8::Handle<v8::Value> key) {
00172 return Just<v8::PropertyAttribute>(obj->GetPropertyAttributes(key));
00173 }
00174
00175 inline Maybe<bool> Has(
00176 v8::Handle<v8::Object> obj
00177 , v8::Handle<v8::String> key) {
00178 return Just<bool>(obj->Has(key));
00179 }
00180
00181 inline Maybe<bool> Has(
00182 v8::Handle<v8::Object> obj
00183 , uint32_t index) {
00184 return Just<bool>(obj->Has(index));
00185 }
00186
00187 inline Maybe<bool> Delete(
00188 v8::Handle<v8::Object> obj
00189 , v8::Handle<v8::String> key) {
00190 return Just<bool>(obj->Delete(key));
00191 }
00192
00193 inline Maybe<bool> Delete(
00194 v8::Handle<v8::Object> obj
00195 , uint32_t index) {
00196 return Just<bool>(obj->Delete(index));
00197 }
00198
00199 inline
00200 MaybeLocal<v8::Array> GetPropertyNames(v8::Handle<v8::Object> obj) {
00201 return MaybeLocal<v8::Array>(obj->GetPropertyNames());
00202 }
00203
00204 inline
00205 MaybeLocal<v8::Array> GetOwnPropertyNames(v8::Handle<v8::Object> obj) {
00206 return MaybeLocal<v8::Array>(obj->GetOwnPropertyNames());
00207 }
00208
00209 inline Maybe<bool> SetPrototype(
00210 v8::Handle<v8::Object> obj
00211 , v8::Handle<v8::Value> prototype) {
00212 return Just<bool>(obj->SetPrototype(prototype));
00213 }
00214
00215 inline MaybeLocal<v8::String> ObjectProtoToString(
00216 v8::Handle<v8::Object> obj) {
00217 return MaybeLocal<v8::String>(obj->ObjectProtoToString());
00218 }
00219
00220 inline Maybe<bool> HasOwnProperty(
00221 v8::Handle<v8::Object> obj
00222 , v8::Handle<v8::String> key) {
00223 return Just<bool>(obj->HasOwnProperty(key));
00224 }
00225
00226 inline Maybe<bool> HasRealNamedProperty(
00227 v8::Handle<v8::Object> obj
00228 , v8::Handle<v8::String> key) {
00229 return Just<bool>(obj->HasRealNamedProperty(key));
00230 }
00231
00232 inline Maybe<bool> HasRealIndexedProperty(
00233 v8::Handle<v8::Object> obj
00234 , uint32_t index) {
00235 return Just<bool>(obj->HasRealIndexedProperty(index));
00236 }
00237
00238 inline Maybe<bool> HasRealNamedCallbackProperty(
00239 v8::Handle<v8::Object> obj
00240 , v8::Handle<v8::String> key) {
00241 return Just<bool>(obj->HasRealNamedCallbackProperty(key));
00242 }
00243
00244 inline MaybeLocal<v8::Value> GetRealNamedPropertyInPrototypeChain(
00245 v8::Handle<v8::Object> obj
00246 , v8::Handle<v8::String> key) {
00247 return MaybeLocal<v8::Value>(
00248 obj->GetRealNamedPropertyInPrototypeChain(key));
00249 }
00250
00251 inline MaybeLocal<v8::Value> GetRealNamedProperty(
00252 v8::Handle<v8::Object> obj
00253 , v8::Handle<v8::String> key) {
00254 return MaybeLocal<v8::Value>(obj->GetRealNamedProperty(key));
00255 }
00256
00257 inline MaybeLocal<v8::Value> CallAsFunction(
00258 v8::Handle<v8::Object> obj
00259 , v8::Handle<v8::Object> recv
00260 , int argc
00261 , v8::Handle<v8::Value> argv[]) {
00262 return MaybeLocal<v8::Value>(obj->CallAsFunction(recv, argc, argv));
00263 }
00264
00265 inline MaybeLocal<v8::Value> CallAsConstructor(
00266 v8::Handle<v8::Object> obj
00267 , int argc
00268 , v8::Local<v8::Value> argv[]) {
00269 return MaybeLocal<v8::Value>(obj->CallAsConstructor(argc, argv));
00270 }
00271
00272 inline
00273 MaybeLocal<v8::String> GetSourceLine(v8::Handle<v8::Message> msg) {
00274 return MaybeLocal<v8::String>(msg->GetSourceLine());
00275 }
00276
00277 inline Maybe<int> GetLineNumber(v8::Handle<v8::Message> msg) {
00278 return Just<int>(msg->GetLineNumber());
00279 }
00280
00281 inline Maybe<int> GetStartColumn(v8::Handle<v8::Message> msg) {
00282 return Just<int>(msg->GetStartColumn());
00283 }
00284
00285 inline Maybe<int> GetEndColumn(v8::Handle<v8::Message> msg) {
00286 return Just<int>(msg->GetEndColumn());
00287 }
00288
00289 inline MaybeLocal<v8::Object> CloneElementAt(
00290 v8::Handle<v8::Array> array
00291 , uint32_t index) {
00292 return MaybeLocal<v8::Object>(array->CloneElementAt(index));
00293 }
00294
00295 inline MaybeLocal<v8::Value> Call(
00296 v8::Local<v8::Function> fun
00297 , v8::Local<v8::Object> recv
00298 , int argc
00299 , v8::Local<v8::Value> argv[]) {
00300 return MaybeLocal<v8::Value>(fun->Call(recv, argc, argv));
00301 }
00302
00303 #endif // NAN_MAYBE_PRE_43_INL_H_