add examples dir

master
Daniel Kolesa 2016-01-31 23:19:23 +01:00
parent a2b50e59df
commit 9380d2f430
5 changed files with 196 additions and 0 deletions

View File

@ -0,0 +1,22 @@
#include <ostd/io.hh>
#include <ostd/filesystem.hh>
using namespace ostd;
void list_dirs(ConstCharRange path, int off = 0) {
DirectoryStream ds(path);
/* iterate all items in directory */
for (auto v: ds.iter()) {
if (v.type() != FileType::directory)
continue;
for (int i: range(off)) write(' ');
writeln(v.filename());
list_dirs(v.path(), off + 1);
}
}
int main(int argc, char **argv) {
if (argc < 1) return 1;
list_dirs(argv[1]);
return 0;
}

27
examples/range.cc 100644
View File

@ -0,0 +1,27 @@
#include <ostd/range.hh>
#include <ostd/io.hh>
#include <ostd/algorithm.hh>
using namespace ostd;
int main() {
/* range iter */
writeln("range iter test");
for (int i: range(10))
writeln(i);
/* algorithm: map */
writeln("range map test");
for (int i: map(range(10), [](int v) { return v + 0.5f; }))
writeln(i);
/* alrogithm: filter */
writeln("range filter test");
auto v = { 5, 5, 5, 5, 5, 10, 15 };
for (int i: filter(v, [](int v) { return v > 5; }))
writeln(i);
/* generate string ABCDEF */
String v(map(range(6), [](int v) -> char { return v + 65; }));
writeln(v);
}

88
examples/signal.cc 100644
View File

@ -0,0 +1,88 @@
#include <ostd/io.hh>
#include <ostd/string.hh>
#include <ostd/event.hh>
using namespace ostd;
struct SignalTest {
/* const on the class means that a const reference to the event
* can actually emit (in that case, the reference passed to each
* callback will always be const to make sure nothing changes)
*/
Signal<const SignalTest, int, const char *> on_simple = this;
Signal< SignalTest, float > on_param = this;
SignalTest(): p_param(3.14f) {
/* we can connect methods */
on_simple.connect(&SignalTest::simple_method);
writeln("constructed signal test");
}
float get_param() const { return p_param; }
void set_param(float nv) {
float oldval = p_param;
p_param = nv;
/* types passed to emit must match the types of the event */
on_param.emit(oldval);
}
void foo() const {
/* because on_simple is const, we can emit form const method */
on_simple.emit(150, "hello world");
}
void simple_method(int v, const char *str) const {
writefln("simple method handler: %d, %s", v, str);
}
private:
float p_param;
};
int main() {
writeln("=== program start ===");
SignalTest st;
int test = 42;
/* we can connect lambdas, including closures
* this callback can access "test" easily and it will still work
*/
auto idx = st.on_simple.connect([&](const SignalTest &, int v,
const char *str) {
writefln("and lambda test: %d, %s (%d)", v, str, test);
});
writeln("--- test emit ---");
st.foo();
/* we can disconnect callbacks too */
st.on_simple.disconnect(idx);
/* this should not print from the lambda above */
writeln("--- test emit after disconnect ---");
st.foo();
writeln("--- set value ---");
st.set_param(6.28f);
/* the reference to SignalTest here is mutable */
st.on_param.connect([](SignalTest &self, float oldval) {
writeln("value changed...");
writefln(" old value: %f, new value: %f", oldval,
self.get_param());
/* when we have a mutable reference we can change the original
* object, for example re-emit its own signal once again
*/
if (self.get_param() > 140.0f) return;
self.set_param(self.get_param() + 1.0f);
});
/* trigger the callback above */
writeln("--- test set value ---");
st.set_param(134.28f);
writeln("=== program end ===");
return 0;
}

View File

@ -0,0 +1,22 @@
#include <ostd/functional.hh>
#include <ostd/stream.hh>
using namespace ostd;
void print_result(Uint32 x) {
writefln("got x: 0x%X", x);
}
int main() {
FileStream wtest("test.bin", StreamMode::write);
copy(iter({ 0xABCD1214, 0xBADC3264, 0xDEADBEEF, 0xBEEFDEAD }), wtest.iter<Uint32>());
wtest.close();
FileStream rtest("test.bin", StreamMode::read);
printf("stream size: %zu\n", rtest.size());
for (Uint32 x: map(rtest.iter<Uint32>(), FromBigEndian<Uint32>()))
print_result(x);
return 0;
}

View File

@ -0,0 +1,37 @@
#include <ostd/algorithm.hh>
#include <ostd/string.hh>
#include <ostd/stream.hh>
using namespace ostd;
int main() {
writeln("writing sample file...");
FileStream wtest("test.txt", StreamMode::write);
String smpl = "This is a test file for later read.\n"
"It contains some sample text in order to see whether "
"things actually read correctly.\n\n\n"
""
"This is after a few newlines. The file continues here.\n"
"The file ends here.\n";
copy(smpl.iter(), wtest.iter());
wtest.close();
FileStream test("test.txt", StreamMode::read);
writeln("## WHOLE FILE READ ##\n");
String ts1(test.iter());
writefln("-- str beg --\n%s-- str end --", ts1);
test.seek(0);
writeln("\n## PART FILE READ ##\n");
String ts2(take(test.iter(), 25));
writefln("-- str beg --\n%s\n-- str end --", ts2);
return 0;
}