MAIA bb96820c
Multiphysics at AIA
Loading...
Searching...
No Matches
cpptoml::table Class Reference

#include <cpptoml.h>

Inheritance diagram for cpptoml::table:
[legend]
Collaboration diagram for cpptoml::table:
[legend]

Public Types

using iterator = string_to_base_map::iterator
 
using const_iterator = string_to_base_map::const_iterator
 

Public Member Functions

std::shared_ptr< baseclone () const override
 
iterator begin ()
 
const_iterator begin () const
 
iterator end ()
 
const_iterator end () const
 
MBool is_table () const override
 
MBool empty () const
 
MBool contains (const std::string &key) const
 
MBool contains_qualified (const std::string &key) const
 
std::shared_ptr< baseget (const std::string &key) const
 
std::shared_ptr< baseget_qualified (const std::string &key) const
 
std::shared_ptr< tableget_table (const std::string &key) const
 
std::shared_ptr< tableget_table_qualified (const std::string &key) const
 
std::shared_ptr< arrayget_array (const std::string &key) const
 
std::shared_ptr< arrayget_array_qualified (const std::string &key) const
 
std::shared_ptr< table_arrayget_table_array (const std::string &key) const
 
std::shared_ptr< table_arrayget_table_array_qualified (const std::string &key) const
 
template<class T >
option< T > get_as (const std::string &key) const
 
template<class T >
option< T > get_qualified_as (const std::string &key) const
 
template<class T >
array_of_trait< T >::return_type get_array_of (const std::string &key) const
 
template<class T >
array_of_trait< T >::return_type get_qualified_array_of (const std::string &key) const
 
void insert (const std::string &key, const std::shared_ptr< base > &value)
 
template<class T >
void insert (const std::string &key, T &&val, typename value_traits< T >::type *=0)
 
void erase (const std::string &key)
 
 table (const table &obj)=delete
 
tableoperator= (const table &rhs)=delete
 
template<>
array_of_trait< array >::return_type get_array_of (const std::string &key) const
 
template<>
array_of_trait< array >::return_type get_qualified_array_of (const std::string &key) const
 
- Public Member Functions inherited from cpptoml::base
virtual ~base ()=default
 
virtual std::shared_ptr< baseclone () const =0
 
virtual MBool is_value () const
 
virtual MBool is_table () const
 
std::shared_ptr< tableas_table ()
 
virtual MBool is_array () const
 
std::shared_ptr< arrayas_array ()
 
virtual MBool is_table_array () const
 
std::shared_ptr< table_arrayas_table_array ()
 
template<class T >
std::shared_ptr< value< T > > as ()
 
template<class T >
std::shared_ptr< const value< T > > as () const
 
template<class Visitor , class... Args>
void accept (Visitor &&visitor, Args &&... args) const
 
base_type type () const
 
template<>
std::shared_ptr< value< double > > as ()
 
template<>
std::shared_ptr< const value< double > > as () const
 

Private Member Functions

 table ()
 
 table ()
 
std::vector< std::string > split (const std::string &value, char separator) const
 
MBool resolve_qualified (const std::string &key, std::shared_ptr< base > *p=nullptr) const
 

Private Attributes

string_to_base_map map_
 

Friends

class table_array
 
std::shared_ptr< tablemake_table ()
 

Additional Inherited Members

- Protected Member Functions inherited from cpptoml::base
 base (const base_type t)
 
 base ()
 

Detailed Description

Represents a TOML keytable.

Definition at line 1063 of file cpptoml.h.

Member Typedef Documentation

◆ const_iterator

using cpptoml::table::const_iterator = string_to_base_map::const_iterator

tables can be iterated over. Const version.

Definition at line 1078 of file cpptoml.h.

◆ iterator

using cpptoml::table::iterator = string_to_base_map::iterator

tables can be iterated over.

Definition at line 1073 of file cpptoml.h.

Constructor & Destructor Documentation

◆ table() [1/3]

cpptoml::table::table ( )
inlineprivate

Definition at line 1295 of file cpptoml.h.

1296 // nothing
1297 }

◆ table() [2/3]

cpptoml::table::table ( )
inlineprivate

Definition at line 1299 of file cpptoml.h.

1299 {
1300 // nothing
1301 }

◆ table() [3/3]

cpptoml::table::table ( const table obj)
delete

Member Function Documentation

◆ begin() [1/2]

iterator cpptoml::table::begin ( )
inline

Definition at line 1080 of file cpptoml.h.

1080{ return map_.begin(); }
string_to_base_map map_
Definition: cpptoml.h:1351

◆ begin() [2/2]

