NSVD Reader  0.0.1
Cluster.hpp
Go to the documentation of this file.
1 
5 /*
6  * These codes are licensed under CC0.
7  * http://creativecommons.org/publicdomain/zero/1.0/
8  */
9 #ifndef __NODAMUSHI_SVD_NORMALIZED_CLUSTER_HPP__
10 #define __NODAMUSHI_SVD_NORMALIZED_CLUSTER_HPP__
11 
13 
14 namespace nodamushi{
15 namespace svd{
16 namespace normalized{
17 
18 /*
19  * @brief Normalized cluster class.
20  * if STRREF is string_view or something like it,
21  * the reference source of string is the value of nodamushi::svd::Cluster instance member.
22  *
23  * @see http://www.keil.com/pack/doc/CMSIS/SVD/html/elem_registers.html#elem_cluster
24  * @see nodamushi::svd::Cluster
25  */
26 template<typename STRREF>struct Cluster
27 {
34 
35 
36 private:
38  p_ptr parent;
40  p2_ptr parent2;
41 public:
45  std::string name;
49  STRREF description;
70  uint64_t addressOffset;
71 
97 
119  //-----------------------------------------------------------
125  node_ptr<Peripheral> get_parent() noexcept{return parent.lock();}
131  node_ptr<Cluster> get_parent2() noexcept{return parent2.lock();}
137  node_ptr<Peripheral const> get_parent()const noexcept{return parent.lock();}
138 
144  node_ptr<Cluster const> get_parent2()const noexcept{return parent2.lock();}
145 
150 
155  uint64_t get_address()const{ return calc_address(*this);}
157  uint64_t get_address_offset()const{ return calc_offset(*this);}
159  uint32_t get_size()const{
160  return size.get<this_t,get_default_size<this_t>>(this);
161  }
163  uint64_t get_resetValue()const{
164  return resetValue.get<this_t,get_default_resetValue<this_t>>(this);
165  }
167  uint64_t get_resetMask()const{
168  return resetMask.get<this_t,get_default_resetMask<this_t>>(this);
169  }
172  return access.get<this_t,get_default_access<this_t>>(this);
173  }
176  return protection.get<this_t,get_default_protection<this_t>>(this);
177  }
178  //-------------------------------------------
185  template<typename STR>
187  {
188  const size_t ps = p.size();
189  if(ps > pindex){
190  string_ref n = p[pindex];
191  if(auto c = __find_helper(clusters,n)){
192  if(ps == pindex +1)return c;
193  else return c->find(p,pindex+1);
194  }
195  if(auto c = __find_helper(registers,n)){
196  if(ps == pindex +1)return c;
197  else return c->find(p,pindex+1);
198  }
199  }
200  return {};
201  }
208  template<typename STR>
210  {
211  const size_t ps = p.size();
212  if(ps > pindex){
213  string_ref n = p[pindex];
214  if(auto c = __find_helper(clusters,n)){
215  if(ps == pindex +1)return c;
216  else return c->find_cluster(p,pindex+1);
217  }
218  }
219  return {};
220  }
227  template<typename STR>
229  {
230  const size_t ps = p.size();
231  if(ps > pindex){
232  string_ref n = p[pindex];
233  if(ps > pindex + 1){
234  if(auto c = __find_helper(clusters,n))
235  return c->find_register(p,pindex+1);
236  }else if(ps == pindex + 1){
237  return __find_helper(registers,n);
238  }
239  }
240  return {};
241  }
248  template<typename STR>
250  {
251  const size_t ps = p.size();
252  if(ps > pindex+1){
253  string_ref n = p[pindex];
254  if(ps > pindex + 2){
255  if(auto c = __find_helper(clusters,n))
256  return c->find_field(p,pindex+1);
257  }else if(ps == pindex + 2){
258  if(auto c = __find_helper(registers,n))
259  return c->find_field(p,pindex+1);
260  }
261  }
262  return {};
263  }
266  {
268  path_helper(*this,p);
269  return p;
270  }
271 
272  //-------------------------------------------
277  template<typename T>
278  Cluster(const T& n):// 'n' don't change name
279  parent(),
280  parent2(),
281  derivedFrom(),
282  name(n.name),
283  dim(n.dim),
293  clusters(n.clusters.size()),
295  {
296  if(n.svd.derivedFrom)
297  derivedFrom = *n.svd.derivedFrom;
298  // add dim offset
299  if(n.dim && n.dim.index!=0){
300  __NORMALIZED_DERIVED_FROM_HELPER(dimIncrement);
301  size_t inc = 1;
302  if(dimIncrement)
303  inc = *dimIncrement;
304  addressOffset += inc * n.dim.index;
305  }
306 
307  for(const auto& c:n.clusters)
308  clusters.emplace_back(c);
309  for(const auto& c:n.registers)
310  registers.emplace_back(c);
311  // sort by address
312  clusters.sort([](const Cluster& a,const Cluster& b)->bool
313  {return a.addressOffset < b.addressOffset;});
314  registers.sort([](const Register& a,const Register& b)->bool
315  {return a.addressOffset < b.addressOffset;});
316 
317  }
323  void update_parent(p_ptr& new_parent,node_ptr<this_t>& me)
324  {
325  parent = new_parent;
326  parent2= {};
329  }
330 
336  void update_parent(p2_ptr& new_parent,node_ptr<this_t>& me)
337  {
338  parent2= new_parent;
339  parent = {};
342  }
343 
344 };
345 
346 //---------- Visitor --------------------
348 {
349  namespace nv = ::nodamushi::visitor;
350  using r = nv::result;
351  r ret;
352  // visit register / cluster
353  ret = CONTROLLER::apply(t.clusters,v);
354  if(ret == r::BREAK)return r::BREAK;
355  if(ret == r::SKIP)return r::CONTINUE;
356 
357  ret = CONTROLLER::apply(t.registers,v);
358  if(ret == r::BREAK)return r::BREAK;
359  return r::CONTINUE;
360 }};
361 //--------------------------------------------
362 
363 }}} // end namespace
364 #endif // __NODAMUSHI_SVD_NORMALIZED_CLUSTER_HPP__
::nodamushi::svd::path get_path() const
this cluster path
Definition: Cluster.hpp:264
uint64_t calc_address(const O &o, uint64_t offset=0)
std::shared_ptr< T > node_ptr
#define __NORMALIZED_DERIVED_FROM(name)
__NORMALIZED_DERIVED_FROM(name)
__NX_NORM_HANDLE_VISIT(Enumeration)
Definition: Enumeration.hpp:90
size_t size() const noexcept
Definition: path.hpp:218
node_ptr< Cluster const > get_parent2() const noexcept
get parent cluster pointer
Definition: Cluster.hpp:144
nullable< uint64_t > resetMask
<resetMask>
Definition: Cluster.hpp:96
Access get_access() const
resolve the value of access and return it.
Definition: Cluster.hpp:171
node_ptr< Peripheral > get_parent() noexcept
get parent peripheral pointer
Definition: Cluster.hpp:125
nullable< Access > access
<access>
Definition: Cluster.hpp:81
uint64_t addressOffset
<addressOffset>.
Definition: Cluster.hpp:70
const std::string & string_ref
Definition: string_type.hpp:44
parent_ptr< Peripheral< STRREF > > p_ptr
Definition: Cluster.hpp:29
list< Register > registers
<register> elements list. This list is sorted by the addressOffset.
Definition: Cluster.hpp:118
node_ptr< Cluster > find_cluster(::nodamushi::svd::path< STR > &p, size_t pindex=0)
find cluster element
Definition: Cluster.hpp:209
node_ptr< Cluster > get_parent2() noexcept
get parent cluster pointer
Definition: Cluster.hpp:131
uint64_t get_address() const
calculate an absolute address.
Definition: Cluster.hpp:155
node_ptr< Field > find_field(::nodamushi::svd::path< STR > &p, size_t pindex=0)
find field element
Definition: Cluster.hpp:249
node_ptr< Register > find_register(::nodamushi::svd::path< STR > &p, size_t pindex=0)
find register element
Definition: Cluster.hpp:228
container class decralation
nullable< uint64_t > resetValue
<resetValue>
Definition: Cluster.hpp:91
path< STRREF > derivedFrom
derivedFrom attribute
Definition: Cluster.hpp:43
parent_ptr< Cluster< STRREF > > p2_ptr
Definition: Cluster.hpp:30
node_ptr< void > find(::nodamushi::svd::path< STR > &p, size_t pindex=0)
find path element
Definition: Cluster.hpp:186
uint64_t get_resetValue() const
resolve the value of resetValue and return it.
Definition: Cluster.hpp:163
Protection get_protection() const
resolve the value of protection and return it.
Definition: Cluster.hpp:175
void update_parent(p_ptr &new_parent, node_ptr< this_t > &me)
set the peripheral parent
Definition: Cluster.hpp:322
void update_parent_of_children(list< T > &l, node_ptr< You > &you)
std::weak_ptr< Parent > parent_ptr
STRREF headerStructName
<headerStructName>
Definition: Cluster.hpp:55
uint64_t get_resetMask() const
resolve the value of resetMask and return it.
Definition: Cluster.hpp:167
dim_info dim
dimemsion information object.
Definition: Cluster.hpp:47
T get(T defaultValue) const noexcept
Definition: nullable.hpp:70
result apply(T &t)
Definition: imple.hpp:197
Access
Read/Write Access type enum.<access> Read-Write/Read-Only/Write-Only/Write-Once/Read- Write Once.
Definition: Access.hpp:26
dim,dimIndex,array information object.
Definition: dim_info.hpp:27
Cluster(const T &n)
This constructor is for normalizer.
Definition: Cluster.hpp:277
void path_helper(const O &o,::nodamushi::svd::path< S > &path)
uint32_t get_size() const
resolve the value of size and return it.
Definition: Cluster.hpp:159
node_ptr< Peripheral const > get_parent() const noexcept
get parent peripheral pointer
Definition: Cluster.hpp:137
normalized peripheral element
vector<pointer<T>>.
Definition: boxvec.hpp:75
node_ptr< Peripheral const > get_peripheral() const noexcept
get the Peripheral to which this cluster belongs.
Definition: Cluster.hpp:147
uint64_t get_address_offset() const
a relative address from the baseAddress of the Peripheral to which this cluster belongs
Definition: Cluster.hpp:157
Protection
<protection> enum
Definition: Protection.hpp:23
list< Cluster > clusters
<cluster> elements list. This list is sorted by the addressOffset.
Definition: Cluster.hpp:107
uint64_t calc_offset(const O &o, uint64_t offset=0)
nullable< Protection > protection
<protection>
Definition: Cluster.hpp:86
normalized field element
Definition: Field.hpp:32
node_ptr< R > __find_helper(list< R > &v, string_ref n)
STRREF alternateCluster
<alternateCluster>
Definition: Cluster.hpp:53
node_ptr< Peripheral > get_peripheral() noexcept
get the Peripheral to which this cluster belongs.
Definition: Cluster.hpp:149
node_ptr< Peripheral< STRREF > > find_parent_peripheral(O< STRREF > &o)
STRREF description
<description>
Definition: Cluster.hpp:49
nullable< uint32_t > size
<size> default bit-width of registers.
Definition: Cluster.hpp:76
uint64_t addressOffset
<addressOffset>
Definition: Register.hpp:69
#define __NORMALIZED_DERIVED_FROM_HELPER(name)