All Downloads are FREE. Search and download functionalities are using the official Maven repository.

node_modules.smrf-native.src.marshalling.h Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
/*
 * #%L
 * %%
 * Copyright (C) 2017 BMW Car IT GmbH
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

#ifndef MARSHALLING_H
#define MARSHALLING_H

#include 
#include 
#include 
#include 
#include 

#include 
#include 

#include "util.h"

namespace marshalling
{

using OptionalV8Value = boost::optional>;

OptionalV8Value maybeToOptionalValue(Nan::MaybeLocal maybeValue)
{
    OptionalV8Value optionalV8Value;
    if (!maybeValue.IsEmpty()) {
        v8::Local value = maybeValue.ToLocalChecked();
        if (!value->IsUndefined()) {
            optionalV8Value = value;
        }
    }
    return optionalV8Value;
}

OptionalV8Value getOptionalMemberValue(v8::Local context, const char* key)
{
    Nan::MaybeLocal maybeValue = Nan::Get(context, Nan::New(key).ToLocalChecked());
    return maybeToOptionalValue(maybeValue);
}

void convertFromV8(const v8::Local& v8Value, bool& value)
{
    value = v8Value->BooleanValue();
}

void convertFromV8(const v8::Local& v8Value, std::string& value)
{
    v8::String::Utf8Value stringValue(v8Value->ToString());
    value = std::string(*stringValue, stringValue.length());
}

template 
std::enable_if_t::value> convertFromV8(const v8::Local& v8Value, T& value)
{
    value = v8Value->NumberValue();
}

v8::Local getMemberValue(const v8::Local& context, const v8::Local& key)
{
    Nan::MaybeLocal maybeValue = Nan::Get(context, key);
    if (!maybeValue.IsEmpty()) {
        v8::Local value = maybeValue.ToLocalChecked();
        if (!value->IsUndefined()) {
            return value;
        }
    }
    std::string keyStr;
    convertFromV8(key, keyStr);
    throw std::invalid_argument(std::string("member is not set: " + keyStr));
}

v8::Local getMemberValue(const v8::Local& context, const Nan::Persistent& key)
{
    return getMemberValue(context, Nan::New(key));
}

template 
v8::Local getMemberValue(const v8::Local& context, const char (&key)[N])
{
    return getMemberValue(context, util::string(key));
}

} // namespace marshalling

#endif // MARSHALLING_H




© 2015 - 2025 Weber Informatics LLC | Privacy Policy