Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Recipes

Efficient Input/Output

ELM comes with several functions to make easier the access to files:

#include <elm/io.h>
sys::Path p = ...;
int x;
auto in = io::read(p);
in >> x;
auto out1 = io::write(p);
out1 << x;
auto out2 = io::append(p);
out2 << x;

These three commands open a file named p and returns an input/output handle ready for reading or writing.

Inputing from a string can be done in the same way:

#include <elm/io.h>
int x;
auto in = io::read("my string");
in >> x;

Outputting to a string requires a StringBuffer:

#include <elm/string.h>
StringBuffer buf;
buf << 111;
string s = buf.toString();

There is a short cut to this with a special named "_":

#include <elm/string.h>
string s = _ << 111 << " euros";

To read a file line by line, the function Input::lines() provides an iterator:

#include <elm/io.h>
auto in = open_file();
for(auto line: in.lines())
...

Using maps

ELM comes with several map data structure:

These map implementation shares the same concepts concept::Map and concept::MutableMap. This means they are controlled in the same way.

To add a value to the map, one can type:

MyMap<string, int> map;
map.put("red", 111);
map["red"] = 111;

To get a value, several alternative exists using default values or options:

int x = map.get("red", -1);
Option<int> y = map.get("red");
int z = map["red"];

In the last form, a KeyException is raised if there is no value for the key.

elm::io::p
Printable< T, M > p(const T &data, const M &man)
Definition: Output.h:302
elm::io::write
FileOutput write(sys::Path path)
Definition: io.h:30
elm::map
void map(const C &c, const F &f, D &d)
Definition: util.h:89
elm::t::in
typename type_info< T >::in_t in
Definition: type_info.h:283
elm::_
AutoStringStartup & _
Definition: debug_CrashHandler.cpp:232
elm::io::read
FileInput read(sys::Path path)
Definition: io.h:26
elm::io::append
FileOutput append(sys::Path path)
Definition: io.h:31
elm::StringBuffer::toString
String toString()
Definition: StringBuffer.h:25