IO¶
Inmemory Files¶
The File object maintains a simple, in-memory file.
- Note
This file is part of Empirical, https://github.com/devosoft/Empirical
- Copyright
Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md
- Date
2018-2020.
- Note
Status: BETA
-
namespace
emp
If we are in emscripten, make sure to include the header.
-
class
File
- #include <File.hpp>
A class to maintin files for loading, writing, storing, and easy access to components.
Public Functions
-
File
()
-
File
(std::istream &input)
-
File
(const std::string &filename)
-
File
(const File&) = default
-
File
(File&&) = default
-
~File
()
-
auto
begin
() const Return const iterator to beginning of file.
-
auto
end
() const Return const iterator to end of file.
-
auto
begin
() Return iterator to beginning of file.
-
auto
end
() Return iterator to end of file.
-
size_t
GetNumLines
() const How many lines are in this file?
-
size_t
size
() const Compatibility with size()
-
std::string &
operator[]
(size_t pos) Index into a specific line in this file.
-
const std::string &
operator[]
(size_t pos) const Const index into a specific line in this file.
-
std::string &
front
() Return the first line in the file.
-
const std::string &
front
() const Return a const reference to to the first line in the file.
-
std::string &
back
() Return the last line in the file.
-
const std::string &
back
() const Return a const reference to the last line in the file.
-
File &
Append
(const emp::vector<std::string> &in_lines) Append a vector of lines to the end of the file.
-
template<typename
T
>
autooperator<<
(T &&in) Insert formatted data into file This is exactly the same as operator+=
-
auto
operator>>
(std::string &out) Extract first line from file.
-
bool
operator==
(const File in) Test if two files are identical.
-
bool
operator!=
(const File in) Test if two files are different.
-
File &
Load
(const std::string &filename) Load a file from disk using the provided name. If file does not exist, this is a nop
-
bool
Contains
(const std::string &pattern) const Test if a substring exists on ANY line of a file.
-
std::set<std::string>
AsSet
() const Convert this file into an std::set of lines (loses line ordering).
-
File &
Apply
(const std::function<void(std::string&)> &fun) Apply a string manipulation function to all lines in the file.
-
File &
KeepIf
(const std::function<bool(const std::string&)> &fun) Purge all lines that don’t the criterion function.
-
File &
KeepIfContains
(const std::string &pattern) Keep only strings that contain a specific substring.
-
File &
RemoveIfContains
(const std::string &pattern) Remove all strings that contain a specific substring.
-
File &
RemoveEmpty
() Remove all lines that are empty strings.
-
File &
CompressWhitespace
() Any time multiple whitespaces are next to each other, collapse to a single WS char. Prefer ‘
’ if in whitespace collapsed, otherwise use ‘ ‘.
-
File &
RemoveWhitespace
(bool keep_newlines = true) Delete all whitespace; by default keep newlines.
-
File &
RemoveComments
(char marker) Allow remove comments to also be specified with a single character.
-
template<typename
T
>
emp::vector<T>Process
(const std::function<T(std::string&)> &fun) Run a function on each line of a file and return the restults as a vector. Note: Function is allowed to modify string.
-
emp::vector<std::string>
ExtractCol
(char delim = ',') Remove the first column from the file, returning it as a vector of strings.
-
template<typename
T
>
emp::vector<T>ExtractColAs
(char delim = ',') Remove the first column from the file, returning it as a vector of a specified type.
-
emp::vector<std::string_view>
ViewRowSlices
(size_t row_id, char delim = ',') Convert a row of a file to a vector of string views.
-
emp::vector<std::string>
ExtractRow
(char delim = ',') Remove the first row from the file, returning it as a vector of strings.
-
-
class
Serialization Macros¶
Macros for simplifying to serialization of objects.
- Note
This file is part of Empirical, https://github.com/devosoft/Empirical
- Copyright
Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md
- Date
2015-2021.
- Note
Status: ALPHA
Defines
-
EMP_SERIALIZE_INIT_VAR
(VAR)
-
EMP_SETUP_DATAPOD_BASEINFO
(CLASS_NAME, BASE_LOAD, BASE_STORE, ...) Use this macro to automatically build methods in a class to save and load data.
-
EMP_SETUP_DATAPOD
(CLASS_NAME, ...) Version to use in stand-alone classes.
-
EMP_SETUP_DATAPOD_D
(CLASS_NAME, BASE_CLASS, ...) Version to use in derived classes (with a base that also needs to be serialized).
-
EMP_SETUP_DATAPOD_D2
(CLASS_NAME, BASE_CLASS1, BASE_CLASS2, ...) Version to use in derived classes (with TWO bases that need to be serialized).
-
EMP_CALL_BASE_1
(BASE1)
-
EMP_CALL_BASE_2
(BASE1, BASE2)
Serialization Tools¶
Tools to save and load data from classes.
All of the important information about a class is stored in a DataPod, which can be used to restore the class at a later time.
- Note
This file is part of Empirical, https://github.com/devosoft/Empirical
- Copyright
Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md
- Date
2015-2021.
- Note
Status: ALPHA
Why is this better than other serialization techniques?
Only one line of code is added to a custom class to make it serializable.
Serialized objects do not need a default constructor (a DataPod constructor is added)
Serialized objects can be const since they get rebuilt during construction.
Synergistic interactions with other EMP classes, such as config and tuple_struct
In order to setup a target class to be able to be serialized into a pod, you must add a macro to include the needed functionality. For a basic class, use:
EMP_SETUP_DATAPOD(ClassName, var1, var2, …)
Where ClassName is the target class’ name and var1, var2, etc are the names of the member variables that also need to be stored. Note that member variables can be either built-in types or custom types that have also had DataPods setup in them.
If the target class is a derived class, you must use either:
EMP_SETUP_DATAPOD_D(ClassName, BassClassName, var1, var2, …)
-or-
EMP_SETUP_DATAPOD_D2(ClassName, BassClass1Name, BaseClass2Name, var1, var2, …)
…depending on how many base classes it was derived from (currently max 2).
Note also that this macro must either go in the public section of the target class definition, or the target class must be made a friend to the emp::serialize::DataPod class.
-
namespace
emp
If we are in emscripten, make sure to include the header.
-
namespace
serialize
Functions
-
template<typename
T
>
autoStoreVar
(DataPod &pod, const T &var, bool) -> typename T::emp_load_return_type& StoreVar() takes a DataPod and a variable and stores that variable to the pod. The third argument (bool vs. int) will receive a bool, and thus bool versions are preferred in the case of a tie. Specialized versions of this function can be included elsewhere, as needed, and should take a bool as the third argument.
-
class
DataPod
- #include <serialize.hpp>
A DataPod managed information about another class for serialization.
Public Functions
-
DataPod
(std::iostream &_ios)
-
DataPod
(DataPod &&rhs)
-
DataPod
() = delete
-
DataPod
(const DataPod&) = delete
-
~DataPod
()
-
std::ostream &
OStream
()
-
std::istream &
IStream
()
Protected Functions
-
void
ClearData
()
-
-
template<typename
-
namespace