File: utilities.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (103 lines) | stat: -rw-r--r-- 3,485 bytes parent folder | download | duplicates (12)
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
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_TOOLS_V8WINDBG_BASE_UTILITIES_H_
#define V8_TOOLS_V8WINDBG_BASE_UTILITIES_H_

#include "tools/v8windbg/base/dbgext.h"

inline const wchar_t* U16ToWChar(const char16_t* p_u16) {
  static_assert(sizeof(wchar_t) == sizeof(char16_t), "wrong wchar size");
  return reinterpret_cast<const wchar_t*>(p_u16);
}

inline const wchar_t* U16ToWChar(std::u16string& str) {
  return U16ToWChar(str.data());
}

#if defined(WIN32)
inline std::u16string ConvertToU16String(std::string utf8_string) {
  int len_chars =
      ::MultiByteToWideChar(CP_UTF8, 0, utf8_string.c_str(), -1, nullptr, 0);

  char16_t* p_buff =
      static_cast<char16_t*>(malloc(len_chars * sizeof(char16_t)));

  // On Windows wchar_t is the same a char16_t
  static_assert(sizeof(wchar_t) == sizeof(char16_t), "wrong wchar size");
  len_chars =
      ::MultiByteToWideChar(CP_UTF8, 0, utf8_string.c_str(), -1,
                            reinterpret_cast<wchar_t*>(p_buff), len_chars);
  std::u16string result{p_buff};
  free(p_buff);

  return result;
}

inline std::string ConvertFromU16String(std::u16string u16string) {
  int len_chars =
      ::WideCharToMultiByte(CP_UTF8, 0, U16ToWChar(u16string.c_str()), -1,
                            nullptr, 0, nullptr, nullptr);

  char* p_buff = static_cast<char*>(malloc(len_chars * sizeof(char)));

  len_chars = ::WideCharToMultiByte(CP_UTF8, 0, U16ToWChar(u16string.c_str()),
                                    -1, p_buff, len_chars, nullptr, nullptr);
  std::string result{p_buff};
  free(p_buff);

  return result;
}

#else
#error String encoding conversion must be provided for the target platform.
#endif

HRESULT CreateProperty(IDataModelManager* p_manager,
                       IModelPropertyAccessor* p_property,
                       IModelObject** pp_property_object);

HRESULT CreateMethod(IDataModelManager* p_manager, IModelMethod* p_method,
                     IModelObject** pp_method_object);

HRESULT UnboxProperty(IModelObject* object, IModelPropertyAccessor** result);

HRESULT CreateTypedIntrinsic(uint64_t value, IDebugHostType* type,
                             IModelObject** result);

HRESULT CreateULong64(ULONG64 value, IModelObject** pp_int);

HRESULT UnboxULong64(IModelObject* object, ULONG64* value,
                     bool convert = false);

HRESULT GetInt32(IDebugHostConstant* object, int* value);

HRESULT CreateInt32(int value, IModelObject** pp_int);

HRESULT CreateUInt32(uint32_t value, IModelObject** pp_int);

HRESULT CreateBool(bool value, IModelObject** pp_val);

HRESULT CreateNumber(double value, IModelObject** pp_val);

HRESULT CreateString(std::u16string value, IModelObject** pp_val);

HRESULT UnboxString(IModelObject* object, BSTR* value);

HRESULT GetModelAtIndex(WRL::ComPtr<IModelObject>& sp_parent,
                        WRL::ComPtr<IModelObject>& sp_index,
                        IModelObject** p_result);

HRESULT GetCurrentThread(WRL::ComPtr<IDebugHostContext>& sp_host_context,
                         IModelObject** p_current_thread);

#define RETURN_IF_FAIL(expression) \
  do {                             \
    HRESULT hr = expression;       \
    if (FAILED(hr)) {              \
      return hr;                   \
    }                              \
  } while (false)

#endif  // V8_TOOLS_V8WINDBG_BASE_UTILITIES_H_