DATAKIT API  V2025.1
dtk_val.hpp
Go to the documentation of this file.
1 #ifndef __DTK_VAL_HPP__
2 #define __DTK_VAL_HPP__
3 
4 #include "def/define.h"
5 #include "dtk/config.hpp"
12 #include "util/error_dtk.hpp"
13 #include "util/util_stl_dtk.hpp"
14 #include <iosfwd>
15 #include <type_traits>
16 #include <utility>
17 
30 };
31 
32 namespace Dtk_Detail_Val
33 {
34  template <Dtk_val_type v, bool trivial = false> struct Value : std::integral_constant<Dtk_val_type, v> { static bool const is_trivial = trivial; };
35 
36  template <typename T = void> struct ValueFor;
37  template <> struct ValueFor<Dtk_Char8> : Value<DTK_VAL_TYPE_CHAR, true> {};
38  template <> struct ValueFor<Dtk_Int32> : Value<DTK_VAL_TYPE_INT, true> {};
39  template <> struct ValueFor<Dtk_Double64> : Value<DTK_VAL_TYPE_DOUBLE, true> {};
40  template <> struct ValueFor<Dtk_RGB> : Value<DTK_VAL_TYPE_RGB> {};
41  template <> struct ValueFor<Dtk_pnt> : Value<DTK_VAL_TYPE_PNT> {};
42  template <> struct ValueFor<Dtk_dir> : Value<DTK_VAL_TYPE_DIR> {};
43  template <> struct ValueFor<Dtk_UUID> : Value<DTK_VAL_TYPE_UUID> {};
44  template <> struct ValueFor<Dtk_string> : Value<DTK_VAL_TYPE_STRING> {};
45  template <> struct ValueFor<Dtk_tab<Dtk_Int32>> : Value<DTK_VAL_TYPE_INTARRAY> {};
46  template <> struct ValueFor<Dtk_tab<Dtk_Char8>> : Value<DTK_VAL_TYPE_BUFFERDATA> {};
47 
48  template <typename T> struct Binder : ValueFor<T>
49  {
50  static T * GetPointer( void * u )
51  {
52  return GetPointer( u, std::integral_constant<bool, Binder::is_trivial>() );
53  }
54  static T const * GetPointer( void const * u )
55  {
56  return GetPointer( u, std::integral_constant<bool, Binder::is_trivial>() );
57  }
58  private:
59  static T * GetPointer( void * u, std::integral_constant<bool, true> ) { return static_cast< T * >( u ); }
60  static T * GetPointer( void * u, std::integral_constant<bool, false> ) { return *static_cast< T ** >( u ); }
61  static T const * GetPointer( void const * u, std::integral_constant<bool, true> ) { return static_cast< T const * >( u ); }
62  static T const * GetPointer( void const * u, std::integral_constant<bool, false> ) { return *static_cast< T * const * >( u ); }
63  };
64 }
65 
66 class Dtk_Val
67 {
68 protected:
69  union {
80  }_val;
81  enum Dtk_val_type _type;
84  void _copy(const Dtk_Val&s);
86 public:
88  Dtk_Val(const Dtk_Int32 val);
89  Dtk_Val(const Dtk_Char8 val);
90  Dtk_Val(const Dtk_Double64 val);
91  Dtk_Val( const Dtk_Val& val );
93  Dtk_Val(const Dtk_string& val);
95  Dtk_Val(const Dtk_RGB & val);
96  Dtk_Val(const Dtk_pnt & val);
97  Dtk_Val(const Dtk_dir & val);
98  Dtk_Val(const Dtk_UUID & val);
99  Dtk_Val(const Dtk_tab< Dtk_Int32 > & val);
102  Dtk_Val& operator = (const Dtk_Val& s);
103  Dtk_Val & operator=( Dtk_Val && ) DTK_NOEXCEPT;
104 public:
105  int GetInt(Dtk_status & st = Dtk_status::GetDefaultStatus()) const;
106  char GetChar(Dtk_status & st = Dtk_status::GetDefaultStatus()) const;
107  double GetDouble(Dtk_status & st = Dtk_status::GetDefaultStatus()) const;
108  Dtk_string GetString(Dtk_status & st = Dtk_status::GetDefaultStatus()) const;
109  Dtk_RGB GetRgb(Dtk_status & st = Dtk_status::GetDefaultStatus()) const;
110  Dtk_pnt GetPnt(Dtk_status & st = Dtk_status::GetDefaultStatus()) const;
111  Dtk_dir GetDir(Dtk_status & st = Dtk_status::GetDefaultStatus()) const;
112  Dtk_UUID GetUuid(Dtk_status & st = Dtk_status::GetDefaultStatus()) const;
113  Dtk_tab< Dtk_Int32 > GetIntArray(Dtk_status & st = Dtk_status::GetDefaultStatus()) const;
114  Dtk_tab<Dtk_Char8> GetBufferData( Dtk_status & st = Dtk_status::GetDefaultStatus() ) const;
115 public:
116  template <typename T> std::pair<bool, T> Get() const
117  {
118  typedef Dtk_Detail_Val::Binder<T> Binded;
119  return ( _type == Binded::value )
120  ? std::make_pair( true, *Binded::GetPointer( &_val ) )
121  : std::make_pair( false, T() );
122  }
123  template <typename T> T * GetIf()
124  {
125  typedef Dtk_Detail_Val::Binder<T> Binded;
126  return ( _type == Binded::value ) ? Binded::GetPointer( &_val ) : nullptr;
127  }
128  template <typename T> T const * GetIf() const
129  {
130  typedef Dtk_Detail_Val::Binder<T> Binded;
131  return ( _type == Binded::value ) ? Binded::GetPointer( &_val ) : nullptr;
132  }
133 public:
135 public:
137  friend std::ostream& operator<<(std::ostream& o,const Dtk_Val& d);
139 };
140 
141 
142 
143 
144 #endif //#ifndef __DTK_VAL_HPP__
DTK_VAL_TYPE_RGB
@ DTK_VAL_TYPE_RGB
Definition: dtk_val.hpp:24
Dtk_Val::_Store
Dtk_ErrorStatus _Store(void *)
Dtk_Val::dir_val
Dtk_dir * dir_val
Definition: dtk_val.hpp:76
dtk_dir.hpp
Dtk_Val::_init
void _init() DTK_NOEXCEPT
dtk_status.hpp
Dtk_Val::_copy
void _copy(const Dtk_Val &s)
Dtk_Val::_type
enum Dtk_val_type _type
Definition: dtk_val.hpp:81
config.hpp
Dtk_Detail_Val
Definition: dtk_val.hpp:33
Dtk_Val::double_val
Dtk_Double64 double_val
Definition: dtk_val.hpp:72
DTK_VAL_TYPE_CHAR
@ DTK_VAL_TYPE_CHAR
Definition: dtk_val.hpp:21
Dtk_string
This is a high level string class.
Definition: dtk_string.hpp:58
DTK_VAL_TYPE_PNT
@ DTK_VAL_TYPE_PNT
Definition: dtk_val.hpp:25
Dtk_UUID
Definition: dtk_uuid.hpp:8
Dtk_Val::GetIntArray
Dtk_tab< Dtk_Int32 > GetIntArray(Dtk_status &st=Dtk_status::GetDefaultStatus()) const
DTK_VAL_TYPE_UUID
@ DTK_VAL_TYPE_UUID
Definition: dtk_val.hpp:27
Dtk_Val::tab_val
Dtk_tab< Dtk_Int32 > * tab_val
Definition: dtk_val.hpp:78
Dtk_status
Definition: dtk_status.hpp:16
Dtk_Detail_Val::Binder::GetPointer
static T * GetPointer(void *u)
Definition: dtk_val.hpp:50
Dtk_Val::buffer_val
Dtk_tab< Dtk_Char8 > * buffer_val
Definition: dtk_val.hpp:79
Dtk_Val::GetUuid
Dtk_UUID GetUuid(Dtk_status &st=Dtk_status::GetDefaultStatus()) const
Dtk_Val::_reset
void _reset() DTK_NOEXCEPT
DTK_VAL_TYPE_INT
@ DTK_VAL_TYPE_INT
Definition: dtk_val.hpp:20
Dtk_Double64
double Dtk_Double64
Definition: define.h:699
DTK_VAL_TYPE_DIR
@ DTK_VAL_TYPE_DIR
Definition: dtk_val.hpp:26
DTK_VAL_TYPE_UNKNOWN
@ DTK_VAL_TYPE_UNKNOWN
Definition: dtk_val.hpp:19
Dtk_Val::GetBufferData
Dtk_tab< Dtk_Char8 > GetBufferData(Dtk_status &st=Dtk_status::GetDefaultStatus()) const
Dtk_Val
Definition: dtk_val.hpp:67
dtk_uuid.hpp
Dtk_Val::GetInt
int GetInt(Dtk_status &st=Dtk_status::GetDefaultStatus()) const
dtk_pnt.hpp
Dtk_Detail_Val::Value::is_trivial
static bool const is_trivial
Definition: dtk_val.hpp:34
Dtk_Detail_Val::Value
Definition: dtk_val.hpp:34
Dtk_Val::pnt_val
Dtk_pnt * pnt_val
Definition: dtk_val.hpp:75
Dtk_Val::Get
std::pair< bool, T > Get() const
Definition: dtk_val.hpp:116
Dtk_Val::string_val
Dtk_string * string_val
Definition: dtk_val.hpp:73
Dtk_Int32
int32_t Dtk_Int32
Definition: define.h:687
Dtk_Val::GetIf
T const * GetIf() const
Definition: dtk_val.hpp:128
Dtk_Char8
char Dtk_Char8
Definition: define.h:697
Dtk_Detail_Val::Binder
Definition: dtk_val.hpp:49
DTK_VAL_TYPE_BUFFERDATA
@ DTK_VAL_TYPE_BUFFERDATA
Definition: dtk_val.hpp:29
error_dtk.hpp
Dtk_ErrorStatus
Dtk_ErrorStatus
Definition: error_dtk.hpp:6
Dtk_Val::GetPnt
Dtk_pnt GetPnt(Dtk_status &st=Dtk_status::GetDefaultStatus()) const
Dtk_Val::_val
union Dtk_Val::@0 _val
Dtk_Val::GetString
Dtk_string GetString(Dtk_status &st=Dtk_status::GetDefaultStatus()) const
Dtk_pnt
This is a mathematical point class.
Definition: dtk_pnt.hpp:22
Dtk_Val::_move
void _move(Dtk_Val &&) DTK_NOEXCEPT
Dtk_Val::~Dtk_Val
~Dtk_Val()
Dtk_Val::GetDir
Dtk_dir GetDir(Dtk_status &st=Dtk_status::GetDefaultStatus()) const
util_stl_dtk.hpp
Dtk_Val::GetIf
T * GetIf()
Definition: dtk_val.hpp:123
define.h
Dtk_Val::GetDouble
double GetDouble(Dtk_status &st=Dtk_status::GetDefaultStatus()) const
Dtk_Detail_Val::ValueFor
Definition: dtk_val.hpp:36
Dtk_tab
This is a high level array class.
Definition: util_stl_dtk.hpp:85
Dtk_Val::uuid_val
Dtk_UUID * uuid_val
Definition: dtk_val.hpp:77
Dtk_Val::char_val
Dtk_Char8 char_val
Definition: dtk_val.hpp:71
DTK_VAL_TYPE_INTARRAY
@ DTK_VAL_TYPE_INTARRAY
Definition: dtk_val.hpp:28
Dtk_Val::operator<<
friend std::ostream & operator<<(std::ostream &o, const Dtk_Val &d)
Dtk_Val::int_val
Dtk_Int32 int_val
Definition: dtk_val.hpp:70
Dtk_Val::GetRgb
Dtk_RGB GetRgb(Dtk_status &st=Dtk_status::GetDefaultStatus()) const
DTK_VAL_TYPE_STRING
@ DTK_VAL_TYPE_STRING
Definition: dtk_val.hpp:23
dtk_string.hpp
Dtk_val_type
Dtk_val_type
Definition: dtk_val.hpp:18
Dtk_Detail_Val::Binder::GetPointer
static T const * GetPointer(void const *u)
Definition: dtk_val.hpp:54
dtk_rgb.hpp
Dtk_RGB
Definition: dtk_rgb.hpp:7
DTK_VAL_TYPE_DOUBLE
@ DTK_VAL_TYPE_DOUBLE
Definition: dtk_val.hpp:22
Dtk_dir
This is a mathematical direction class.
Definition: dtk_dir.hpp:15
Dtk_Val::rgb_val
Dtk_RGB * rgb_val
Definition: dtk_val.hpp:74
Dtk_Val::GetValType
Dtk_val_type GetValType() const
Dtk_Val::GetChar
char GetChar(Dtk_status &st=Dtk_status::GetDefaultStatus()) const
DTK_NOEXCEPT
#define DTK_NOEXCEPT
Definition: config.hpp:26