const_iterator cpptoml::table::begin ( ) const
inline

Definition at line 1082 of file cpptoml.h.

1082{ return map_.begin(); }

◆ clone()

std::shared_ptr< base > cpptoml::table::clone ( ) const
inlineoverridevirtual

Implements cpptoml::base.

Definition at line 1453 of file cpptoml.h.

1453 {
1454 auto result = make_table();
1455 for(const auto& pr : map_)
1456 result->insert(pr.first, pr.second->clone());
1457 return result;
1458}
friend std::shared_ptr< table > make_table()
Definition: cpptoml.h:1417

◆ contains()

MBool cpptoml::table::contains ( const std::string &  key) const
inline

Determines if this key table contains the given key.

Definition at line 1095 of file cpptoml.h.

1095{ return map_.find(key) != map_.end(); }

◆ contains_qualified()

MBool cpptoml::table::contains_qualified ( const std::string &  key) const
inline

Determines if this key table contains the given key. Will resolve "qualified keys". Qualified keys are the full access path separated with dots like "grandparent.parent.child".

Definition at line 1102 of file cpptoml.h.

1102{ return resolve_qualified(key); }
MBool resolve_qualified(const std::string &key, std::shared_ptr< base > *p=nullptr) const
Definition: cpptoml.h:1326

◆ empty()

MBool cpptoml::table::empty ( ) const
inline

Definition at line 1090 of file cpptoml.h.

1090{ return map_.empty(); }

◆ end() [1/2]

iterator cpptoml::table::end ( )
inline

Definition at line 1084 of file cpptoml.h.

1084{ return map_.end(); }

◆ end() [2/2]

const_iterator cpptoml::table::end ( ) const
inline

Definition at line 1086 of file cpptoml.h.

1086{ return map_.end(); }

◆ erase()

void cpptoml::table::erase ( const std::string &  key)
inline

Removes an element from the table.

Definition at line 1289 of file cpptoml.h.

1289{ map_.erase(key); }

◆ get()

std::shared_ptr< base > cpptoml::table::get ( const std::string &  key) const
inline

Obtains the base for a given key.

Exceptions
std::out_of_rangeif the key does not exist

Definition at line 1108 of file cpptoml.h.

1108{ return map_.at(key); }

◆ get_array()

std::shared_ptr< array > cpptoml::table::get_array ( const std::string &  key) const
inline

Obtains an array for a given key.

Definition at line 1144 of file cpptoml.h.

1144 {
1145 if(!contains(key)) return nullptr;
1146 return get(key)->as_array();
1147 }
std::shared_ptr< base > get(const std::string &key) const
Definition: cpptoml.h:1108
MBool contains(const std::string &key) const
Definition: cpptoml.h:1095

◆ get_array_of() [1/2]

template<class T >
array_of_trait< T >::return_type cpptoml::table::get_array_of ( const std::string &  key) const
inline

Helper function that attempts to get an array of values of a given type corresponding to the template parameter for a given key.

If the key doesn't exist, doesn't exist as an array type, or one or more keys inside the array type are not of type T, an empty option is returned. Otherwise, an option containing a vector of the values is returned.

Definition at line 1227 of file cpptoml.h.

1227 {
1228 if(auto v = get_array(key)) {
1229 std::vector<T> result;
1230 result.reserve(v->get().size());
1231
1232 for(const auto& b : v->get()) {
1233 if(auto val = b->as<T>())
1234 result.push_back(val->get());
1235 else
1236 return {};
1237 }
1238 return {std::move(result)};
1239 }
1240
1241 return {};
1242 }
std::shared_ptr< array > get_array(const std::string &key) const
Definition: cpptoml.h:1144

◆ get_array_of() [2/2]

template<>
array_of_trait< array >::return_type cpptoml::table::get_array_of ( const std::string &  key) const
inline

Helper function that attempts to get an array of arrays for a given key.

If the key doesn't exist, doesn't exist as an array type, or one or more keys inside the array type are not of type T, an empty option is returned. Otherwise, an option containing a vector of the values is returned.

Definition at line 1364 of file cpptoml.h.

1364 {
1365 if(auto v = get_array(key)) {
1366 std::vector<std::shared_ptr<array>> result;
1367 result.reserve(v->get().size());
1368
1369 for(const auto& b : v->get()) {
1370 if(auto val = b->as_array())
1371 result.push_back(val);
1372 else
1373 return {};
1374 }
1375
1376 return {std::move(result)};
1377 }
1378
1379 return {};
1380}

◆ get_array_qualified()

std::shared_ptr< array > cpptoml::table::get_array_qualified ( const std::string &  key) const
inline

