1#ifndef included_AMP_Database
2#define included_AMP_Database
4#include "AMP/AMP_TPLs.h"
5#include "AMP/utils/Array.h"
6#include "AMP/utils/TypeTraits.h"
7#include "AMP/utils/Units.h"
8#include "AMP/utils/typeid.h"
10#include "StackTrace/source_location.h"
45 virtual std::unique_ptr<KeyData>
clone()
const = 0;
47 virtual void print( std::ostream &os,
48 std::string_view indent =
"",
50 bool printType =
false )
const = 0;
77 virtual size_t pack( std::byte * )
const = 0;
79 virtual size_t unpack(
const std::byte * ) = 0;
81 virtual void writeHDF5( int64_t fid,
const std::string &name )
const = 0;
83 virtual void readHDF5( int64_t fid,
const std::string &name ) = 0;
87 std::string_view name =
"",
92 std::string_view name =
"",
144 static std::unique_ptr<KeyData>
readYAML( std::string_view filename,
154 template<
class... Args>
155 static std::unique_ptr<Database>
create( Args... args );
164 template<
class... Args>
212 std::unique_ptr<KeyData>
clone()
const override;
247 std::vector<std::string>
getAllKeys(
bool sort =
true )
const;
278 return getScalar<std::string>( key, {}, src );
397 const std::vector<TYPE> &data,
429 std::unique_ptr<KeyData> data,
488 std::shared_ptr<const Database>
509 inline void putDatabase( std::string_view key, std::unique_ptr<Database> db )
511 putData( key, std::move( db ) );
529 putDatabase( key, std::make_unique<Database>( std::string( key ) ) );
540 inline std::shared_ptr<Database>
putDatabase( std::string_view key )
542 putData( key, std::make_unique<Database>( std::string( key.data(), key.size() ) ) );
554 void erase( std::string_view key,
bool check =
true );
571 std::string_view indent =
"",
573 bool printType =
false )
const override;
588 print( std::string_view indent =
"",
bool sort =
true,
bool printType =
false )
const;
596 std::vector<std::string>
getUnused(
bool recursive =
true )
const;
601 size_t pack( std::byte * )
const override;
602 size_t unpack(
const std::byte * )
override;
603 void writeHDF5( int64_t fid,
const std::string &name )
const override;
604 void readHDF5( int64_t fid,
const std::string &name )
override;
609 Database( SAMRAI::tbox::Database & );
612 std::shared_ptr<SAMRAI::tbox::Database> cloneToSAMRAI()
const;
615 static std::shared_ptr<Database> readThroughSAMRAI(
const std::string &filename );
625 std::vector<std::shared_ptr<KeyData>>
d_data;
629 template<
class TYPE,
class... Args>
630 void addArgs( std::string_view key, TYPE value, Args... args );
633 template<
class TYPE,
class... Args>
639 return std::hash<std::string_view>{}( s );
643 inline int find( uint32_t hash,
bool use )
const
645 for (
size_t i = 0; i <
d_hash.size(); i++ ) {
646 if ( hash ==
d_hash[i] ) {
682 SAMRAI::tbox::DatabaseBox cloneToSAMRAI()
const;
719void registerKeyData(
const std::string &name, std::function<std::unique_ptr<KeyData>()> fun );
731template<
class TYPE,
class... Args>
734 if constexpr ( is_vector_v<TYPE> ) {
736 }
else if constexpr ( is_Array_v<TYPE> ) {
738 }
else if constexpr ( std::is_same_v<TYPE, std::string> ||
739 std::is_same_v<TYPE, std::string_view> ) {
741 }
else if constexpr ( has_size_v<TYPE> || is_initializer_list_v<TYPE> ) {
742 typedef decltype( *value.begin() ) TYPE2;
744 std::vector<TYPE3> data( value.begin(), value.end() );
749 if constexpr (
sizeof...( args ) > 0 )
752template<
class TYPE,
class... Args>
756 if constexpr ( is_vector_v<TYPE> ) {
758 }
else if constexpr ( is_Array_v<TYPE> ) {
760 }
else if constexpr ( std::is_same_v<TYPE, std::string> ||
761 std::is_same_v<TYPE, std::string_view> ) {
763 }
else if constexpr ( has_size_v<TYPE> || is_initializer_list_v<TYPE> ) {
764 typedef decltype( *value.begin() ) TYPE2;
766 std::vector<TYPE3> data( value.begin(), value.end() );
771 if constexpr (
sizeof...( args ) > 0 )
774template<
class... Args>
777 constexpr size_t n =
sizeof...( args );
778 static_assert( n % 2 == 0 );
779 auto db = std::make_unique<Database>();
782 db->addArgs( args... );
785template<
class... Args>
788 constexpr size_t n =
sizeof...( args );
789 static_assert( n % 3 == 0 );
790 auto db = std::make_unique<Database>();
793 db->addArgsWithUnits( args... );
Simple class to store the array dimensions.
std::array< int, 5 > d_lower
DatabaseBox(int dim, const int *lower, const int *upper)
Default constructor.
int & lower(uint8_t i)
Return non-const reference to the specified component of the lower index.
int lower(uint8_t i) const
Return the specified component of the lower index.
std::array< int, 5 > d_upper
int & upper(uint8_t i)
Return non-const reference to the specified component of the upper index.
uint8_t dim() const
Return the number of dimensions.
uint8_t & dim()
Return a non-const reference to the number of dimensions.
DatabaseBox()
Empty constructor.
bool operator==(const DatabaseBox &box) const
DatabaseBox(std::string_view str)
Construct from a string of the format "[(0,0,0), (7,7,7)]".
int upper(uint8_t i) const
Return the specified component of the upper index.
void copy(const Database &rhs)
Copy the data.
bool isEquation(std::string_view key, source_location src=source_location::current()) const
Database(std::string name)
Basic constructor.
bool is_integral() const override
Return true if the type is a integer point type.
std::vector< TYPE > getVector(std::string_view key, const Units &unit=Units(), source_location src=source_location::current()) const
bool operator==(const KeyData &rhs) const override
Return true if the databases are equivalent.
void deleteData(std::string_view key)
std::string getString(std::string_view key, source_location src=source_location::current()) const
size_t packSize() const override
Return the number of bytes required to pack the data.
TYPE getWithDefault(std::string_view key, IdentityType< const TYPE & > value, const Units &unit=Units(), source_location src=source_location::current()) const
Check getDefaultAddKeyBehavior() const
Get the default behavior when adding keys.
std::shared_ptr< const Database > getDatabase(std::string_view key, source_location src=source_location::current()) const
static std::unique_ptr< KeyData > readYAML(std::string_view filename, source_location src=source_location::current())
void readDatabase(const std::string &filename, source_location src=source_location::current())
bool operator==(const Database &rhs) const
Return true if the databases are equivalent.
void putScalar(std::string_view key, TYPE value, Units unit=Units(), Check check=Check::GetDatabaseDefault, source_location src=source_location::current())
typeID getDataType(std::string_view key) const
Get the fundamental type (e.g. double, int, float, ...)
Database(const Database &)
Copy constructor.
const std::string & getName() const
Get the name of the database.
size_t unpack(const std::byte *) override
Unpack the data from a buffer.
void putData(std::string_view key, std::unique_ptr< KeyData > data, Check check=Check::GetDatabaseDefault, source_location src=source_location::current())
bool keyExists(std::string_view key) const
Database & operator=(const Database &)
Assignment operator.
std::unique_ptr< Database > cloneDatabase() const
Copy the data.
void erase(std::string_view key, bool check=true)
static std::unique_ptr< Database > create(Args... args)
Create a database from key/value pairs.
Array< double > convertToDouble() const override
Return the data as a Array<double> (throw error if this is not valid)
std::vector< std::shared_ptr< KeyData > > d_data
std::shared_ptr< Database > putDatabase(std::string_view key)
typeID getDataType() const override
Print the type.
void setDefaultAddKeyBehavior(Check check, bool setChildren)
Set the default behavior when adding keys.
std::vector< std::string > getAllKeys(bool sort=true) const
Return all keys in the database.
void readHDF5(int64_t fid, const std::string &name) override
Read the data from HDF5.
std::unique_ptr< MathExpr > getEquation(std::string_view key, const Units &unit=Units(), source_location src=source_location::current()) const
Array< TYPE > getArray(std::string_view key, const Units &unit=Units(), source_location src=source_location::current()) const
Database & operator=(Database &&rhs)
Move assignment operator.
bool isString(std::string_view key, source_location src=source_location::current()) const
Check if the named entry is a string.
void putVector(std::string_view key, const std::vector< TYPE > &data, Units unit=Units(), Check check=Check::GetDatabaseDefault, source_location src=source_location::current())
Array< int64_t > convertToInt64() const override
Return the data as a Array<int64_t> (throw error if this is not valid)
std::vector< uint32_t > d_hash
bool operator!=(const Database &rhs) const
Return the number of entries in the database.
typeID getClassType() const override
Return class type.
size_t empty() const
Return true if the database is empty.
void setName(std::string name)
Get the name of the database.
static std::shared_ptr< Database > parseInputFile(const std::string &filename)
size_t pack(std::byte *) const override
Pack the data to a buffer.
void print(std::ostream &os, std::string_view indent="", bool sort=true, bool printType=false) const override
virtual ~Database()=default
Destructor.
std::string print(std::string_view indent="", bool sort=true, bool printType=false) const
typename IdentityTypeStruct< const T & >::type IdentityType
Database()
Empty constructor.
void putArray(std::string_view key, Array< TYPE > data, Units unit=Units(), Check check=Check::GetDatabaseDefault, source_location src=source_location::current())
std::vector< std::string > d_keys
void putDatabase(std::string_view key, std::unique_ptr< Database > db)
std::vector< std::string > getUnused(bool recursive=true) const
bool isType(std::string_view key, source_location src=source_location::current()) const
Check if the entry can be stored as the given type.
int find(uint32_t hash, bool use) const
KeyData * getData(std::string_view key)
void addArgsWithUnits(std::string_view key, TYPE value, const Units &unit, Args... args)
void addArgs(std::string_view key, TYPE value, Args... args)
static uint32_t hashString(std::string_view s)
bool isDatabase(std::string_view key, source_location src=source_location::current()) const
Check if the key is a database object.
Check
enum to control behavior when trying to add existing keys
@ Keep
Keep the existing data.
@ WarnKeep
Keep the existing data but print a warning.
@ Overwrite
Overwrite the data.
@ WarnOverwrite
Overwrite the data but print a warning (default)
void writeHDF5(int64_t fid, const std::string &name) const override
Write the data to HDF5.
static std::unique_ptr< Database > createFromString(std::string_view data)
std::unique_ptr< KeyData > clone() const override
Copy the data.
bool is_floating_point() const override
Return true if the type is a floating point type.
Database(Database &&rhs)
Move constructor.
std::shared_ptr< Database > createAddDatabase(std::string_view key)
ArraySize arraySize() const override
Return the array size.
static std::unique_ptr< Database > createWithUnits(Args... args)
Create a database from key/value/unit triplets.
TYPE getScalar(std::string_view key, const Units &unit=Units(), source_location src=source_location::current()) const
const Database & operator()(std::string_view key, source_location src=source_location::current()) const
size_t size() const
Return the number of entries in the database.
std::shared_ptr< Database > getDatabase(std::string_view key, source_location src=source_location::current())
const KeyData * getData(std::string_view key) const
std::vector< bool > d_used
Base class to hold data of a given type.
KeyData(const KeyData &)=delete
KeyData & operator=(const KeyData &)=delete
virtual size_t unpack(const std::byte *)=0
Unpack the data from a buffer.
virtual bool is_floating_point() const =0
Return true if the type is a floating point type.
virtual void readHDF5(int64_t fid, const std::string &name)=0
Read the data from HDF5.
virtual Array< int64_t > convertToInt64() const =0
Return the data as a Array<int64_t> (throw error if this is not valid)
virtual size_t packSize() const =0
Return the number of bytes required to pack the data.
TYPE getScalar(const Units &unit={}, std::string_view name="", source_location src=source_location::current()) const
Convert the data to a scalar of the given type.
double convertUnits(const Units &, std::string_view="") const
Return the conversion factor (if used)
virtual typeID getDataType() const =0
Array< TYPE > getArray(const Units &unit={}, std::string_view name="", source_location src=source_location::current()) const
Convert the data to a scalar of the given type.
virtual ~KeyData()
Destructor.
KeyData(KeyData &&)=delete
virtual void writeHDF5(int64_t fid, const std::string &name) const =0
Write the data to HDF5.
KeyData & operator=(KeyData &&)=delete
virtual bool is_integral() const =0
Return true if the type is a integer point type.
virtual std::unique_ptr< KeyData > clone() const =0
Copy the data.
StackTrace::source_location source_location
const Units & unit() const
Return the units.
virtual ArraySize arraySize() const =0
Return the array size.
KeyData(const Units &unit)
virtual Array< double > convertToDouble() const =0
Return the data as a Array<double> (throw error if this is not valid)
virtual typeID getClassType() const =0
Return class type.
virtual bool operator==(const KeyData &rhs) const =0
Check if two sets of data are equal.
bool operator!=(const KeyData &rhs) const
Check if two sets of data are not equal.
virtual size_t pack(std::byte *) const =0
Pack the data to a buffer.
virtual void print(std::ostream &os, std::string_view indent="", bool sort=true, bool printType=false) const =0
Print the data to a stream.
Provides a class for storing units.
std::ostream & operator<<(std::ostream &out, const AMP::ArraySize &s)
void registerKeyData(const std::string &name, std::function< std::unique_ptr< KeyData >()> fun)
typename std::remove_cv_t< typename std::remove_reference_t< T > > remove_cvref_t
Class to store type info.