\doxysection{cpp\+\_\+example.\+cpp}
\hypertarget{cpp__example_8cpp_source}{}\label{cpp__example_8cpp_source}\index{cpp\_example.cpp@{cpp\_example.cpp}}

\begin{DoxyCode}{0}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00001}00001\ }
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00002}00002\ \textcolor{preprocessor}{\#include\ <iostream>}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00003}00003\ \textcolor{preprocessor}{\#include\ <atomic>}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00004}00004\ }
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00005}00005\ \textcolor{preprocessor}{\#ifndef\ WIN32}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00006}00006\ \textcolor{preprocessor}{\#include\ <unistd.h>}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00007}00007\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00008}00008\ }
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00009}00009\ \textcolor{preprocessor}{\#include\ <\doxymbox{\hyperlink{lo_8h}{lo/lo.h}}>}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00010}00010\ \textcolor{preprocessor}{\#include\ <\doxymbox{\hyperlink{lo__cpp_8h}{lo/lo\_cpp.h}}>}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00011}00011\ }
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00012}00012\ \textcolor{keywordtype}{int}\ main()}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00013}00013\ \{}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00014}00014\ \ \ \ \ \textcolor{comment}{/*}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00015}00015\ \textcolor{comment}{\ \ \ \ \ *\ Create\ a\ server\ on\ a\ background\ thread.\ \ Note,\ all\ message}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00016}00016\ \textcolor{comment}{\ \ \ \ \ *\ handlers\ will\ run\ on\ the\ background\ thread!}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00017}00017\ \textcolor{comment}{\ \ \ \ \ */}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00018}00018\ \ \ \ \ \doxymbox{\hyperlink{classlo_1_1ServerThread}{lo::ServerThread}}\ st(9000);}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00019}00019\ \ \ \ \ \textcolor{keywordflow}{if}\ (!st.is\_valid())\ \{}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00020}00020\ \ \ \ \ \ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}Nope."{}}\ <<\ std::endl;}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00021}00021\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 1;}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00022}00022\ \ \ \ \ \}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00023}00023\ }
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00024}00024\ \ \ \ \ \textcolor{comment}{/*\ Set\ some\ lambdas\ to\ be\ called\ when\ the\ thread\ starts\ and}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00025}00025\ \textcolor{comment}{\ \ \ \ \ *\ ends.\ Here\ we\ demonstrate\ capturing\ a\ reference\ to\ the\ server}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00026}00026\ \textcolor{comment}{\ \ \ \ \ *\ thread.\ */}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00027}00027\ \ \ \ \ st.set\_callbacks([\&st]()\{printf(\textcolor{stringliteral}{"{}Thread\ init:\ \%p.\(\backslash\)n"{}},\&st);\},}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00028}00028\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ []()\{printf(\textcolor{stringliteral}{"{}Thread\ cleanup.\(\backslash\)n"{}});\});}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00029}00029\ }
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00030}00030\ \ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}URL:\ "{}}\ <<\ st.url()\ <<\ std::endl;}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00031}00031\ }
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00032}00032\ \ \ \ \ \textcolor{comment}{/*}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00033}00033\ \textcolor{comment}{\ \ \ \ \ *\ Counter\ for\ number\ of\ messages\ received\ -\/-\/\ we\ use\ an\ atomic}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00034}00034\ \textcolor{comment}{\ \ \ \ \ *\ because\ it\ will\ be\ updated\ in\ a\ background\ thread.}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00035}00035\ \textcolor{comment}{\ \ \ \ \ */}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00036}00036\ \ \ \ \ std::atomic<int>\ received(0);}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00037}00037\ }
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00038}00038\ \ \ \ \ \textcolor{comment}{/*}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00039}00039\ \textcolor{comment}{\ \ \ \ \ *\ Add\ a\ method\ handler\ for\ "{}/example,i"{}\ using\ a\ C++11\ lambda\ to}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00040}00040\ \textcolor{comment}{\ \ \ \ \ *\ keep\ it\ succinct.\ \ We\ capture\ a\ reference\ to\ the\ \`{}received'}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00041}00041\ \textcolor{comment}{\ \ \ \ \ *\ count\ and\ modify\ it\ atomatically.}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00042}00042\ \textcolor{comment}{\ \ \ \ \ *}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00043}00043\ \textcolor{comment}{\ \ \ \ \ *\ You\ can\ also\ pass\ in\ a\ normal\ function,\ or\ a\ callable\ function}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00044}00044\ \textcolor{comment}{\ \ \ \ \ *\ object.}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00045}00045\ \textcolor{comment}{\ \ \ \ \ *}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00046}00046\ \textcolor{comment}{\ \ \ \ \ *\ Note:\ If\ the\ lambda\ doesn't\ specify\ a\ return\ value,\ the\ default}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00047}00047\ \textcolor{comment}{\ \ \ \ \ *\ \ \ \ \ \ \ is\ \`{}return\ 0',\ meaning\ "{}this\ message\ has\ been\ handled,}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00048}00048\ \textcolor{comment}{\ \ \ \ \ *\ \ \ \ \ \ \ don't\ continue\ calling\ the\ method\ chain."{}\ \ If\ this\ is\ not}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00049}00049\ \textcolor{comment}{\ \ \ \ \ *\ \ \ \ \ \ \ the\ desired\ behaviour,\ add\ \`{}return\ 1'\ to\ your\ method}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00050}00050\ \textcolor{comment}{\ \ \ \ \ *\ \ \ \ \ \ \ handlers.}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00051}00051\ \textcolor{comment}{\ \ \ \ \ */}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00052}00052\ \ \ \ \ st.add\_method(\textcolor{stringliteral}{"{}example"{}},\ \textcolor{stringliteral}{"{}i"{}},}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00053}00053\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ [\&received](\doxymbox{\hyperlink{unionlo__arg}{lo\_arg}}\ **argv,\ \textcolor{keywordtype}{int})}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00054}00054\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \{std::cout\ <<\ \textcolor{stringliteral}{"{}example\ ("{}}\ <<\ (++received)\ <<\ \textcolor{stringliteral}{"{}):\ "{}}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00055}00055\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ argv[0]-\/>i\ <<\ std::endl;\});}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00056}00056\ }
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00057}00057\ \ \ \ \ \textcolor{comment}{/*}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00058}00058\ \textcolor{comment}{\ \ \ \ \ *\ Start\ the\ server.}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00059}00059\ \textcolor{comment}{\ \ \ \ \ */}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00060}00060\ \ \ \ \ st.start();}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00061}00061\ }
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00062}00062\ \ \ \ \ \textcolor{comment}{/*}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00063}00063\ \textcolor{comment}{\ \ \ \ \ *\ Send\ some\ messages\ to\ the\ server\ we\ just\ created\ on\ localhost.}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00064}00064\ \textcolor{comment}{\ \ \ \ \ */}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00065}00065\ \ \ \ \ \doxymbox{\hyperlink{classlo_1_1Address}{lo::Address}}\ a(\textcolor{stringliteral}{"{}localhost"{}},\ \textcolor{stringliteral}{"{}9000"{}});}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00066}00066\ }
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00067}00067\ \ \ \ \ \textcolor{comment}{/*}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00068}00068\ \textcolor{comment}{\ \ \ \ \ *\ An\ individual\ message}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00069}00069\ \textcolor{comment}{\ \ \ \ \ */}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00070}00070\ \ \ \ \ a.send(\textcolor{stringliteral}{"{}example"{}},\ \textcolor{stringliteral}{"{}i"{}},\ 7890987);}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00071}00071\ }
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00072}00072\ \ \ \ \ \textcolor{comment}{/*}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00073}00073\ \textcolor{comment}{\ \ \ \ \ *\ Initalizer\ lists\ and\ message\ constructors\ are\ supported,\ so}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00074}00074\ \textcolor{comment}{\ \ \ \ \ *\ that\ bundles\ can\ be\ created\ easily:}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00075}00075\ \textcolor{comment}{\ \ \ \ \ */}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00076}00076\ \ \ \ \ a.send(\doxymbox{\hyperlink{classlo_1_1Bundle}{lo::Bundle}}(\{\{\textcolor{stringliteral}{"{}example"{}},\ \doxymbox{\hyperlink{classlo_1_1Message}{lo::Message}}(\textcolor{stringliteral}{"{}i"{}},\ 1234321)\},}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00077}00077\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \{\textcolor{stringliteral}{"{}example"{}},\ \doxymbox{\hyperlink{classlo_1_1Message}{lo::Message}}(\textcolor{stringliteral}{"{}i"{}},\ 4321234)\}\}));}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00078}00078\ }
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00079}00079\ \ \ \ \ \textcolor{comment}{/*}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00080}00080\ \textcolor{comment}{\ \ \ \ \ *\ Polymorphic\ overloads\ on\ lo::Message::add()\ mean\ you\ don't\ need}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00081}00081\ \textcolor{comment}{\ \ \ \ \ *\ to\ specify\ the\ type\ explicitly.\ \ This\ is\ intended\ to\ be\ useful}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00082}00082\ \textcolor{comment}{\ \ \ \ \ *\ for\ templates.}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00083}00083\ \textcolor{comment}{\ \ \ \ \ */}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00084}00084\ \ \ \ \ \doxymbox{\hyperlink{classlo_1_1Message}{lo::Message}}\ m;}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00085}00085\ \ \ \ \ m.add(7654321);}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00086}00086\ \ \ \ \ a.send(\textcolor{stringliteral}{"{}example"{}},\ m);}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00087}00087\ }
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00088}00088\ \ \ \ \ \textcolor{comment}{/*}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00089}00089\ \textcolor{comment}{\ \ \ \ \ *\ Wait\ for\ messages\ to\ be\ received\ and\ processed.}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00090}00090\ \textcolor{comment}{\ \ \ \ \ */}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00091}00091\ \ \ \ \ \textcolor{keywordtype}{int}\ tries\ =\ 200;}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00092}00092\ \ \ \ \ \textcolor{keywordflow}{while}\ (received\ <\ 4\ \&\&\ -\/-\/tries\ >\ 0)\ \{}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00093}00093\ \textcolor{preprocessor}{\#ifdef\ WIN32}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00094}00094\ \ \ \ \ \ \ \ \ Sleep(10);}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00095}00095\ \textcolor{preprocessor}{\#else}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00096}00096\ \ \ \ \ \ \ \ \ usleep(10*1000);}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00097}00097\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00098}00098\ \ \ \ \ \}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00099}00099\ }
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00100}00100\ \ \ \ \ \textcolor{keywordflow}{if}\ (tries\ <=\ 0)\ \{}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00101}00101\ \ \ \ \ \ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}Error,\ waited\ too\ long\ for\ messages."{}}\ <<\ std::endl;}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00102}00102\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 1;}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00103}00103\ \ \ \ \ \}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00104}00104\ }
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00105}00105\ \ \ \ \ \textcolor{comment}{/*}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00106}00106\ \textcolor{comment}{\ \ \ \ \ *\ Resources\ are\ freed\ automatically,\ RAII-\/style,\ including}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00107}00107\ \textcolor{comment}{\ \ \ \ \ *\ closing\ the\ background\ server.}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00108}00108\ \textcolor{comment}{\ \ \ \ \ */}}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00109}00109\ \ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}Success!"{}}\ <<\ std::endl;}
\DoxyCodeLine{\Hypertarget{cpp__example_8cpp_source_l00110}00110\ \}}

\end{DoxyCode}