Obtains an array for a given key. Will resolve "qualified keys".

Definition at line 1152 of file cpptoml.h.

1152 {
1153 if(!contains_qualified(key)) return nullptr;
1154 return get_qualified(key)->as_array();
1155 }
std::shared_ptr< base > get_qualified(const std::string &key) const
Definition: cpptoml.h:1117
MBool contains_qualified(const std::string &key) const
Definition: cpptoml.h:1102

◆ get_as()

template<class T >
option< T > cpptoml::table::get_as ( const std::string &  key) const
inline

Helper function that attempts to get a value corresponding to the template parameter from a given key.

Definition at line 1179 of file cpptoml.h.

1179 {
1180#ifndef CPPTOML_NO_EXCEPTIONS
1181 try {
1182 return get_impl<T>(get(key));
1183 } catch(const std::out_of_range&) {
1184 return {};
1185 }
1186#else
1187 if(contains(key)) {
1188 return get_impl<T>(get(key));
1189 } else {
1190 return {};
1191 }
1192#endif
1193 }

◆ get_qualified()

std::shared_ptr< base > cpptoml::table::get_qualified ( const std::string &  key) const
inline

Obtains the base for a given key. Will resolve "qualified keys". Qualified keys are the full access path separated with dots like "grandparent.parent.child".

Exceptions
std::out_of_rangeif the key does not exist

Definition at line 1117 of file cpptoml.h.

1117 {
1118 std::shared_ptr<base> p;
1119 resolve_qualified(key, &p);
1120 return p;
1121 }
constexpr std::underlying_type< FcCell >::type p(const FcCell property)
Converts property name to underlying integer value.

◆ get_qualified_array_of() [1/2]

template<class T >
array_of_trait< T >::return_type cpptoml::table::get_qualified_array_of ( const std::string &  key) const
inline

Helper function that attempts to get an array of values of a given type corresponding to the template parameter for a given key. Will resolve "qualified keys".

If the key doesn't exist, doesn't exist as an array type, or one or more keys inside the array type are not of type T, an empty option is returned. Otherwise, an option containing a vector of the values is returned.

Definition at line 1255 of file cpptoml.h.

1255 {
1256 if(auto v = get_array_qualified(key)) {
1257 std::vector<T> result;
1258 result.reserve(v->get().size());
1259
1260 for(const auto& b : v->get()) {
1261 if(auto val = b->as<T>())
1262 result.push_back(val->get());
1263 else
1264 return {};
1265 }
1266 return {std::move(result)};
1267 }
1268
1269 return {};
1270 }
std::shared_ptr< array > get_array_qualified(const std::string &key) const
Definition: cpptoml.h:1152

◆ get_qualified_array_of() [2/2]

template<>
array_of_trait< array >::return_type cpptoml::table::get_qualified_array_of ( const std::string &  key) const
inline

Helper function that attempts to get an array of arrays for a given key. Will resolve "qualified keys".

If the key doesn't exist, doesn't exist as an array type, or one or more keys inside the array type are not of type T, an empty option is returned. Otherwise, an option containing a vector of the values is returned.

Definition at line 1392 of file cpptoml.h.

1392 {
1393 if(auto v = get_array_qualified(key)) {
1394 std::vector<std::shared_ptr<array>> result;
1395 result.reserve(v->get().size());
1396
1397 for(const auto& b : v->get()) {
1398 if(auto val = b->as_array())
1399 result.push_back(val);
1400 else
1401 return {};
1402 }
1403
1404 return {std::move(result)};
1405 }
1406
1407 return {};
1408}

◆ get_qualified_as()

template<class T >
option< T > cpptoml::table::get_qualified_as ( const std::string &  key) const
inline

Helper function that attempts to get a value corresponding to the template parameter from a given key. Will resolve "qualified keys".

Definition at line 1201 of file cpptoml.h.

1201 {
1202#ifndef CPPTOML_NO_EXCEPTIONS
1203 try {
1204 return get_impl<T>(get_qualified(key));
1205 } catch(const std::out_of_range&) {
1206 return {};
1207 }
1208#else
1209 if(contains_qualified(key)) {
1210 return get_impl<T>(get_qualified(key));
1211 } else {
1212 return {};
1213 }
1214#endif
1215 }

◆ get_table()

std::shared_ptr< table > cpptoml::table::get_table ( const std::string &  key) const
inline

Obtains a table for a given key, if possible.

Definition at line 1126 of file cpptoml.h.

1126 {
1127 if(contains(key) && get(key)->is_table()) return std::static_pointer_cast<table>(get(key));
1128 return nullptr;
1129 }
MBool is_table() const override
Definition: cpptoml.h:1088

