NetOceanDirect  2.4.0
OceanDirect .NET API
Utility.h
1 #pragma once
2 #include "stdafx.h"
3 
4 class Utility
5 {
6 public:
7  static array<int>^ IntegerManagedArray(int* memory, int count) {
8  if (memory == NULL)
9  throw gcnew ArgumentNullException("memory");
10 
11  if (count <= 0)
12  return gcnew array<int>(0);
13 
14  array<int>^ arr = gcnew array<int>(count);
15 
16  pin_ptr<int> arrPin = &arr[0];
17 
18  memcpy_s(arrPin, count * sizeof(int), memory, count * sizeof(int));
19 
20  return arr;
21  }
22 
23  static array<int>^ LongToIntegerManagedArray(long* memory, int count) {
24  if (memory == NULL) {
25  throw gcnew ArgumentNullException("memory");
26  }
27 
28  if (count <= 0) {
29  return gcnew array<int>(0);
30  }
31 
32  array<int>^ arr = gcnew array<int>(count);
33 
34  for (int i = 0; i < count; i++) {
35  arr[i] = static_cast<int>(memory[i]);
36  }
37 
38  return arr;
39  }
40 
41 
42  static array<std::uint32_t>^ UnsignedIntegerManagedArray(std::uint32_t* memory, int count) {
43  if (memory == NULL) {
44  throw gcnew ArgumentNullException("memory");
45  }
46 
47  if (count <= 0) {
48  return gcnew array<std::uint32_t>(0);
49  }
50 
51  array<std::uint32_t>^ arr = gcnew array<std::uint32_t>(count);
52 
53  pin_ptr<std::uint32_t> arrPin = &arr[0];
54 
55  memcpy_s(arrPin, count * sizeof(std::uint32_t), memory, count * sizeof(std::uint32_t));
56 
57  return arr;
58  }
59 
60  static array<double>^ DoubleManagedArray(double* memory, int count) {
61  if (memory == NULL)
62  throw gcnew ArgumentNullException("memory");
63 
64  if (count <= 0)
65  return gcnew array<double>(0);
66 
67  array<double>^ arr = gcnew array<double>(count);
68 
69  pin_ptr<double> arrPin = &arr[0];
70  memcpy_s(arrPin, count * sizeof(double), memory, count * sizeof(double));
71  return arr;
72  }
73 
74  static array<float>^ FloatManagedArray(float* memory, int count) {
75  if (memory == NULL)
76  throw gcnew ArgumentNullException("memory");
77 
78  if (count <= 0)
79  return gcnew array<float>(0);
80 
81  array<float>^ arr = gcnew array<float>(count);
82 
83  pin_ptr<float> arrPin = &arr[0];
84  memcpy_s(arrPin, count * sizeof(float), memory, count * sizeof(float));
85  return arr;
86  }
87 
88  static array<unsigned char>^ UCharManagedArray(unsigned char* memory, int count) {
89  if (memory == NULL)
90  throw gcnew ArgumentNullException("memory");
91 
92  if (count <= 0)
93  return gcnew array<unsigned char>(0);
94 
95  array<unsigned char>^ arr = gcnew array<unsigned char>(count);
96 
97  pin_ptr<unsigned char> arrPin = &arr[0];
98  memcpy_s(arrPin, count * sizeof(unsigned char), memory, count * sizeof(unsigned char));
99  return arr;
100  }
101 
102  static array<char>^ CharManagedArray(char* memory, int count) {
103  if (memory == NULL)
104  throw gcnew ArgumentNullException("memory");
105 
106  if (count <= 0)
107  return gcnew array<char>(0);
108 
109  array<char>^ arr = gcnew array<char>(count);
110 
111  pin_ptr<char> arrPin = &arr[0];
112  memcpy_s(arrPin, count * sizeof(char), memory, count * sizeof(char));
113  return arr;
114  }
115 
116  static String^ ToManagedString(const char* string)
117  {
118  return Marshal::PtrToStringAnsi((IntPtr)(char*)string);
119  }
120 
121  static char* ToUnmanagedString(String^ string)
122  {
123  return (char*)Marshal::StringToHGlobalAnsi(string).ToPointer();
124  }
125 
126  static const char* string_to_char_array(String^ string)
127  {
128  const char* str = (const char*)(Marshal::StringToHGlobalAnsi(string)).ToPointer();
129  return str;
130  }
131 
132 private:
133  Utility() {}
134 };
135 
Definition: Utility.h:5