libpnicore
scalar.hpp
1 //
2 // (c) Copyright 2011 DESY, Eugen Wintersberger <eugen.wintersberger@desy.de>
3 //
4 // This file is part of libpnicore.
5 //
6 // libpnicore is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // libpnicore is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with libpnicore. If not, see <http://www.gnu.org/licenses/>.
18 //
19 // ============================================================================
20 //
21 // Created on: Jun 9, 2011
22 // Author: Eugen Wintersberger
23 //
24 //
25 #pragma once
26 
27 #include <iostream>
28 #include <array>
29 
30 #include "../types.hpp"
31 #include "scalar_iterator.hpp"
32 
33 namespace pni {
34 namespace core {
35 
36  template<typename ATYPE> class array_view;
37 
47  template<typename T>
48  class scalar
49  {
50  private:
52  std::array<T,1> _data;
53  public:
54  //================public data types================================
56  typedef T value_type;
60  typedef std::array<T,1> storage_type;
67 
68  //===============public members====================================
71 
72  //====================constructors and destructor==================
74  scalar():_data() {}
75 
76  //-----------------------------------------------------------------
78  scalar(const array_type &s):_data(s._data) {}
79 
80  //-----------------------------------------------------------------
82  scalar(const value_type &r):_data(storage_type{{r}}) {}
83 
84  //-----------------------------------------------------------------
85 
86  //-----------------------------------------------------------------
88  ~scalar() {}
89 
90  //=================assignment operators============================
98  array_type &operator=(const value_type &v)
99  {
100  this->_data[0] = v;
101  return *this;
102  }
103 
104  //-----------------------------------------------------------------
113  array_type &operator=(const array_type &v)
114  {
115  if(this == &v) return *this;
116  this->_data = v._data;
117  return *this;
118  }
119 
120  //=======================conversion operator========================
129 #pragma GCC diagnostic push
130 #pragma GCC diagnostic ignored "-Wunused-parameter"
131  operator T() const { return this->_data[0]; }
132 #pragma GCC diagnostic pop
133 
134  //===================linear access operators========================
144  T operator[](size_t) const { return this->_data[0]; }
145 
146  //-----------------------------------------------------------------
157  T &operator[](size_t) { return this->_data[0]; }
158 
159  //-----------------------------------------------------------------
169  T at(size_t) const { return this->_data[0]; }
170 
171  //-----------------------------------------------------------------
181  T &at(size_t) { return this->_data[0]; }
182 
183  //-----------------------------------------------------------------
191  void insert(size_t,const T &v) { this->_data[0] = v; }
192 
193  //-----------------------------------------------------------------
202  const storage_type &storage() const { return this->_data; }
203 
204  //==================methods for multidim data access===============
215  template<typename ...ITYPES>
216  T &operator()(ITYPES ...)
217  {
218  return this->_data[0];
219  }
220 
221  //-----------------------------------------------------------------
232  template<typename ...ITYPES>
233  T operator()(ITYPES ...) const
234  {
235  return this->_data[0];
236  }
237 
238  //-----------------------------------------------------------------
250  template<
251  template<typename ...> class CTYPE,
252  typename ...OTS
253  >
254  T &operator()(const CTYPE<OTS...> &)
255  {
256  return this->_data[0];
257  }
258 
259  //-----------------------------------------------------------------
271  template<template<typename ...> class CTYPE,typename ...OTS>
272  T operator()(const CTYPE<OTS...> &) const
273  {
274  return this->_data[0];
275  }
276 
277  //===================inquery methods===============================
287  size_t size() const { return 1; }
288 
289  //-----------------------------------------------------------------
297  size_t rank() const { return 0; }
298 
299  //------------------------------------------------------------------
308  template<typename CTYPE>
309  CTYPE shape() const { return CTYPE(); }
310 
311  //------------------------------------------------------------------
313  iterator begin() { return iterator(this,0); }
314 
315  //------------------------------------------------------------------
317  iterator end() { return iterator(this,0); }
318 
319  //------------------------------------------------------------------
321  const_iterator begin() const { const_iterator(this,0); }
322 
323  //------------------------------------------------------------------
325  const_iterator end() const { return const_iterator(this,0); }
326  };
327 
328  //-------------------------------------------------------------------------
340  template<typename T>
341  bool operator==(const scalar<T> &a,const scalar<T> &b)
342  {
343  return T(a)==T(b);
344  }
345 
346  //-------------------------------------------------------------------------
358  template<typename T>
359  bool operator!=(const scalar<T> &a,const scalar<T> &b)
360  {
361  return !(a==b);
362  }
363 
364  //-------------------------------------------------------------------------
374  template<typename T>
375  std::ostream &operator<<(std::ostream &os,const scalar<T> &s)
376  {
377  T value = s[0];
378  os<<value;
379  return os;
380  }
381 
382  //-------------------------------------------------------------------------
392  template<typename T>
393  std::istream &operator>>(std::istream &is,scalar<T> &s)
394  {
395  T value;
396  is>>value;
397  s = value;
398  return is;
399  }
400 
401 //end of namespace
402 }
403 }
scalar_iterator< const array_type > const_iterator
const iterator
Definition: scalar.hpp:64
const_iterator begin() const
get const iterator to first element
Definition: scalar.hpp:321
size_t size() const
get size
Definition: scalar.hpp:287
array_type & operator=(const array_type &v)
assignment from Scalar
Definition: scalar.hpp:113
std::array< T, 1 > _data
static buffer holding the data value
Definition: scalar.hpp:52
std::istream & operator>>(std::istream &is, scalar< T > &s)
input strema data
Definition: scalar.hpp:393
T operator[](size_t) const
get data
Definition: scalar.hpp:144
array_type & operator=(const value_type &v)
assignment from T value
Definition: scalar.hpp:98
scalar(const array_type &s)
copy constructor
Definition: scalar.hpp:78
scalar_iterator< array_type > iterator
iterator
Definition: scalar.hpp:62
T at(size_t) const
get data value
Definition: scalar.hpp:169
T & operator()(const CTYPE< OTS...> &)
get reference to data
Definition: scalar.hpp:254
scalar()
default constructor
Definition: scalar.hpp:74
T & operator[](size_t)
get reference to data
Definition: scalar.hpp:157
CTYPE shape() const
get shape
Definition: scalar.hpp:309
Definition: add_op.hpp:29
iterator begin()
get iterator to first element
Definition: scalar.hpp:313
scalar(const value_type &r)
constructor from a scalar value
Definition: scalar.hpp:82
T & operator()(ITYPES...)
get reference to the data
Definition: scalar.hpp:216
T operator()(ITYPES...) const
get value of data
Definition: scalar.hpp:233
size_t rank() const
get rank
Definition: scalar.hpp:297
void insert(size_t, const T &v)
set data value
Definition: scalar.hpp:191
const storage_type & storage() const
get reference to the storage object
Definition: scalar.hpp:202
Scalar template for scalar values.
Definition: scalar.hpp:48
map from a type to type_id
Definition: type_id_map.hpp:55
scalar< T > array_type
type of the scalar itself
Definition: scalar.hpp:58
~scalar()
destructor
Definition: scalar.hpp:88
array_view< array_type > view_type
view type
Definition: scalar.hpp:66
T operator()(const CTYPE< OTS...> &) const
get data value
Definition: scalar.hpp:272
T value_type
native data type of the scalar
Definition: scalar.hpp:56
static const type_id_t type_id
type ID of the data type held by the scalar
Definition: scalar.hpp:70
scalar iterator
Definition: scalar_iterator.hpp:57
T & at(size_t)
get data reference
Definition: scalar.hpp:181
bool operator!=(const scalar< T > &a, const scalar< T > &b)
!= operator for scalar
Definition: scalar.hpp:359
const_iterator end() const
get const iterator to last+1 element
Definition: scalar.hpp:325
bool operator==(const scalar< T > &a, const scalar< T > &b)
== operator for scalar
Definition: scalar.hpp:341
std::array< T, 1 > storage_type
storage type
Definition: scalar.hpp:60
iterator end()
get iterator to last+1 element
Definition: scalar.hpp:317
type_id_t
type codes for PNI data types
Definition: types/types.hpp:148
type erasure for POD data
Definition: value.hpp:46
provides a view on a part of an array
Definition: add_op.hpp:32