This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct main_window; | |
struct window { | |
window(main_window &parent) : _parent(parent) { } | |
void close(); | |
private: | |
main_window &_parent; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//File main_window.h -------------------- | |
#pragma once | |
#include "window.h" | |
struct main_window { | |
void close_child(); | |
private: | |
window child; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---- 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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()); | |
/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |