diff --git a/Makefile b/Makefile index 1e93764..10722a1 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CXXFLAGS:=-std=gnu++17 -Wall -O3 -MMD -MP -ggdb -pthread -fopenmp -Iext/flat_hash_map -PROGRAMS = test exception smartfp string thread memory windex windex-par classes templates lambdas multi move +PROGRAMS = test exception smartfp string thread memory windex windex-par classes templates lambdas multi move rest all: $(PROGRAMS) @@ -40,3 +40,6 @@ multi: multi.o move: move.o g++ -std=gnu++17 $^ -o $@ -pthread + +rest: rest.o + g++ -std=gnu++17 $^ -o $@ -pthread diff --git a/multi.cc b/multi.cc index f2d3d94..16c4338 100644 --- a/multi.cc +++ b/multi.cc @@ -24,10 +24,10 @@ std::ostream& operator<<(std::ostream& ios, const IPAddress& ip) constexpr uint32_t chtonl(uint32_t s) { - return ( + return ((s & 0x000000FF) << 24) | ((s & 0x0000FF00) << 8) - | ((s & 0xFF000000) >> 24) | ((s & 0x00FF0000) >> 8) - ); + | ((s & 0xFF000000) >> 24) | ((s & 0x00FF0000) >> 8); + } constexpr IPAddress operator "" _ipv4(const char* p, size_t l) diff --git a/rest.cc b/rest.cc new file mode 100644 index 0000000..9c08a27 --- /dev/null +++ b/rest.cc @@ -0,0 +1,145 @@ +#include +#include +#include +#include +#include + +using namespace std; + +struct Event +{}; + +struct Policy +{ + bool check() { return false; } +}; + +struct Channel +{ + enum class Priority { Low, Medium, High}; + void sendEvent(const Event& e); + void sendEvent(const Event& e, Priority p); + void sendEvent2(const Event& e, std::optional p={}) + { + if(!p) + cout << "No priority passed" << endl; + switch(*p) { + case Priority::Low: + cout << "Low!" << endl; break; + case Priority::Medium: + cout << "Medium!" << endl; break; + case Priority::High: + cout << "High!" << endl; break; + ; + } + } + + std::optional getPolicy(const Event& e) + { + return std::nullopt; + } +}; + + +#include + +template +bool isSet(const T& t) +{ + static_assert(std::is_pointer(), "isSet requires a pointer"); + return t != nullptr; +} + +int main(int argc, char** argv) +{ + static_assert(sizeof(int) == 4, "Int must be 4 bytes"); + + if(isSet(argv)) + cout << "Yes, is set"<::min() << endl; + cout << std::numeric_limits::max() << endl; + + ifstream in("/etc/passwd"); + if (!in.is_open()) { + std::cout << "failed to open\n"; + } else { + string line; + int count = 0; + while(getline(in, line)) + ++count; + cout << "Read " << count << " lines\n"; + } + + vector vi{1, 2, 3, 4}; + vector vs{"foo", "bar", "baz"}; + + auto printAverages = [](int i){}; + + for(auto a : { 1, 10, 15} ) + printAverages(a); + + auto doAverage = [](vector v) {}; + + vector x, y, z; + for(auto& a : {x, y, z}) + doAverage(a); + + struct Coordinate + { + double x, y; + }; + + + Coordinate coord{-1.0, 1.0}; + + auto [xc, yc] = coord; + xc = yc = 0; + + std::string s = R"foo(Some people, when confronted with a problem, think: +"I know, I'll use regular expressions." +Now they have two problems.)foo"; + + cout << s << endl; + std::regex word_regex("(\\S+)"); + auto words_begin = + std::sregex_iterator(s.begin(), s.end(), word_regex); + auto words_end = std::sregex_iterator(); + + std::cout << "Found " + << std::distance(words_begin, words_end) + << " words\n"; + + for_each(words_begin, words_end, [](const auto& w) { + cout << "Word: " << w.str() << endl; + }); + + + Channel c; + Event e1, e2; + + c.sendEvent2(e1); + c.sendEvent2(e2, Channel::Priority::High); + + std::optional p; + if(!p) + cout << "Priority not set" << endl; + p = Channel::Priority::High; + if(*p == Channel::Priority::High) + cout << "Priority is now set to High" << endl; + + if(auto policy = c.getPolicy(e1)) { + if(policy->check()) + c.sendEvent(e1); + + } + else + cout << "No policy was set" <check()) + c.sendEvent(e1); + +} +