GranOO  3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
MArray.hpp
Go to the documentation of this file.
1 // This file is part of GranOO, a workbench for DEM simulation.
2 //
3 // Author(s) : - Damien Andre IRCER/UNILIM, Limoges France
4 // <damien.andre@unilim.fr>
5 // - Jean-luc Charles Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
6 // <jean-luc.charles@ensam.eu>
7 // - Jeremie Girardot Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
8 // <jeremie.girardot@ensam.eu>
9 // - Cedric Hubert LAMIH/UPHF, Valenciennes France
10 // <cedric.hubert@uphf.fr>
11 // - Ivan Iordanoff Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
12 // <ivan.iordanoff@ensam.eu>
13 //
14 // Copyright (C) 2008-2019 D. Andre, JL. Charles, J. Girardot, C. Hubert, I. Iordanoff
15 //
16 // This program is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 // This program is distributed in the hope that it will be useful,
22 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 // GNU General Public License for more details.
25 //
26 // You should have received a copy of the GNU General Public License
27 // along with this program. If not, see <http://www.gnu.org/licenses/>.
28 
29 
30 #ifndef _libCore_MArray_hpp_
31 #define _libCore_MArray_hpp_
32 
33 #include <boost/archive/text_oarchive.hpp>
34 #include <boost/archive/text_iarchive.hpp>
35 
36 #include "GranOO3/Core/SetOf.hpp"
37 #include "GranOO3/3rdParty/Eigen/Dense"
38 
39 namespace GranOO3
40 {
41  namespace Core
42  {
43 
44  // MPtr is a small utility class able to switch between a legacy object and a pointer
45  template < typename T>
46  class MPtr
47  {
48  public:
49  template<typename... Args> explicit MPtr(Args... args);
50  explicit MPtr(const T&);
51 
52 
53  const T* operator->() const;
54  T* operator->();
55 
56  T& operator* ();
57  const T& operator* () const;
58 
59  void set_ptr(T* pt);
60  T*& ptr();
61  const T*& get_ptr() const;
62 
64  template<class Archive> void serialize(Archive&, const unsigned int);
65 
66  T& operator=(const T& v ) {*_p= v; return *_p;};
67  MPtr& operator=(const MPtr& v) {*_p=*v; return *this;};
68  template<typename T1> void operator+= (T1 v) {*_p+=v;}
69  template<typename T1> void operator-= (T1 v) {*_p-=v;}
70  template<typename T1> void operator*= (T1 v) {*_p*=v;}
71  template<typename T1> void operator/= (T1 v) {*_p/=v;}
72 
73  private:
74  MPtr(const MPtr&) = delete;
75 
76 
77  private:
78  T _t;
79  T* _p;
80  };
81 
82 
83 
84  // MArray class is able to initialize an Eigen::Vector from a collection of C objects
85  // It changes on-the-fly the MPtr objects for pointing to the good position in the Eigen::Vector
86  // MArray should not be used as this, this an intermediate class
87  class MArray
88  {
89  public:
90  static constexpr int N();
91 
92  template <typename C, typename T, typename... Rest>
93  static constexpr int N(Core::MPtr<T>& (C::*f)(), Rest... rest);
94 
95 
96  public:
97  MArray();
98 
99 
100  template <typename C, typename T, typename... Rest>
101  void init(const std::vector<C*>& set, Core::MPtr<T>& (C::*f)(), Rest... rest);
102 
103  template <typename C>
104  void fill(size_t index, C* item);
105 
106  template <typename C, typename T, typename... Rest>
107  void fill(size_t index, C* item, Core::MPtr<T>& (C::*f)(), Rest... rest);
108 
109  Eigen::VectorXd& array();
110  const Eigen::VectorXd& array() const;
111 
112  private:
113  Eigen::VectorXd _array;
114  bool _init;
115  };
116 
117 
118 
119 
120  }
121 }
122 
123 
124 #include "GranOO3/Core/MArray.tpp"
125 
126 
127 
128 #endif
Definition: MArray.hpp:88
bool _init
Definition: MArray.hpp:114
Eigen::VectorXd & array()
static constexpr int N(Core::MPtr< T > &(C::*f)(), Rest... rest)
void fill(size_t index, C *item, Core::MPtr< T > &(C::*f)(), Rest... rest)
const Eigen::VectorXd & array() const
static constexpr int N()
void init(const std::vector< C * > &set, Core::MPtr< T > &(C::*f)(), Rest... rest)
Eigen::VectorXd _array
Definition: MArray.hpp:113
void fill(size_t index, C *item)
Definition: MArray.hpp:47
MPtr & operator=(const MPtr &v)
Definition: MArray.hpp:67
void operator/=(T1 v)
Definition: MArray.hpp:71
void operator+=(T1 v)
Definition: MArray.hpp:68
T & operator=(const T &v)
Definition: MArray.hpp:66
T _t
Definition: MArray.hpp:78
MPtr(const MPtr &)=delete
void operator-=(T1 v)
Definition: MArray.hpp:69
const T *& get_ptr() const
MPtr(Args... args)
void operator*=(T1 v)
Definition: MArray.hpp:70
const T * operator->() const
T * _p
Definition: MArray.hpp:79
friend class boost::serialization::access
Definition: MArray.hpp:63
void set_ptr(T *pt)
void serialize(Archive &, const unsigned int)
Definition: Common.hpp:198
const T1
Definition: Exprtk.hpp:16489