RINGMesh  Version 5.0.0
A programming library for geological model meshes
common.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2017, Association Scientifique pour la Geologie et ses
3  * Applications (ASGA). All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of ASGA nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ASGA BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * http://www.ring-team.org
28  *
29  * RING Project
30  * Ecole Nationale Superieure de Geologie - GeoRessources
31  * 2 Rue du Doyen Marcel Roubault - TSA 70605
32  * 54518 VANDOEUVRE-LES-NANCY
33  * FRANCE
34  */
35 
36 #pragma once
37 
38 /* Include the configuration file generated by CMAKE
39  from cmake config options and the
40  include/ringmesh/config_ringmesh.h.in file.
41  File is in RINGMESH_BIN/ringmesh directory.
42  */
43 #include <ringmesh/ringmesh_config.h>
44 #include <ringmesh/ringmesh_export.h>
45 
46 #if defined( _WIN32 )
47 #ifndef WIN32
48 #define WIN32
49 #endif
50 #endif
51 
52 #ifndef NDEBUG
53 #define RINGMESH_DEBUG
54 #else
55 #undef RINGMESH_DEBUG
56 #endif
57 
58 #ifdef WIN32
59 #pragma warning( \
60  disable : 4267 ) // conversion between long unsigned int and unsigned int
61 #pragma warning( disable : 4250 ) // warning about diamond inheritance
62 #pragma warning( disable : 4251 ) // dll interface warnings
63 #pragma warning( disable : 4275 ) // let's pray we have no issues
64 #endif
65 
66 #define ringmesh_disable_copy( Class ) \
67 public: \
68  Class( const Class& ) = delete; \
69  Class& operator=( const Class& ) = delete
70 
71 #define ringmesh_disable_move( Class ) \
72 public: \
73  Class( const Class&& ) = delete; \
74  Class& operator=( Class&& ) = delete
75 
76 #define ringmesh_disable_copy_and_move( Class ) \
77  ringmesh_disable_copy( Class ); \
78  ringmesh_disable_move( Class )
79 
80 #define ringmesh_template_assert_2d_or_3d( type ) \
81  static_assert( \
82  ( type ) == 2 || type == 3, #type " template should be 2 or 3" )
83 
84 #define ringmesh_template_assert_3d( type ) \
85  static_assert( ( type ) == 3, #type " template should be 3" )
86 
87 #define ALIAS_2D( Class ) using Class##2D = Class< 2 >
88 
89 #define ALIAS_3D( Class ) using Class##3D = Class< 3 >
90 
91 #define ALIAS_2D_AND_3D( Class ) \
92  ALIAS_2D( Class ); \
93  ALIAS_3D( Class )
94 
95 #define FORWARD_DECLARATION_DIMENSION_CLASS( Class ) \
96  template < index_t > \
97  class Class;
98 
99 #define FORWARD_DECLARATION_DIMENSION_STRUCT( Struct ) \
100  template < index_t > \
101  struct Struct;
102 
103 // To avoid unused argument warning in function definition
104 template < typename T >
105 void ringmesh_unused( const T& /*unused*/ )
106 {
107 }
108 
109 #include <future>
110 
111 #include <ringmesh/basic/logger.h>
113 #include <ringmesh/basic/types.h>
114 
115 #include <geogram/basic/string.h>
116 
117 #define DEBUG( a ) Logger::out( "Debug", #a, " = ", a )
118 
119 #include <stdexcept>
120 
121 namespace RINGMesh
122 {
127  void RINGMESH_API configure_geogram();
131  void RINGMESH_API configure_ringmesh();
132  void RINGMESH_API default_configure();
133 
134  void RINGMESH_API print_header_information();
135 
151  class RINGMESH_API RINGMeshException : public std::runtime_error
152  {
153  public:
154  template < typename... Args >
156  std::string category, const Args&... messages )
157  : std::runtime_error( string_concatener( messages... ) ),
158  category_( std::move( category ) )
159  {
160  }
161  virtual ~RINGMeshException() throw()
162  {
163  }
164 
165  const std::string& category() const
166  {
167  return category_;
168  }
169 
170  private:
171  template < typename A0 >
172  std::string string_concatener( const A0& a0 )
173  {
174  return GEO::String::to_string( a0 );
175  }
176 
177  template < typename A0, typename A1, typename... Args >
178  std::string string_concatener(
179  const A0& a0, const A1& a1, const Args&... args )
180  {
181  return GEO::String::to_string( a0 )
182  + string_concatener( a1, args... );
183  }
184 
185  protected:
186  std::string category_{};
187  };
188 
202  class RINGMESH_API range
203  {
204  public:
205  template < typename T1, typename T2 >
206  range( T1 begin, T2 end )
207  : iter_( static_cast< index_t >( begin ) ),
208  last_( static_cast< index_t >( end ) )
209  {
210  }
211  template < typename T >
212  explicit range( T end ) : last_( static_cast< index_t >( end ) )
213  {
214  }
215  // Iterable functions
216  const range& begin() const
217  {
218  return *this;
219  }
220  const range& end() const
221  {
222  return *this;
223  }
224  // Iterator functions
225  bool operator!=( const range& /*unused*/ ) const
226  {
227  return iter_ < last_;
228  }
229  void operator++()
230  {
231  ++iter_;
232  }
233  index_t operator*() const
234  {
235  return iter_;
236  }
237 
238  protected:
239  index_t iter_{ 0 };
240  index_t last_{ 0 };
241  };
242 
243  template < typename ACTION >
244  void parallel_for( index_t size, const ACTION& action )
245  {
246  if( size == 0 )
247  {
248  return;
249  }
250  index_t nb_threads{ std::min(
251  size, std::thread::hardware_concurrency() ) };
252  std::vector< std::future< void > > futures;
253  futures.reserve( nb_threads );
254  index_t start{ 0 };
255  auto action_per_thread = [&action]( index_t start, index_t end ) {
256  for( auto i : range( start, end ) )
257  {
258  action( i );
259  }
260  };
261  index_t nb_tasks_per_thread{ size / nb_threads };
262  for( auto thread : range( nb_threads - 1 ) )
263  {
264  ringmesh_unused( thread );
265  futures.emplace_back( std::async( std::launch::async,
266  action_per_thread, start, start + nb_tasks_per_thread ) );
267  start += nb_tasks_per_thread;
268  }
269  futures.emplace_back(
270  std::async( std::launch::async, action_per_thread, start, size ) );
271  for( auto& future : futures )
272  {
273  future.get();
274  }
275  }
276 } // namespace RINGMesh
bool operator!=(const range &) const
Definition: common.h:225
void RINGMESH_API print_header_information()
Definition: common.cpp:106
index_t operator*() const
Definition: common.h:233
void RINGMESH_API configure_geogram()
Definition: common.cpp:66
void ringmesh_unused(const T &)
Definition: common.h:105
std::string string_concatener(const A0 &a0, const A1 &a1, const Args &... args)
Definition: common.h:178
const range & begin() const
Definition: common.h:216
std::string string_concatener(const A0 &a0)
Definition: common.h:172
range(T1 begin, T2 end)
Definition: common.h:206
void operator++()
Definition: common.h:229
const range & end() const
Definition: common.h:220
range(T end)
Definition: common.h:212
virtual ~RINGMeshException()
Definition: common.h:161
RINGMeshException(std::string category, const Args &... messages)
Definition: common.h:155
const std::string & category() const
Definition: common.h:165
Classes to build GeoModel from various inputs.
Definition: algorithm.h:48
void RINGMESH_API configure_ringmesh()
Definition: common.cpp:84
void RINGMESH_API default_configure()
Definition: common.cpp:99
void parallel_for(index_t size, const ACTION &action)
Definition: common.h:244