Skip to content

Instantly share code, notes, and snippets.

@phuf
Created February 20, 2019 11:04
Show Gist options
  • Save phuf/b0e76419fcace35e6394f2a01a4ef5a3 to your computer and use it in GitHub Desktop.
Save phuf/b0e76419fcace35e6394f2a01a4ef5a3 to your computer and use it in GitHub Desktop.
// sudo apt install uuid-dev
// g++ hack.cpp -luuid
#include <iostream>
#include <sstream>
#include <math.h>
#include <memory>
#include <memory.h>
#include <uuid/uuid.h>
#include <unordered_map>
#include <bits/hash_bytes.h>
#include <functional>
using namespace std;
struct shared_memory {
shared_memory() {
::memset(this, 0, sizeof(shared_memory));
frequency = 88;
}
int frequency;
int rssi;
char security[8000];
int security_length;
};
class SetHandler {
public:
virtual void handle(int) = 0;
};
class set_handler : public SetHandler {
public:
void handle(int x) {
cout << "got it " << x << endl;
}
};
using pdu_size_t = unsigned short;
enum pdu_t : unsigned short {
bind,
unbind,
trap,
set
};
enum trap_t : unsigned short {
frequency_changed
};
enum property_t : unsigned short {
rx_frequecy
};
struct pdu_header {
pdu_size_t size;
pdu_t type;
};
struct set_int_command {
pdu_header header;
property_t property;
int value;
};
struct set_octet_string_command {
pdu_header header;
property_t property;
unsigned char value[1];
};
struct trap_command {
pdu_header header;
uuid_t subagent_id;
};
class fifo {
};
class read_fifo : public fifo {
};
class receiver {
private:
int frequency;
public:
receiver() {
frequency = 99;
}
int get_frequency() {
return frequency;
}
int set_frequency(int value) {
frequency = value;
}
};
class role {
virtual void trap(int);
virtual void set(int);
virtual void bind();
virtual void unbind();
};
class master_role {
public:
typedef const std::function<void (int)> handler_t;
handler_t* handler;
SetHandler* sh;
master_role() {
handler = nullptr;
sh = nullptr;
}
void set_handler(handler_t& h) {
handler = &h;
}
void set_handler(SetHandler* sh) {
this->sh = sh;
}
void fire() {
if (handler) {
(*handler)(99);
}
if (this->sh) {
sh->handle(51);
}
}
};
class subagent_role {
};
class terminal {
public:
static std::shared_ptr<::terminal> make_terminal(){
struct make_shared_enabler : public terminal {};
return std::make_shared<make_shared_enabler>();
}
std::shared_ptr<::receiver> get_receiver() {
return receiver;
}
public:
terminal() {
receiver = std::make_shared<::receiver>();
}
std::shared_ptr<::receiver> receiver;
};
shared_ptr<::receiver> foo() {
std::shared_ptr<::terminal> terminal = terminal::make_terminal();
shared_ptr<::receiver> r = terminal->get_receiver();
cout << "use count " << r.use_count() << endl;
cout << "use count " << r.use_count() << endl;
return r;
}
template <>
struct std::hash<uuid_t>
{
std::size_t operator()(const uuid_t& uuid) const
{
return std::_Hash_bytes(uuid, sizeof(uuid), 99);
}
};
class ffoo {
public:
int y = 100;
void woa() {
std::cout << "yo yo yo " << y << endl;
}
};
void joe(master_role& role) {
ffoo* fff = new ffoo();
role.set_handler([&](int num) {
std::cout << "A: " << num << std::endl;
});
role.fire();
}
int main() {
// master_role role;
// joe(role);
// role.fire();
set_handler* sh = new set_handler();
master_role role;
role.set_handler(sh);
role.fire();
// delete sh;
role.fire();
/*
uuid_t uuid;
uuid_generate(uuid);
char out[128];
uuid_unparse(uuid, out);
cout << out << endl;
hash<uuid_t> x;
cout << "hash " << x(uuid) << endl;
unordered_map<uuid_t, int> m;
shared_ptr<::receiver> r = foo();
cout << r->get_frequency() << endl;
cout << r.use_count() << endl;
shared_ptr<::shared_memory> shared_memory = std::make_shared<::shared_memory>();
cout << shared_memory.use_count() << endl;
cout << shared_memory->frequency << endl;
shared_memory->frequency = 44;
shared_ptr<::shared_memory> shared_memory2 = shared_memory;
cout << shared_memory.use_count() << endl;
shared_memory2 = nullptr;
cout << shared_memory.use_count() << endl;
cout << shared_memory->frequency << endl;
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment