Skip to content

Instantly share code, notes, and snippets.

View thiagomg's full-sized avatar

Thiago Massari Guedes thiagomg

View GitHub Profile
@thiagomg
thiagomg / resulting.cpp
Created November 5, 2015 13:53
Ciclic dependency problem
//Resulting file after pre-processing ----
int get_total(std::vector<int> &v);
struct int_vector {
//implementation details ...
int size() { return _size; }
int operator[](int pos) {
return _data[pos];
}
int *_data;
@thiagomg
thiagomg / main.cpp
Last active November 12, 2015 00:10
Ciclic dependency problem
//File main.cpp --------------------------
#include "main.h"
#include "int_vector.h"
//We're not using accumulate because our
//int_vector example doesn't support.
int get_total(const int_vector &v) {
int total = 0;
for(int i=0; i < v.size(); i++) {
total += v[i];
@thiagomg
thiagomg / window.cpp
Created November 5, 2015 13:11
Ciclic dependency problem
#include "window.h"
#include "main_window.h"
void window::close() {
//Closing window stuff...
if( msgbox::inform("Really?") ) {
cout << "Closing window" << endl;
_parent.inform_closed(this);
} else {
@thiagomg
thiagomg / window.h
Created November 5, 2015 13:10
Ciclic dependency problem
struct main_window;
struct window {
window(main_window &parent) : _parent(parent) { }
void close();
private:
main_window &_parent;
};
@thiagomg
thiagomg / main_window.h
Last active November 5, 2015 17:44
Ciclic dependency problem
//File main_window.h --------------------
#pragma once
#include "window.h"
struct main_window {
void close_child();
private:
window child;
};
@thiagomg
thiagomg / window.cpp
Created November 5, 2015 12:44
Ciclic dependency problem
void window::close() {
//Closing window stuff...
if( msgbox::inform("Really?") ) {
cout << "Closing window" << endl;
//Closing window stuff
//...
//Now I need to inform the main window
@thiagomg
thiagomg / main_window.cpp
Last active November 4, 2015 20:43
Ciclic dependency problem
// ---- main_window.cpp ----
#include "main_window.h"
#include "window.h"
//My main window class
main_window mw;
//I have a close child that I want to close
mw.close_child();
@thiagomg
thiagomg / loop2.cpp
Last active October 13, 2015 01:30
Range loops - Writing safe
template<typename T>
void print1(T &msgs) {
for(const auto &msg : msgs) {
PublishMessage(msg);
}
}
template<typename T>
void print2(T &msgs) {
for_each(begin(msgs), end(msgs), [](const auto &msg) {
@thiagomg
thiagomg / loop2.cpp
Last active October 13, 2015 01:28
Range loops - Writing safe
//Positive end means [start:start+end)
//Negative end means [start:size-end)
template<typename T>
range<T> narrow_range(const T &cont, int start, int end) {
int items = std::distance(cont.cbegin(), cont.cend());
if( items == 0 || end == 0 ) //Checking for empty range
return range<T>(cont.cend(), cont.cend());
/*
@thiagomg
thiagomg / loop2.cpp
Created October 13, 2015 01:06
Range loops - Writing safe
template<typename T>
struct range {
range(const T &cont) : _b(cont.begin()), _e(cont.end()) {}
range(typename T::const_iterator b, typename T::const_iterator e) : _b(b), _e(e) {}
typename T::const_iterator &begin() { return _b; }
typename T::const_iterator &end() { return _e; }
private:
typename T::const_iterator _b, _e;