STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
marshal_cppstd.h
Go to the documentation of this file.
1 /***
2 *marshal_cppstd.h
3 *
4 * Copyright (c) Microsoft Corporation. All rights reserved.
5 *
6 *Purpose: Marshalling classes
7 *
8 * [Public]
9 *
10 ****/
11 
12 #pragma once
13 
14 #ifndef _INC_MSCLR_MARSHAL_CPPSTD
15 #define _INC_MSCLR_MARSHAL_CPPSTD
16 
17 #include <string>
18 #include <vector>
19 #include <vcclr.h>
20 
21 #include "marshal.h"
22 
23 namespace msclr{
24  namespace interop{
25 
26 template <>
27 inline System::String^ marshal_as(const std::string& _from_obj)
28 {
29  return details::InternalAnsiToStringHelper(_from_obj.c_str(), _from_obj.length());
30 }
31 
32 template <>
33 inline std::string marshal_as(System::String^ const & _from_obj)
34 {
35  if (_from_obj == nullptr)
36  {
37  throw gcnew System::ArgumentNullException(_EXCEPTION_NULLPTR);
38  }
39  std::string _to_obj;
40  size_t _size = details::GetAnsiStringSize(_from_obj);
41 
42  if (_size > 1)
43  {
44  // -1 because resize will automatically +1 for the NULL
45  _to_obj.resize(_size-1);
46  char *_dest_buf = &(_to_obj[0]);
47 
48  details::WriteAnsiString(_dest_buf, _size, _from_obj);
49  }
50 
51  return _to_obj;
52 }
53 
54 template <>
55 inline System::String^ marshal_as(const std::wstring& _from_obj)
56 {
57  return details::InternalUnicodeToStringHelper(_from_obj.c_str(), _from_obj.length());
58 }
59 
60 template <>
61 inline std::wstring marshal_as(System::String^ const & _from_obj)
62 {
63  if(_from_obj != nullptr)
64  {
65  cli::pin_ptr<const wchar_t> _pinned_ptr = PtrToStringChars(_from_obj);
66  std::wstring _to_obj(static_cast<const wchar_t *>(_pinned_ptr), _from_obj->Length);
67 
68  return _to_obj;
69  }
70  else
71  {
72  throw gcnew System::ArgumentNullException(_EXCEPTION_NULLPTR);
73  }
74 }
75 
76  } //namespace interop
77 } //namespace msclr
78 
79 #endif /* _INC_MSCLR_MARSHAL_CPPSTD */
#define _EXCEPTION_NULLPTR
Definition: marshal.h:39
Definition: appdomain.h:36
_Check_return_ size_t GetAnsiStringSize(System::String^_str)
Definition: marshal.h:78
System::String InternalAnsiToStringHelper(_In_reads_z_(_count+1) const char *_src, size_t _count)
Definition: marshal.h:147
__const_Char_ptr PtrToStringChars(__const_String_handle s)
Definition: vcclr.h:40
_To_Type marshal_as(_In_z_ const char _from_object[])
Definition: marshal.h:187
void WriteAnsiString(_Out_writes_all_(_size) _Post_z_ char *_buf, size_t _size, System::String^_str)
Definition: marshal.h:93
basic_string< wchar_t, char_traits< wchar_t >, allocator< wchar_t > > wstring
Definition: xstring:2645
basic_string< char, char_traits< char >, allocator< char > > string
Definition: xstring:2643
System::String InternalUnicodeToStringHelper(_In_reads_z_(_count+1) const wchar_t *_src, size_t _count)
Definition: marshal.h:165