/
Types.h
124 lines (102 loc) · 4.42 KB
/
Types.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) 2013, Richard Eakin and the Dart project authors.
// Use of this source code (and the Dart VM) is governed by a
// BSD-style license that can be found in the LICENSE.txt file.
#pragma once
#include "include/dart_api.h"
#include "cinder/Vector.h"
#include "cinder/Quaternion.h"
#include "cinder/Color.h"
#include "cinder/Rect.h"
#include "cinder/Exception.h"
#include "cinder/Log.h"
namespace cidart {
struct DartScope {
DartScope() { Dart_EnterScope(); }
~DartScope() { Dart_ExitScope(); }
};
Dart_Handle toDart( const char *str );
Dart_Handle toDart( const std::string &str );
Dart_Handle toDart( int value );
Dart_Handle toDart( float value );
Dart_Handle toDart( double value );
Dart_Handle toDart( const ci::vec3 &value );
bool isCinderClass( Dart_Handle handle, const char *className );
// TODO: remove, replace with getMapValueForKey<T>()
float getFloatForKey( Dart_Handle mapHandle, const char *key );
void getValue( Dart_Handle handle, bool *value );
void getValue( Dart_Handle handle, int *value );
void getValue( Dart_Handle handle, size_t *value );
void getValue( Dart_Handle handle, float *value );
void getValue( Dart_Handle handle, double *value );
void getValue( Dart_Handle handle, ci::Color *value );
void getValue( Dart_Handle handle, ci::ColorA *value );
void getValue( Dart_Handle handle, ci::ivec2 *value );
void getValue( Dart_Handle handle, ci::vec2 *value );
void getValue( Dart_Handle handle, ci::dvec2 *value );
void getValue( Dart_Handle handle, ci::ivec3 *value );
void getValue( Dart_Handle handle, ci::vec3 *value );
void getValue( Dart_Handle handle, ci::dvec3 *value );
void getValue( Dart_Handle handle, ci::ivec4 *value );
void getValue( Dart_Handle handle, ci::vec4 *value );
void getValue( Dart_Handle handle, ci::dvec4 *value );
void getValue( Dart_Handle handle, ci::mat4 *value );
void getValue( Dart_Handle handle, ci::quat *value );
void getValue( Dart_Handle handle, ci::dquat *value );
void getValue( Dart_Handle handle, ci::Rectf *value );
void getValue( Dart_Handle handle, ci::log::Level *value );
void getValue( Dart_Handle handle, std::string *value );
//! Returns the value of type \a T held by \a handle. If an error occurs, the value returned is default constructed and an error message is logged.
template <typename T>
T getValue( Dart_Handle handle )
{
T result;
getValue( handle, &result );
return result;
}
//! Returns a \a Dart_Handle that represents the field \a name on \a container.
Dart_Handle getField( Dart_Handle container, const std::string &name );
//! Returns a \a Dart_Handle that represents the field \a name on \a container.
Dart_Handle getField( Dart_Handle container, const char *name );
//! Returns the value of type \a T for field \a name on the \a container. If an error occurs, the value returned is default constructed and an error message is logged.
template <typename T>
T getField( Dart_Handle container, const std::string &name )
{
T result;
getValue( getField( container, name ), &result );
return result;
}
//! Returns the value of type \a T for field \a name on the \a container, or \a defaultValue if the field was null or an error occurs.
template <typename T>
T getFieldOrDefault( Dart_Handle container, const std::string &name, const T &defaultValue )
{
Dart_Handle fieldHandle = cidart::getField( container, name );
if( Dart_IsNull( fieldHandle ) )
return defaultValue;
else
return cidart::getValue<T>( fieldHandle );
}
Dart_Handle getMapValueForKey( Dart_Handle mapHandle, const char *key );
template <typename T>
T getMapValueForKey( Dart_Handle mapHandle, const char *key )
{
CI_ASSERT( Dart_IsMap( mapHandle ) );
Dart_Handle valueHandle = Dart_MapGetAt( mapHandle, toDart( key ) );
T result;
getValue( valueHandle, &result );
return result;
}
//! Returns the native argument of type \a T at \a index. If an error occurs, the value returned is default constructed and an error message is logged.
template <typename T>
T getArg( Dart_NativeArguments args, int index )
{
T result;
Dart_Handle argHandle = Dart_GetNativeArgument( args, index );
getValue( argHandle, &result );
return result;
}
bool hasFunction( Dart_Handle handle, const std::string &name );
Dart_Handle callFunction( Dart_Handle target, const std::string &name, int numArgs = 0, Dart_Handle *args = nullptr );
std::string getTypeName( Dart_Handle handle );
// Debug utils:
std::string printNativeArgumentsToString( Dart_NativeArguments args, bool printMethodNames = false );
} // namespace cidart