-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
128 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright (c) 2017 The Bacardi Authors. | ||
* | ||
* 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. | ||
*/ | ||
|
||
#ifndef GEN_IDL_ENUM_TYPES_H_ | ||
#define GEN_IDL_ENUM_TYPES_H_ | ||
|
||
#include "core/idl_base.h" | ||
|
||
{% for enum in enums %} | ||
struct IDL{{enum.name}} final : public IDLBaseHelper<std::string> {}; | ||
{% endfor %} | ||
|
||
#endif // GEN_IDL_ENUM_TYPES_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright (c) 2017 The Bacardi Authors. | ||
* | ||
* 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. | ||
*/ | ||
|
||
#ifndef GEN_NATIVE_ENUM_TYPE_TRAITS_H_ | ||
#define GEN_NATIVE_ENUM_TYPE_TRAITS_H_ | ||
|
||
#include <napi.h> | ||
#include <string> | ||
#include <type_traits> | ||
#include "core/native_type_traits.h" | ||
#include "idl_enum_type.h" | ||
|
||
{% for enum in enums %} | ||
template <> | ||
struct NativeTypeTraits<IDL{{enum.name}}> | ||
: public NativeTypeTraitsBase<IDL{{enum.name}}> { | ||
static std::string NativeValue(const Napi::Env& env, | ||
const Napi::Value& js_value) { | ||
if (!js_value.IsString()) { | ||
Napi::TypeError::New(env, "It's an invalid string.") | ||
.ThrowAsJavaScriptException(); | ||
return std::string(); | ||
} | ||
|
||
std::string value = js_value.ToString().Utf8Value(); | ||
if (!IsValidValue(value)) { | ||
Napi::TypeError::New(env, "it not matched with values of enum in idl.") | ||
.ThrowAsJavaScriptException(); | ||
return std::string(); | ||
} | ||
|
||
return js_value.ToString().Utf8Value(); | ||
} | ||
|
||
static bool IsTypeEquals(const Napi::Value& js_value) { | ||
if (js_value.IsString()) { | ||
std::string value = js_value.ToString().Utf8Value(); | ||
return IsValidValue(value); | ||
} | ||
return false; | ||
} | ||
|
||
static bool IsValidValue(std::string value) { | ||
if ( | ||
{% for value in enum.values %} | ||
{% if loop.last %} | ||
value.compare("{{value}}") == 0 | ||
{% else %} | ||
value.compare("{{value}}") == 0 || | ||
{% endif %} | ||
{% endfor %} | ||
) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
}; | ||
{% endfor %} | ||
|
||
#endif // CORE_NATIVE_TYPE_TRAITS_H_ |