Switch time() calls to monotonic clock calls (#7597)
[libt2n] / examples-codegen / example2 / default.cpp
1 /*
2     Copyright (C) 2006                                                    
3     intra2net.com                                                         
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19 #include "default_common.hxx"
20
21 //! test pass by copy
22 LIBT2N_EXPORT std::string testfunc(std::string str) 
23 {
24     std::string ret;
25     if (str=="throw")
26         throw libt2n::t2n_runtime_error("throw me around");
27     if (str=="big")
28         ret.insert(0,100*1024,'x');
29     else
30         ret=str+", testfunc() was here";
31     return ret;
32 }
33
34 //! test pass by const reference
35 LIBT2N_EXPORT std::string testfunc_ref(const std::string &str) 
36 {
37     std::string ret;
38     if (str=="throw")
39         throw libt2n::t2n_runtime_error("throw me around");
40     if (str=="big")
41         ret.insert(0,100*1024,'x');
42     else
43         ret=str+", testfunc() was here";
44     return ret;
45 }
46
47 //! test builtin type
48 LIBT2N_EXPORT int t2(int i) 
49 {
50     return i;
51 }
52
53 //! test const ref of builtin
54 LIBT2N_EXPORT int t3(const int &i) 
55 {
56     return i;
57 }
58
59 //! test pointer to const data
60 LIBT2N_EXPORT int t4(const int* i) 
61 {
62     return *i;
63 }
64
65 //! test no args
66 LIBT2N_EXPORT int t5() 
67 {
68     return 10;
69 }
70
71 //! test no args and no return
72 LIBT2N_EXPORT void t6() 
73 {
74     throw libt2n::t2n_runtime_error("throw me around2");
75 }
76
77 template <typename X>
78 unsigned
79 sumT(const X &x) {
80   unsigned res=0;
81   for (typename X::const_iterator it=x.begin();it!=x.end();++it)
82     res+=*it;
83   return res;
84 }
85
86 // todo: export of explicitely instantiated template functions doesn't work
87
88 //! test vector
89 LIBT2N_EXPORT unsigned sum(const std::vector<unsigned> &v)
90 {return sumT(v);}
91
92 //! test list
93 LIBT2N_EXPORT unsigned sum(const std::list<unsigned> &v)
94 {return sumT(v);}
95
96 //! test set
97 LIBT2N_EXPORT unsigned sum(const std::set<unsigned> &v)
98 {return sumT(v);}
99
100 //! test return of std::pair
101 LIBT2N_EXPORT std::pair<int,int> pdup(int i) {
102   return std::pair<int,int>(i,i);
103 }
104
105 /*
106   todo: boost doesn't provide serialization for boost tuple :-(
107   //! test return of boost::tuple
108   LIBT2N_EXPORT boost::tuple<int,int,int,int> quad(int i) {
109   return std::tuple<int,int,int,int>(i,i,i,i);
110 }
111 */