ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Dtype.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <Logging.h>
11 
12 #include <cstring>
13 #include <string>
14 
15 #include "cloudViewer/Macro.h"
17 
18 namespace cloudViewer {
19 namespace core {
20 
22 public:
23  static const Dtype Undefined;
24  static const Dtype Float32;
25  static const Dtype Float64;
26  static const Dtype Int8;
27  static const Dtype Int16;
28  static const Dtype Int32;
29  static const Dtype Int64;
30  static const Dtype UInt8;
31  static const Dtype UInt16;
32  static const Dtype UInt32;
33  static const Dtype UInt64;
34  static const Dtype Bool;
35 
36 public:
37  enum class DtypeCode {
38  Undefined,
39  Bool, // Needed to distinguish bool from uint8_t.
40  Int,
41  UInt,
42  Float,
43  Object,
44  };
45 
46  Dtype() : Dtype(DtypeCode::Undefined, 1, "Undefined") {}
47 
48  explicit Dtype(DtypeCode dtype_code,
49  int64_t byte_size,
50  const std::string &name);
51 
54  template <typename T>
55  static inline const Dtype FromType() {
56  utility::LogError("Unsupported data for Dtype::FromType.");
57  }
58 
59  int64_t ByteSize() const { return byte_size_; }
60 
61  DtypeCode GetDtypeCode() const { return dtype_code_; }
62 
63  bool IsObject() const { return dtype_code_ == DtypeCode::Object; }
64 
65  std::string ToString() const { return name_; }
66 
67  bool operator==(const Dtype &other) const;
68 
69  bool operator!=(const Dtype &other) const;
70 
71 private:
72  static constexpr size_t max_name_len_ = 16;
73  DtypeCode dtype_code_;
74  int64_t byte_size_;
75  char name_[max_name_len_]; // MSVC warns if std::string is exported to DLL.
76 };
77 
78 CLOUDVIEWER_API extern const Dtype Undefined;
79 CLOUDVIEWER_API extern const Dtype Float32;
80 CLOUDVIEWER_API extern const Dtype Float64;
81 CLOUDVIEWER_API extern const Dtype Int8;
82 CLOUDVIEWER_API extern const Dtype Int16;
83 CLOUDVIEWER_API extern const Dtype Int32;
84 CLOUDVIEWER_API extern const Dtype Int64;
85 CLOUDVIEWER_API extern const Dtype UInt8;
86 CLOUDVIEWER_API extern const Dtype UInt16;
87 CLOUDVIEWER_API extern const Dtype UInt32;
88 CLOUDVIEWER_API extern const Dtype UInt64;
89 CLOUDVIEWER_API extern const Dtype Bool;
90 
91 template <>
92 inline const Dtype Dtype::FromType<float>() {
93  return Dtype::Float32;
94 }
95 
96 template <>
97 inline const Dtype Dtype::FromType<double>() {
98  return Dtype::Float64;
99 }
100 
101 template <>
102 inline const Dtype Dtype::FromType<int8_t>() {
103  return Dtype::Int8;
104 }
105 
106 template <>
107 inline const Dtype Dtype::FromType<int16_t>() {
108  return Dtype::Int16;
109 }
110 
111 template <>
112 inline const Dtype Dtype::FromType<int32_t>() {
113  return Dtype::Int32;
114 }
115 
116 template <>
117 inline const Dtype Dtype::FromType<int64_t>() {
118  return Dtype::Int64;
119 }
120 
121 template <>
122 inline const Dtype Dtype::FromType<uint8_t>() {
123  return Dtype::UInt8;
124 }
125 
126 template <>
127 inline const Dtype Dtype::FromType<uint16_t>() {
128  return Dtype::UInt16;
129 }
130 
131 template <>
132 inline const Dtype Dtype::FromType<uint32_t>() {
133  return Dtype::UInt32;
134 }
135 
136 template <>
137 inline const Dtype Dtype::FromType<uint64_t>() {
138  return Dtype::UInt64;
139 }
140 
141 template <>
142 inline const Dtype Dtype::FromType<bool>() {
143  return Dtype::Bool;
144 }
145 
146 } // namespace core
147 } // namespace cloudViewer
std::string name
std::string name_
Definition: FilePLY.cpp:33
#define CLOUDVIEWER_API
Definition: Macro.h:34
static const Dtype Int8
Definition: Dtype.h:26
std::string ToString() const
Definition: Dtype.h:65
static const Dtype UInt8
Definition: Dtype.h:30
static const Dtype Undefined
Definition: Dtype.h:23
static const Dtype Float32
Definition: Dtype.h:24
bool IsObject() const
Definition: Dtype.h:63
static const Dtype UInt16
Definition: Dtype.h:31
static const Dtype FromType()
Definition: Dtype.h:55
static const Dtype UInt64
Definition: Dtype.h:33
static const Dtype Int16
Definition: Dtype.h:27
int64_t ByteSize() const
Definition: Dtype.h:59
static const Dtype UInt32
Definition: Dtype.h:32
static const Dtype Float64
Definition: Dtype.h:25
static const Dtype Int32
Definition: Dtype.h:28
static const Dtype Int64
Definition: Dtype.h:29
DtypeCode GetDtypeCode() const
Definition: Dtype.h:61
static const Dtype Bool
Definition: Dtype.h:34
#define LogError(...)
Definition: Logging.h:60
const Dtype Undefined
Definition: Dtype.cpp:41
const Dtype Int8
Definition: Dtype.cpp:44
const Dtype Bool
Definition: Dtype.cpp:52
const Dtype Int64
Definition: Dtype.cpp:47
const Dtype UInt64
Definition: Dtype.cpp:51
const Dtype UInt32
Definition: Dtype.cpp:50
const Dtype UInt8
Definition: Dtype.cpp:48
const Dtype Int16
Definition: Dtype.cpp:45
const Dtype Float64
Definition: Dtype.cpp:43
const Dtype UInt16
Definition: Dtype.cpp:49
const Dtype Int32
Definition: Dtype.cpp:46
const Dtype Float32
Definition: Dtype.cpp:42
constexpr bool operator!=(const optional< T > &x, const optional< T > &y)
Definition: Optional.h:620
constexpr bool operator==(const optional< T > &x, const optional< T > &y)
Definition: Optional.h:615
Generic file read and write utility for python interface.