GEOSX
ObjectCatalog.hpp
Go to the documentation of this file.
1 #ifndef GEOSX_DATAREPOSITORY_OBJECTCATALOG_HPP_
2 #define GEOSX_DATAREPOSITORY_OBJECTCATALOG_HPP_
3 /*
4  * ------------------------------------------------------------------------------------------------------------
5  * SPDX-License-Identifier: LGPL-2.1-only
6  *
7  * Copyright (c) 2018-2020 Lawrence Livermore National Security LLC
8  * Copyright (c) 2018-2020 The Board of Trustees of the Leland Stanford Junior University
9  * Copyright (c) 2018-2020 Total, S.A
10  * Copyright (c) 2019- GEOSX Contributors
11  * All rights reserved
12  *
13  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
14  * ------------------------------------------------------------------------------------------------------------
15  */
16 
28 #include "common/Logger.hpp"
29 #include "LvArray/src/system.hpp"
30 
31 #include <unordered_map>
32 #include <string>
33 #include <iostream>
34 #include <memory>
35 
36 #ifndef OBJECTCATALOGVERBOSE
37 
40 #define OBJECTCATALOGVERBOSE 0
41 #endif
42 
43 
44 #ifndef BASEHOLDSCATALOG
45 
48 #define BASEHOLDSCATALOG 1
49 #endif
50 
51 namespace geosx
52 {
53 namespace dataRepository
54 {
55 
62 //START_SPHINX_0
63 template< typename BASETYPE, typename ... ARGS >
65 {
66 public:
67 
69  //START_SPHINX_1
71  std::unique_ptr< CatalogInterface< BASETYPE, ARGS... > > > CatalogType;
72  //STOP_SPHINX
73 
78  {
79 #if OBJECTCATALOGVERBOSE > 1
80  GEOSX_LOG( "Calling constructor for CatalogInterface< " << LvArray::system::demangle( typeid( BASETYPE ).name() ) << " , ... >" );
81 #endif
82  }
83 
88  {
89 #if OBJECTCATALOGVERBOSE > 1
90  GEOSX_LOG( "Calling destructor for CatalogInterface< " << LvArray::system::demangle( typeid( BASETYPE ).name() ) << " , ... >" );
91 #endif
92  }
93 
97  explicit CatalogInterface( CatalogInterface const & ) = default;
98 
102  CatalogInterface( CatalogInterface && ) = default;
103 
108  CatalogInterface & operator=( CatalogInterface const & ) = default;
109 
114  CatalogInterface & operator=( CatalogInterface && ) = default;
115 
121  {
122 #if BASEHOLDSCATALOG == 1
123  return BASETYPE::GetCatalog();
124 #else
125  static CatalogType catalog;
126  return catalog;
127 #endif
128  }
129 
135  virtual std::unique_ptr< BASETYPE > Allocate( ARGS... args ) const = 0;
136 
142  static bool hasKeyName( std::string const & objectTypeName )
143  {
144  return GetCatalog().count( objectTypeName );
145  }
146 
154  //START_SPHINX_2
155  static std::unique_ptr< BASETYPE > Factory( std::string const & objectTypeName, ARGS... args )
156  {
157  return GetCatalog().at( objectTypeName ).get()->Allocate( args ... );
158  }
159  //STOP_SPHINX
160 
171  template< typename TYPE >
172  static TYPE & catalog_cast( BASETYPE & object )
173  {
174  std::string castedName = TYPE::CatalogName();
175  std::string objectName = object.getName();
176 
177  if( castedName != objectName )
178  {
179 #if OBJECTCATALOGVERBOSE > 1
180  GEOSX_LOG( "Invalid Cast of " << objectName << " to " << castedName );
181 #endif
182  }
183 
184  return static_cast< TYPE & >(object);
185  }
186 
187 };
188 
195 //START_SPHINX_3
196 template< typename BASETYPE, typename TYPE, typename ... ARGS >
197 class CatalogEntry final : public CatalogInterface< BASETYPE, ARGS... >
198 {
199 public:
200 
205  CatalogInterface< BASETYPE, ARGS... >()
206  {
207 #if OBJECTCATALOGVERBOSE > 1
208  GEOSX_LOG( "Calling constructor for CatalogEntry< " << LvArray::system::demangle( typeid(TYPE).name())
209  << " , " << LvArray::system::demangle( typeid(BASETYPE).name())
210  << " , ... >" );
211 #endif
212  }
213 
217  ~CatalogEntry() override
218  {
219 #if OBJECTCATALOGVERBOSE > 1
220  GEOSX_LOG( "Calling destructor for CatalogEntry< " << LvArray::system::demangle( typeid(TYPE).name())
221  << " , " << LvArray::system::demangle( typeid(BASETYPE).name())
222  << " , ... >" );
223 #endif
224 
225  }
226 
231  CatalogEntry( CatalogEntry const & source ):
232  CatalogInterface< BASETYPE, ARGS... >( source )
233  {}
234 
240  CatalogInterface< BASETYPE, ARGS... >( std::move( source ))
241  {}
242 
248  CatalogEntry & operator=( CatalogEntry const & source )
249  {
251  }
252 
259  {
261  }
262 
268  //START_SPHINX_4
269  virtual std::unique_ptr< BASETYPE > Allocate( ARGS... args ) const override
270  {
271 #if OBJECTCATALOGVERBOSE > 0
272  GEOSX_LOG( "Creating type " << LvArray::system::demangle( typeid(TYPE).name())
273  << " from catalog of " << LvArray::system::demangle( typeid(BASETYPE).name()));
274 #endif
275 #if ( __cplusplus >= 201402L )
276  return std::make_unique< TYPE >( args ... );
277 #else
278  return std::unique_ptr< BASETYPE >( new TYPE( args ... ) );
279 #endif
280  }
281  //STOP_SPHINX
282 
283 };
284 
285 
291 template< typename BASETYPE, typename TYPE, typename ... ARGS >
293 {
294 public:
300  {
301 #if OBJECTCATALOGVERBOSE > 1
302  GEOSX_LOG( "Calling constructor for CatalogEntryConstructor< " << LvArray::system::demangle( typeid(TYPE).name())
303  << " , " << LvArray::system::demangle( typeid(BASETYPE).name())
304  << " , ... >" );
305 #endif
306 
307  std::string name = TYPE::CatalogName();
308 #if ( __cplusplus >= 201402L )
309  std::unique_ptr< CatalogEntry< BASETYPE, TYPE, ARGS... > > temp = std::make_unique< CatalogEntry< BASETYPE, TYPE, ARGS... > >();
310 #else
311  std::unique_ptr< CatalogEntry< BASETYPE, TYPE, ARGS... > > temp = std::unique_ptr< CatalogEntry< BASETYPE, TYPE, ARGS... > >( new CatalogEntry< BASETYPE,
312  TYPE,
313  ARGS... >() );
314 #endif
315  ( CatalogInterface< BASETYPE, ARGS... >::GetCatalog() ).insert( std::move( std::make_pair( name, std::move( temp ) ) ) );
316 
317 #if OBJECTCATALOGVERBOSE > 0
318  GEOSX_LOG( "Registered " << LvArray::system::demangle( typeid(BASETYPE).name())
319  << " catalog component of derived type "
320  << LvArray::system::demangle( typeid(TYPE).name())
321  << " where " << LvArray::system::demangle( typeid(TYPE).name())
322  << "::CatalogName() = " << TYPE::CatalogName());
323 #endif
324  }
325 
330  {
331 #if OBJECTCATALOGVERBOSE > 1
332  GEOSX_LOG( "Calling destructor for CatalogEntryConstructor< " << LvArray::system::demangle( typeid(TYPE).name())
333  << " , " << LvArray::system::demangle( typeid(BASETYPE).name())
334  << " , ... >" );
335 #endif
336  }
337 
342 
347 
348 
354 
360 
361 };
362 
367 template< typename BASETYPE >
368 class CatalogInterface< BASETYPE >
369 {
370 public:
371 
373  typedef std::unordered_map< std::string, std::unique_ptr< CatalogInterface< BASETYPE > > > CatalogType;
374 
379  {
380 #if OBJECTCATALOGVERBOSE > 1
381  GEOSX_LOG( "Calling constructor for CatalogInterface< " << LvArray::system::demangle( typeid(BASETYPE).name())
382  << " , ... >" );
383 #endif
384  }
385 
390  {
391 #if OBJECTCATALOGVERBOSE > 1
392  GEOSX_LOG( "Calling destructor for CatalogInterface< " << LvArray::system::demangle( typeid(BASETYPE).name())
393  << " , ... >" );
394 #endif
395  }
396 
400  explicit CatalogInterface( CatalogInterface const & ) = default;
401 
405  CatalogInterface( CatalogInterface && ) = default;
406 
411  CatalogInterface & operator=( CatalogInterface const & ) = default;
412 
417  CatalogInterface & operator=( CatalogInterface && ) = default;
418 
423  static CatalogType & GetCatalog()
424  {
425 #if BASEHOLDSCATALOG == 1
426  return BASETYPE::GetCatalog();
427 #else
428  static CatalogType catalog;
429  return catalog;
430 #endif
431  }
432 
437  virtual std::unique_ptr< BASETYPE > Allocate( ) const = 0;
438 
444  static std::unique_ptr< BASETYPE > Factory( std::string const & objectTypeName )
445  {
446  CatalogInterface< BASETYPE > const * const entry = GetCatalog().at( objectTypeName ).get();
447  return entry->Allocate();
448  }
449 
460  template< typename TYPE >
461  static TYPE & catalog_cast( BASETYPE & object )
462  {
463  std::string castedName = TYPE::CatalogName();
464  std::string objectName = object.getName();
465 
466  if( castedName != objectName )
467  {
468 #if OBJECTCATALOGVERBOSE > 1
469  GEOSX_LOG( "Invalid Cast of " << objectName << " to " << castedName );
470 #endif
471  }
472 
473  return static_cast< TYPE & >(object);
474  }
475 
476 };
477 
483 template< typename BASETYPE, typename TYPE >
484 class CatalogEntry< BASETYPE, TYPE > final : public CatalogInterface< BASETYPE >
485 {
486 public:
491  CatalogInterface< BASETYPE >()
492  {
493 #if OBJECTCATALOGVERBOSE > 1
494  GEOSX_LOG( "Calling constructor for CatalogEntry< " << LvArray::system::demangle( typeid(TYPE).name())
495  << " , " << LvArray::system::demangle( typeid(BASETYPE).name())
496  << " , ... >" );
497 #endif
498  }
499 
503  ~CatalogEntry() override
504  {
505 #if OBJECTCATALOGVERBOSE > 1
506  GEOSX_LOG( "Calling destructor for CatalogEntry< " << LvArray::system::demangle( typeid(TYPE).name())
507  << " , " << LvArray::system::demangle( typeid(BASETYPE).name())
508  << " , ... >" );
509 #endif
510 
511  }
512 
517  CatalogEntry( CatalogEntry const & source ):
518  CatalogInterface< BASETYPE >( source )
519  {}
520 
526  CatalogInterface< BASETYPE >( std::move( source ))
527  {}
528 
534  CatalogEntry & operator=( CatalogEntry const & source )
535  {
537  }
538 
545  {
546  CatalogInterface< BASETYPE >::operator=( std::move(source));
547  }
548 
553  virtual std::unique_ptr< BASETYPE > Allocate( ) const override
554  {
555 #if OBJECTCATALOGVERBOSE > 0
556  GEOSX_LOG( "Creating type " << LvArray::system::demangle( typeid(TYPE).name())
557  << " from catalog of " << LvArray::system::demangle( typeid(BASETYPE).name()));
558 #endif
559 #if ( __cplusplus >= 201402L )
560  return std::make_unique< TYPE >( );
561 #else
562  return std::unique_ptr< BASETYPE >( new TYPE( ) );
563 #endif
564  }
565 };
566 
567 
571 template< typename BASETYPE, typename TYPE >
572 class CatalogEntryConstructor< BASETYPE, TYPE >
573 {
574 public:
575 
580  {
581 #if OBJECTCATALOGVERBOSE > 1
582  GEOSX_LOG( "Calling constructor for CatalogEntryConstructor< " << LvArray::system::demangle( typeid(TYPE).name())
583  << " , " << LvArray::system::demangle( typeid(BASETYPE).name())
584  << " , ... >" );
585 #endif
586 
587  std::string name = TYPE::CatalogName();
588 #if ( __cplusplus >= 201402L )
589  std::unique_ptr< CatalogEntry< BASETYPE, TYPE > > temp = std::make_unique< CatalogEntry< BASETYPE, TYPE > >();
590 #else
591  std::unique_ptr< CatalogEntry< BASETYPE, TYPE > > temp = std::unique_ptr< CatalogEntry< BASETYPE, TYPE > >( new CatalogEntry< BASETYPE, TYPE >() );
592 #endif
593  ( CatalogInterface< BASETYPE >::GetCatalog() ).insert( std::move( std::make_pair( name, std::move( temp ) ) ) );
594 
595 #if OBJECTCATALOGVERBOSE > 0
596  GEOSX_LOG( "Registered " << LvArray::system::demangle( typeid(BASETYPE).name())
597  << " catalog component of derived type "
598  << LvArray::system::demangle( typeid(TYPE).name())
599  << " where " << LvArray::system::demangle( typeid(TYPE).name())
600  << "::CatalogName() = " << TYPE::CatalogName());
601 #endif
602  }
603 
608  {
609 #if OBJECTCATALOGVERBOSE > 1
610  GEOSX_LOG( "Calling destructor for CatalogEntryConstructor< " << LvArray::system::demangle( typeid(TYPE).name())
611  << " , " << LvArray::system::demangle( typeid(BASETYPE).name()) << " , ... >" );
612 #endif
613  }
614 
619 
624 
630 
636 
637 };
638 
639 
640 }
641 }
642 
643 
655 #define REGISTER_CATALOG_ENTRY( BaseType, DerivedType, ... ) \
656  namespace { geosx::dataRepository::CatalogEntryConstructor< BaseType, DerivedType, __VA_ARGS__ > catEntry_ ## DerivedType; }
657 
661 #define REGISTER_CATALOG_ENTRY0( BaseType, DerivedType ) \
662  namespace { geosx::dataRepository::CatalogEntryConstructor< BaseType, DerivedType > catEntry_ ## DerivedType; }
663 
664 #endif /* GEOSX_DATAREPOSITORY_OBJECTCATALOG_HPP_ */
virtual std::unique_ptr< BASETYPE > Allocate() const override
Create a new instance of TYPE.
Class to hold allocation capability for specific target derived types.
void insert(OrderedVariableToManyElementRelation &relation, localIndex const firstIndex, localIndex const er, localIndex const esr, localIndex const ei)
Insert an element relation for an object in the relation.
static CatalogType & GetCatalog()
Get the catalog from that is stored in the target base class.
A class to generate the catalog entry.
~CatalogEntry() override
Default destructor.
CatalogInterface & operator=(CatalogInterface const &)=default
Copy assignment operator.
static CatalogType & GetCatalog()
Get the catalog from that is stored in the target base class.
CatalogEntryConstructor()
Default constructor.
CatalogEntry & operator=(CatalogEntry const &source)
Copy assignment operator.
CatalogEntryConstructor()
Constructor creates a catalog entry using the key defined by TYPE::CatalogName(), and value of Catalo...
~CatalogEntryConstructor()
Default destuctor.
CatalogEntry()
Default constructor.
CatalogEntry & operator=(CatalogEntry &&source)
Move assignment operator.
Contains functions that interact with the system or runtime environment.
static std::unique_ptr< BASETYPE > Factory(std::string const &objectTypeName, ARGS... args)
Static method to create a new object that derives from BASETYPE.
CatalogEntry(CatalogEntry const &source)
Copy constructor.
Specialization of CatalogInterface for types with no-argument constructors/.
CatalogEntry()
Default constructor.
Specialization of CatalogEntry for types with no-argument constructors.
CatalogEntry(CatalogEntry &&source)
Move constructor.
static std::unique_ptr< BASETYPE > Factory(std::string const &objectTypeName)
Create a new object that derives from BASETYPE.
virtual std::unique_ptr< BASETYPE > Allocate(ARGS... args) const =0
Create a new object that derives from BASETYPE.
~CatalogEntry() override
Default destructor.
CatalogEntry & operator=(CatalogEntry &&source)
Move assignment operator.
static bool hasKeyName(std::string const &objectTypeName)
Check if catalog contains a given key.
CatalogEntry(CatalogEntry const &source)
Copy constructor.
virtual ~CatalogInterface()
Default destructor.
static TYPE & catalog_cast(BASETYPE &object)
Downcast base type reference to derived type.
#define GEOSX_LOG(...)
Log a message on screen.
Definition: Logger.hpp:35
std::unordered_map< std::string, std::unique_ptr< CatalogInterface< BASETYPE > > > CatalogType
This is the type that will be used for the catalog. The catalog is actually instantiated in the BASET...
This class provides the base class/interface for the catalog value objects.
CatalogEntry(CatalogEntry &&source)
Move constructor.
static TYPE & catalog_cast(BASETYPE &object)
Downcast base type reference to derived type.
mapBase< TKEY, TVAL, std::integral_constant< bool, false > > unordered_map
Unordered map type.
Definition: DataTypes.hpp:390
virtual std::unique_ptr< BASETYPE > Allocate() const =0
Create a new object that derives from BASETYPE.
std::string string
String type.
Definition: DataTypes.hpp:131
std::string demangle(char const *const name)
CatalogEntry & operator=(CatalogEntry const &source)
Copy assignment operator.
std::unordered_map< std::string, std::unique_ptr< CatalogInterface< BASETYPE, ARGS... > > > CatalogType
This is the type that will be used for the catalog. The catalog is actually instantiated in the BASET...
~CatalogEntryConstructor()
Default destructor.
virtual std::unique_ptr< BASETYPE > Allocate(ARGS... args) const override
Create a new object that derives from BASETYPE.