NetOceanDirect  3.1.1
OceanDirect .NET API
Utility.h
1 #pragma once
2 #include "stdafx.h"
3 
4 
5 //NOTE:
6 //This class uses a cli::array which is needed for the .NET wrapper. During compilation this class could easily fail (template mismatch) if the
7 //imported C++ header file (ex: SerialPortAPI.h) has a statement "using namespace std". The compiler would then think that this array
8 //is a std::array instead of cli::array.
9 class Utility
10 {
11 public:
12  static array<int>^ IntegerManagedArray(int* memory, int count) {
13  if (memory == NULL)
14  throw gcnew ArgumentNullException("memory");
15 
16  if (count <= 0)
17  return gcnew array<int>(0);
18 
19  array<int>^ arr = gcnew array<int>(count);
20 
21  pin_ptr<int> arrPin = &arr[0];
22 
23  memcpy_s(arrPin, count * sizeof(int), memory, count * sizeof(int));
24 
25  return arr;
26  }
27 
28  static array<int>^ LongToIntegerManagedArray(long* memory, int count) {
29  if (memory == NULL) {
30  throw gcnew ArgumentNullException("memory");
31  }
32 
33  if (count <= 0) {
34  return gcnew array<int>(0);
35  }
36 
37  array<int>^ arr = gcnew array<int>(count);
38 
39  for (int i = 0; i < count; i++) {
40  arr[i] = static_cast<int>(memory[i]);
41  }
42 
43  return arr;
44  }
45 
46  static array<unsigned char>^ UnsignedCharManagedArray(uint8_t* memory, int count) {
47  if (!memory) {
48  throw gcnew ArgumentNullException("memory");
49  }
50 
51  if (count <= 0) {
52  return gcnew array<uint8_t>(0);
53  }
54 
55  array<uint8_t>^ arr = gcnew array<uint8_t>(count);
56 
57  pin_ptr<uint8_t> arrPin = &arr[0];
58 
59  memcpy_s(arrPin, count * sizeof(uint8_t), memory, count * sizeof(uint8_t));
60 
61  return arr;
62  }
63 
64  static array<uint16_t>^ UnsignedShortManagedArray(uint16_t* memory, int count) {
65  if (!memory) {
66  throw gcnew ArgumentNullException("memory");
67  }
68 
69  if (count <= 0) {
70  return gcnew array<uint16_t>(0);
71  }
72 
73  array<uint16_t>^ arr = gcnew array<uint16_t>(count);
74 
75  pin_ptr<uint16_t> arrPin = &arr[0];
76 
77  memcpy_s(arrPin, count * sizeof(uint16_t), memory, count * sizeof(uint16_t));
78 
79  return arr;
80  }
81 
82  static array<std::uint32_t>^ UnsignedIntegerManagedArray(std::uint32_t* memory, int count) {
83  if (memory == NULL) {
84  throw gcnew ArgumentNullException("memory");
85  }
86 
87  if (count <= 0) {
88  return gcnew array<std::uint32_t>(0);
89  }
90 
91  array<std::uint32_t>^ arr = gcnew array<std::uint32_t>(count);
92 
93  pin_ptr<std::uint32_t> arrPin = &arr[0];
94 
95  memcpy_s(arrPin, count * sizeof(std::uint32_t), memory, count * sizeof(std::uint32_t));
96 
97  return arr;
98  }
99 
100  static array<double>^ DoubleManagedArray(double* memory, int count) {
101  if (memory == NULL)
102  throw gcnew ArgumentNullException("memory");
103 
104  if (count <= 0)
105  return gcnew array<double>(0);
106 
107  array<double>^ arr = gcnew array<double>(count);
108 
109  pin_ptr<double> arrPin = &arr[0];
110  memcpy_s(arrPin, count * sizeof(double), memory, count * sizeof(double));
111  return arr;
112  }
113 
114  static array<float>^ FloatManagedArray(float* memory, int count) {
115  if (memory == NULL)
116  throw gcnew ArgumentNullException("memory");
117 
118  if (count <= 0)
119  return gcnew array<float>(0);
120 
121  array<float>^ arr = gcnew array<float>(count);
122 
123  pin_ptr<float> arrPin = &arr[0];
124  memcpy_s(arrPin, count * sizeof(float), memory, count * sizeof(float));
125  return arr;
126  }
127 
128  static array<unsigned char>^ UCharManagedArray(unsigned char* memory, int count) {
129  if (memory == NULL)
130  throw gcnew ArgumentNullException("memory");
131 
132  if (count <= 0)
133  return gcnew array<unsigned char>(0);
134 
135  array<unsigned char>^ arr = gcnew array<unsigned char>(count);
136 
137  pin_ptr<unsigned char> arrPin = &arr[0];
138  memcpy_s(arrPin, count * sizeof(unsigned char), memory, count * sizeof(unsigned char));
139  return arr;
140  }
141 
142  static array<char>^ CharManagedArray(char* memory, int count) {
143  if (memory == NULL)
144  throw gcnew ArgumentNullException("memory");
145 
146  if (count <= 0)
147  return gcnew array<char>(0);
148 
149  array<char>^ arr = gcnew array<char>(count);
150 
151  pin_ptr<char> arrPin = &arr[0];
152  memcpy_s(arrPin, count * sizeof(char), memory, count * sizeof(char));
153  return arr;
154  }
155 
156  static String^ ToManagedString(const char* string)
157  {
158  return Marshal::PtrToStringAnsi((IntPtr)(char*)string);
159  }
160 
161  static char* ToUnmanagedString(String^ string)
162  {
163  return (char*)Marshal::StringToHGlobalAnsi(string).ToPointer();
164  }
165 
166  static const char* string_to_char_array(String^ string)
167  {
168  const char* str = (const char*)(Marshal::StringToHGlobalAnsi(string)).ToPointer();
169  return str;
170  }
171 
172 private:
173  Utility() {}
174 };
175 
Definition: Utility.h:10