Files
date_time/doc/print__holidays_8cpp-example.html
Jeff Garland b22d572c70 updated documentation
[SVN r15046]
2002-08-22 00:11:01 +00:00

114 lines
5.5 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Example Documentation</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body bgcolor="#ffffff">
<!-- Generated by Doxygen 1.2.10 -->
<hr><h1>print_holidays.cpp</h1> This is an example of using functors to define a holiday schedule
<p>
<div class="fragment"><pre><font class="comment">/*generate a set of dates using a collections of date generators</font>
<font class="comment">Output looks like:</font>
<font class="comment">Enter Year: 2002</font>
<font class="comment">2002-Jan-01 [Tue]</font>
<font class="comment">2002-Jan-21 [Mon]</font>
<font class="comment">2002-Feb-12 [Tue]</font>
<font class="comment">2002-Jul-04 [Thu]</font>
<font class="comment">2002-Sep-02 [Mon]</font>
<font class="comment">2002-Nov-28 [Thu]</font>
<font class="comment">2002-Dec-25 [Wed]</font>
<font class="comment">Number Holidays: 7</font>
<font class="comment">*/</font>
<font class="preprocessor">#include "boost/gdtl/gregorian/gregorian.hpp"</font>
<font class="preprocessor">#include &lt;algorithm&gt;</font>
<font class="preprocessor">#include &lt;functional&gt;</font>
<font class="preprocessor">#include &lt;vector&gt;</font>
<font class="preprocessor">#include &lt;iostream&gt;</font>
<font class="preprocessor">#include &lt;set&gt;</font>
<font class="keyword">namespace </font>std {
<font class="comment">//define an operator &lt;&lt; for date so transform can call</font>
std::ostream&amp;
operator&lt;&lt;(std::ostream&amp; os, <font class="keyword">const</font> gregorian::date&amp; d)<font class="keyword"></font>
<font class="keyword"> </font>{
os &lt;&lt; gregorian::to_simple_string(d);
<font class="keywordflow">return</font> os;
}
}
<font class="keywordtype">void</font>
print_date(gregorian::date d)<font class="keyword"> </font>
<font class="keyword"></font>{
std::cout &lt;&lt; d &lt;&lt; <font class="stringliteral">" ["</font> &lt;&lt; d.day_of_week().as_short_string() &lt;&lt; <font class="stringliteral">"]\n"</font>;
}
<font class="keywordtype">int</font>
main()<font class="keyword"> </font>{
std::cout &lt;&lt; <font class="stringliteral">"Enter Year: "</font>;
<font class="keywordtype">int</font> year;
std::cin &gt;&gt; year;
<font class="keyword">using</font> <font class="keyword">namespace </font>gregorian;
<font class="comment">//define a collection of holidays fixed by month and day</font>
std::vector&lt;partial_date&gt; holidays;
holidays.push_back(partial_date(Jan,1)); <font class="comment">//Western New Year</font>
holidays.push_back(partial_date(Jul,4)); <font class="comment">//US Independence Day</font>
holidays.push_back(partial_date(Dec,25));<font class="comment">//Christmas day</font>
<font class="comment">//define a shorthand for the nkday function object</font>
<font class="keyword">typedef</font> gdtl::nth_kday_of_month&lt;date&gt; nkday;
<font class="comment">//define a collection of nth kday holidays</font>
std::vector&lt;nkday&gt; more_holidays;
more_holidays.push_back(nkday(nkday::first, Monday, Sep)); <font class="comment">//US labor day</font>
more_holidays.push_back(nkday(nkday::third, Monday, Jan)); <font class="comment">//MLK Day</font>
more_holidays.push_back(nkday(nkday::second, Tuesday, Feb)); <font class="comment">//Pres day</font>
more_holidays.push_back(nkday(nkday::fourth, Thursday, Nov)); <font class="comment">//Thanksgiving</font>
<font class="keyword">typedef</font> std::set&lt;date&gt; date_set;
date_set all_holidays;
<font class="preprocessor">#if (defined(BOOST_MSVC) &amp;&amp; (_MSC_VER &lt;= 1200)) // 1200 == VC++ 6.0</font>
<font class="preprocessor"></font> std::cout &lt;&lt; <font class="stringliteral">"Sorry, this example temporarily disabled on VC 6.\n"</font>
&lt;&lt; <font class="stringliteral">"The std::transform isn't accepted by the compiler\n"</font>
&lt;&lt; <font class="stringliteral">"So if you hack up the example without std::transform\n"</font>
&lt;&lt; <font class="stringliteral">"it should work\n"</font>
&lt;&lt; std::endl;
<font class="preprocessor">#else</font>
<font class="preprocessor"></font>
<font class="comment">//generate a set of concrete dates from the 'partial_date' holidays</font>
<font class="comment">//by calling the get_date functions</font>
std::transform(holidays.begin(), holidays.end(),
std::insert_iterator&lt;date_set&gt;(all_holidays, all_holidays.begin()),
std::bind2nd(std::mem_fun_ref(&amp;partial_date::get_date),
year));
<font class="comment">//Now add in the 'floating' holidays </font>
std::transform(more_holidays.begin(), more_holidays.end(),
std::insert_iterator&lt;date_set&gt;(all_holidays, all_holidays.begin()),
std::bind2nd(std::mem_fun_ref(&amp;nkday::get_date),
year));
<font class="comment">//print the holidays to the screen</font>
std::for_each(all_holidays.begin(), all_holidays.end(), print_date);
std::cout &lt;&lt; <font class="stringliteral">"Number Holidays: "</font> &lt;&lt; all_holidays.size() &lt;&lt; std::endl;
<font class="preprocessor">#endif</font>
<font class="preprocessor"></font>
<font class="keywordflow">return</font> 0;
}
</pre></div><hr><address><small>
Generated Wed Mar 13 13:38:03 2002 by <a href="http://www.doxygen.org">Doxygen</a> for <a href="http://www.crystalclearsoftware.com">CrystalClear Software</a>&nbsp;&copy 2000-2002</small></address>
</body>
</html>