NSVD Reader  0.0.1
dim_helper.hpp
Go to the documentation of this file.
1 
5 #ifndef __NODAMUSHI_SVD_NORMALIZED_DIM_HELPER_HPP__
6 #define __NODAMUSHI_SVD_NORMALIZED_DIM_HELPER_HPP__
7 
11 
12 namespace nodamushi{
13 namespace svd{
14 namespace normalized{
15 
16 
17 
18 
20 template<typename NODE>
21 size_t get_dim_count(const NODE& n){
22  return n.dim && *n.dim!=0? *n.dim : 1;
23 }
24 
28 template<typename STR>struct dim_name_helper
29 {
30  const STR* str;
33  size_t first_end;
34  size_t second_begin;
35 
36  template<typename NODE>
37  dim_name_helper(const NODE& n,const STR* string):
38  str(string),
39  dimIndex(n.dimIndex? &n.dimIndex.get() : nullptr),
40  type(dim_type::NON),
41  first_end(0),
43  {
44  if(!string)return;
45  const STR& s = *string;
46  first_end = s.length();
47  if(s.length() < 2) return;
48  auto p = s.find("%s");
49 
50  // not array,seq
51  if(p == find_npos)return;
52 
53  auto size = s.size();
54 
55  // not array
56  if(s[size-1] != ']'){
58  first_end = p;
59  second_begin = p+2;
60  return;
61  }
62 
63  size_t i = size -2;
64  // skip space
65  while(i != 0 && (s[i] == ' ' || s[i] == '\t')) --i;
66 
67  // %s ?
68  if(i < 3 || s[i-1] != '%' || s[i] != 's' ){
69  // seq
71  first_end = p;
72  second_begin = p+2;
73  }
74  i-=2;// %s
75  // skip space
76  while(i != 0 && (s[i] == ' ' || s[i] == '\t')) --i;
77 
78  if(s[i] == '['){
80  first_end = i+1;
81  second_begin = s.length()-1;
82  }else{
84  first_end = p;
85  second_begin = p+2;
86  }
87  }
88  operator bool()const noexcept{return type != dim_type::NON;}
89  is_array()const noexcept{return type == dim_type::ARRAY;}
90  is_seq()const noexcept{return type == dim_type::SEQ;}
91 
92  std::string operator()(size_t index)const{
93  if(!str || first_end == 0)return "";
94  if(type == dim_type::NON){
95  return std::string(*str);// TODO
96  }
97  const std::string& n = *str;
98  std::string s = n.substr(0,first_end);
99 
100  if(type != dim_type::NON){
101  if(dimIndex){
102  const auto& dimi = *dimIndex;
103  if(dimi.size() <= index) s.append(std::to_string(index));
104  else s.append(dimi[index]);
105  }else s.append(std::to_string(index));
106  if(second_begin != find_npos)
107  s.append(n,second_begin,n.length()-second_begin);
108  }
109  return s;
110  }
111 };
112 
113 
114 template<typename NODE>
115 auto make_dim_name_helper(const NODE& n)
116  ->dim_name_helper<typename decltype(n.name)::type>
117 {
118  return {n,n.name? &n.name.get() : nullptr};
119 }
120 
121 template<typename NODE>
122 auto make_dim_displayName_helper(const NODE& n)
123  ->dim_name_helper<typename decltype(n.displayName)::type>
124 {
125  return {n,n.displayName? &n.displayName.get() : nullptr};
126 }
127 
128 
129 template<typename NODE>
131 {
132  using name_helper = decltype(make_dim_name_helper(*((NODE*)0)));
133  const NODE& node;
135 
136  size_t size;
138 
140  return {name.type,0,size,0};
141  }
142 
143  dim_helper(const NODE& n):
144  node(n),
145  name(n, (n.name? &n.name.get() : nullptr) ),
146  size(get_dim_count(n)),
148  (n.dimIncrement && n.dimIncrement.get() > 0)?n.dimIncrement.get() : 1)
149  {}
150 
151  operator bool()const noexcept{return name.type != dim_type::NON;}
152  is_array()const noexcept{return name.type == dim_type::ARRAY;}
153  is_seq()const noexcept{return name.type == dim_type::SEQ;}
154 
155  size_t offset(size_t index)
156  {
157  return data_byte_size * index ;
158  }
159 
160 };
161 
162 template<typename NODE>
163 inline dim_helper<NODE> make_dim_helper(const NODE& n){return dim_helper<NODE>(n);}
164 
165 
166 
167 template<typename NODELIST>
168 size_t calcChildrenSize(const NODELIST& list)
169 {
170  size_t count=0;
171  for(const auto& f:list){
172  if(f.dim && f.dim!=0) count += *f.dim;
173  else count ++;
174  }
175  return count;
176 }
177 
178 
179 
180 
181 }}}// end namespace nodamushi
182 
183 #endif // __NODAMUSHI_SVD_NORMALIZED_DIM_HELPER_HPP__
auto make_dim_displayName_helper(const NODE &n) -> dim_name_helper< typename decltype(n.displayName)::type >
Definition: dim_helper.hpp:122
dim_helper< NODE > make_dim_helper(const NODE &n)
Definition: dim_helper.hpp:163
auto make_dim_name_helper(const NODE &n) -> dim_name_helper< typename decltype(n.name)::type >
Definition: dim_helper.hpp:115
dim_type
Array or Sequence or not.
Definition: dim_info.hpp:16
constexpr auto find_npos
Definition: string_type.hpp:47
dim_name_helper(const NODE &n, const STR *string)
Definition: dim_helper.hpp:37
std::string operator()(size_t index) const
Definition: dim_helper.hpp:92
size_t get_dim_count(const NODE &n)
get dim size. when dim is empty return 1.
Definition: dim_helper.hpp:21
decltype(make_dim_name_helper(*((NODE *) 0))) name_helper
Definition: dim_helper.hpp:132
void append(std::string &dst, size_t index) const
Definition: dim_type.hpp:258
dim,dimIndex,array information object.
Definition: dim_info.hpp:27
vector<pointer<T>>.
Definition: boxvec.hpp:75
size_t calcChildrenSize(const NODELIST &list)
Definition: dim_helper.hpp:168
constexpr small library
dimension information