◆ get_table_array()

std::shared_ptr< table_array > cpptoml::table::get_table_array ( const std::string &  key) const
inline

Obtains a table_array for a given key, if possible.

Definition at line 1160 of file cpptoml.h.

1160 {
1161 if(!contains(key)) return nullptr;
1162 return get(key)->as_table_array();
1163 }

◆ get_table_array_qualified()

std::shared_ptr< table_array > cpptoml::table::get_table_array_qualified ( const std::string &  key) const
inline

Obtains a table_array for a given key, if possible. Will resolve "qualified keys".

Definition at line 1169 of file cpptoml.h.

1169 {
1170 if(!contains_qualified(key)) return nullptr;
1171 return get_qualified(key)->as_table_array();
1172 }

◆ get_table_qualified()

std::shared_ptr< table > cpptoml::table::get_table_qualified ( const std::string &  key) const
inline

Obtains a table for a given key, if possible. Will resolve "qualified keys".

Definition at line 1135 of file cpptoml.h.

1135 {
1136 if(contains_qualified(key) && get_qualified(key)->is_table())
1137 return std::static_pointer_cast<table>(get_qualified(key));
1138 return nullptr;
1139 }

◆ insert() [1/2]

void cpptoml::table::insert ( const std::string &  key,
const std::shared_ptr< base > &  value 
)
inline

Adds an element to the keytable.

Definition at line 1275 of file cpptoml.h.

1275{ map_[key] = value; }

◆ insert() [2/2]

template<class T >
void cpptoml::table::insert ( const std::string &  key,
T &&  val,
typename value_traits< T >::type = 0 
)
inline

Convenience shorthand for adding a simple element to the keytable.

Definition at line 1282 of file cpptoml.h.

1282 {
1283 insert(key, make_value(std::forward<T>(val)));
1284 }
void insert(const std::string &key, const std::shared_ptr< base > &value)
Definition: cpptoml.h:1275
std::shared_ptr< typename value_traits< T >::type > make_value(T &&val)
Definition: cpptoml.h:588

◆ is_table()

MBool cpptoml::table::is_table ( ) const
inlineoverridevirtual

Determines if the given TOML element is a table.

Reimplemented from cpptoml::base.

Definition at line 1088 of file cpptoml.h.

1088{ return true; }

◆ operator=()

table & cpptoml::table::operator= ( const table rhs)
delete

◆ resolve_qualified()

MBool cpptoml::table::resolve_qualified ( const std::string &  key,
std::shared_ptr< base > *  p = nullptr 
) const
inlineprivate

Definition at line 1326 of file cpptoml.h.

1326 {
1327 auto parts = split(key, '.');
1328 auto last_key = parts.back();
1329 parts.pop_back();
1330
1331 auto cur_table = this;
1332 for(const auto& part : parts) {
1333 cur_table = cur_table->get_table(part).get();
1334 if(!cur_table) {
1335#ifndef CPPTOML_NO_EXCEPTIONS
1336 if(!p) return false;
1337
1338 throw std::out_of_range{key + " is not a valid key"};
1339#else
1340 return false;
1341#endif
1342 }
1343 }
1344
1345 if(!p) return cur_table->map_.count(last_key) != 0;
1346
1347 *p = cur_table->map_.at(last_key);
1348 return true;
1349 }
std::vector< std::string > split(const std::string &value, char separator) const
Definition: cpptoml.h:1309

◆ split()

std::vector< std::string > cpptoml::table::split ( const std::string &  value,
char  separator 
) const
inlineprivate

Definition at line 1309 of file cpptoml.h.

1309 {
1310 std::vector<std::string> result;
1311 std::string::size_type p = 0;
1312 std::string::size_type q;
1313 while((q = value.find(separator, p)) != std::string::npos) {
1314 result.emplace_back(value, p, q - p);
1315 p = q + 1;
1316 }
1317 result.emplace_back(value, p);
1318 return result;
1319 }

Friends And Related Function Documentation

◆ make_table

std::shared_ptr< table > make_table ( )
friend

Definition at line 1417 of file cpptoml.h.

1417 {
1418#if not defined(MAIA_INTEL_COMPILER)
1419 struct make_shared_enabler : public table {
1420 make_shared_enabler() = default;
1421 };
1422#endif
1423
1424 return std::make_shared<make_shared_enabler>();
1425}

◆ table_array

friend class table_array
friend

Definition at line 1065 of file cpptoml.h.

Member Data Documentation

◆ map_

string_to_base_map cpptoml::table::map_
private

Definition at line 1351 of file cpptoml.h.


The documentation for this class was generated from the following file: