CLASSpp Manual
Cosmology reference and developer manual
Loading...
Searching...
No Matches
FileContent Class Reference

#include <parser.h>

Public Member Functions

void set (const std::string &name, const std::string &value)
 
FileContentoperator+= (const FileContent &other)
 
int size () const
 
const std::string & get_filename () const
 
template<class T >
std::optional< T > get (const std::string &name) const
 
template<class T >
get_or (const std::string &name, T fallback) const
 
std::string get_or (const std::string &name, const char *fallback) const
 
void mark_all_unread () const
 
bool was_read (const std::string &name) const
 
std::vector< std::string > instances_with (const std::string &field, const std::string &value) const
 
std::vector< std::string > unread_parameters () const
 
void for_each (const std::function< void(const std::string &name, const std::string &value, bool read)> &fn) const
 

Static Public Member Functions

static FileContent from_file (const std::string &filename)
 
static bool parse_line (const std::string &line, std::string &name, std::string &value)
 
static std::vector< std::string > split_csv (const std::string &s)
 

Detailed Description

Holds the parsed contents of one or more .ini/.pre files.

Internally uses std::map for O(log N) parameter lookup and a std::vector to preserve insertion order. A std::set tracks which parameters have been consumed so that callers can warn about unused entries.

Member Function Documentation

◆ from_file()

static FileContent FileContent::from_file ( const std::string &  filename)
static

Load parameters from an .ini/.pre file. Throws std::invalid_argument on I/O errors or duplicate keys within the same file.

◆ set()

void FileContent::set ( const std::string &  name,
const std::string &  value 
)

Insert or overwrite a single parameter. Marks the key as unread.

◆ operator+=()

FileContent & FileContent::operator+= ( const FileContent other)

Merge another FileContent into this one. Throws std::invalid_argument if a key already present in *this also appears in other.

◆ size()

int FileContent::size ( ) const
inline

Number of stored parameters.

◆ get_filename()

const std::string & FileContent::get_filename ( ) const
inline

Source filename (or "file1 or file2" after a merge).

◆ get()

template<class T >
std::optional< T > FileContent::get ( const std::string &  name) const

Typed accessor: the value if name is present (marking it read), or std::nullopt if absent. Throws on a present-but-unparseable value. Supported T: int, double, std::string, std::vector<{int,double,std::string}>.

◆ get_or() [1/2]

template<class T >
T FileContent::get_or ( const std::string &  name,
fallback 
) const
inline

Read-with-default: get<T>(name) if present, else fallback.

◆ get_or() [2/2]

std::string FileContent::get_or ( const std::string &  name,
const char *  fallback 
) const

Overload so a string-literal fallback does not deduce T = const char*.

◆ mark_all_unread()

void FileContent::mark_all_unread ( ) const
inline

Mark every parameter as unread (used before a shooting iteration).

◆ was_read()

bool FileContent::was_read ( const std::string &  name) const
inline

Return true if name has been marked as read.

◆ instances_with()

std::vector< std::string > FileContent::instances_with ( const std::string &  field,
const std::string &  value 
) const

Return every instance name N such that the entry "N.<field>" has the given value. The dot is a literal separator; N must match the instance regex [A-Za-z_][A-Za-z0-9_]*. Results are returned in insertion order. Does NOT mark anything as read.

◆ unread_parameters()

std::vector< std::string > FileContent::unread_parameters ( ) const

Return the names of all parameters that have not yet been read.

◆ for_each()

void FileContent::for_each ( const std::function< void(const std::string &name, const std::string &value, bool read)> &  fn) const

Invoke fn for every parameter in insertion order. Arguments: parameter name, value string, whether it was read. Declared in the second public section so the Cython wrapper generator (which stops at the first private:) does not attempt to parse it.

◆ parse_line()

static bool FileContent::parse_line ( const std::string &  line,
std::string &  name,
std::string &  value 
)
static

Parse a single .ini line into name/value. Returns true when the line contains a valid key=value pair. Public for the legacy wrapper.

◆ split_csv()

static std::vector< std::string > FileContent::split_csv ( const std::string &  s)
static

Split a comma-separated value string into trimmed substrings.


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