PseudoTuple.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31 
32 
33 #ifndef DRAIN_UNITUPLE_WRAPPER
34 #define DRAIN_UNITUPLE_WRAPPER
35 
36 #include <cstddef>
37 #include <iostream>
38 #include <sstream>
39 #include <typeinfo>
40 #include <stdexcept>
41 #include <string>
42 //#include <set>
43 
44 #include <drain/StringBuilder.h>
45 #include <drain/Type.h> // Utils
46 // lower == cannot use Sprinter, as long as SprinterLayouit uses UniTuple...
47 // #include <drain/VariableT.h> lower
48 //#include <drain/Sprinter.h>
49 #include "TupleBase.h"
50 
51 namespace drain {
52 
53 
54 /***
55  * \tparam C - base class
56  * \tparam S - storage class of members (int, double, char)
57  * \tparam N - number of the members included
58  */
59 template <class C, typename T=typename C::value_t, size_t N=sizeof(C)/sizeof(T)>
60 class PseudoTuple : public C, public TupleBase<T,N> {
61 
62 public:
63 
64  typedef T* iterator;
65  typedef T const* const_iterator;
66 
67  static const size_t baseTypeSize;
68 
69  const size_t elementCount;
70 
71 
72  inline
73  PseudoTuple() : elementCount(N>0 ? N : baseTypeSize/TupleBase<T,N>::storageTypeSize) {
74  // std::cerr << N << '*' << TupleBase<T,N>::storageTypeSize << " ... " << baseTypeSize << '\n';
75  if (N*TupleBase<T,N>::storageTypeSize > baseTypeSize){
76  throw std::runtime_error(StringBuilder<>(__FUNCTION__, ": conflicting geometry ", N, 'x',
77  TupleBase<T,N>::storageTypeSize," in addressing base class ", TypeName<C>::str(), " size=", baseTypeSize));
78  }
79  };
80 
81  virtual inline
82  const_iterator begin() const {
83  return static_cast<const_iterator>((void *)(const C *)this); //
84  }
85 
86  virtual inline
87  const_iterator end() const {
88  return static_cast<const_iterator>((void *)(const C *)this) + N; //
89  }
90 
91  virtual inline
92  iterator begin(){
93  return static_cast<iterator>((void *)(const C *)this);
94  }
95 
96  virtual inline
97  iterator end(){
98  return static_cast<iterator>((void *)(const C *)this) + N;
99  }
100 
101 
102 protected:
103 
104 };
105 
106 template <class C, typename T, size_t N>
107 const size_t PseudoTuple<C,T,N>::baseTypeSize = sizeof(C);
108 
109 
110 
111 template <class C, typename T, size_t N>
112 struct TypeName<PseudoTuple<C,T,N> > {
113 
114  static const std::string & str(){
115  if (N == 0){
116  static const std::string name = drain::StringBuilder<>("PseudoTuple<", drain::TypeName<C>::str(),',' , drain::TypeName<T>::str(),">");
117  return name;
118  }
119  else {
120  static const std::string name = drain::StringBuilder<>("PseudoTuple<", drain::TypeName<C>::str(),',' , drain::TypeName<T>::str(),',' , N, ">");
121  return name;
122  }
123  }
124 
125 };
126 
127 /*
128 template <class T, size_t N>
129 std::ostream & operator<<(std::ostream & ostr, const UniTuple<T,N> & tuple){
130  return tuple.toStream(ostr);
131 }
132 */
133 
134 
135 
136 
137 /*
138  // NOTE: cannot use Sprinter, as long as SprinterLayout uses UniTuple...
139 template <class T, size_t N>
140 std::ostream & Sprinter::toStream(std::ostream & ostr, const UniTuple<T,N> & tuple, const SprinterLayout & layout) {
141  return Sprinter::sequenceToStream(ostr, tuple, layout);
142 }
143 */
144 
145 } // drain
146 
147 
148 #endif
Definition: PseudoTuple.h:60
Definition: StringBuilder.h:58
Definition: TupleBase.h:54
Definition: DataSelector.cpp:1277
Definition: Type.h:542
static const std::string name
Default implementation: name returned by std::type_info::name()
Definition: Type.h:558