-
Notifications
You must be signed in to change notification settings - Fork 0
/
octypes.h
46 lines (35 loc) · 1.06 KB
/
octypes.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
// $Id: octypes.h,v 1.3 2019-04-22 13:46:03-07 - - $
// Type definitiions to compile oc programs with c++ compiler.
#ifndef __OCDEFS_H__
#define __OCDEFS_H__
#include <type_traits>
using string = char*;
template <typename type>
using ptr = std::enable_if_t<std::is_class<type>::value,type*>;
template <typename type>
struct array {
using array_value_type = type;
type* data {};
array() = default;
array (type* that) { data = that; }
array& operator= (type* that) { data = that; return *this; }
type& operator[] (int i) { return data[i]; }
};
template <typename type>
std::enable_if_t<std::is_class<type>::value,ptr<type>>
alloc() {
return new type();
}
template <typename type>
array<typename type::array_value_type>
alloc (int size) {
auto result = new typename type::array_value_type [size] {};
using result_t = array<typename type::array_value_type>*;
return *reinterpret_cast<result_t> (&result);
}
template <typename type>
std::enable_if_t<std::is_same<type,string>::value,string>
alloc (int size) {
return new char[size] {};
}
#endif