1 | // (C) Copyright 2010 Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk |
---|---|
2 | // (C) Copyright 2012 Vicente Botet |
3 | // |
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
6 | |
7 | #define BOOST_THREAD_VERSION 4 |
8 | |
9 | #include <iostream> |
10 | #include <string> |
11 | #include <boost/thread/synchronized_value.hpp> |
12 | |
13 | void addTrailingSlashIfMissing(boost::synchronized_value<std::string> & path) |
14 | { |
15 | boost::strict_lock_ptr<std::string> u=path.synchronize(); |
16 | |
17 | if(u->empty() || (*u->rbegin()!='/')) |
18 | { |
19 | *u+='/'; |
20 | } |
21 | } |
22 | |
23 | void f(const boost::synchronized_value<int> &v) { |
24 | std::cout<<"v="<<*v<<std::endl; |
25 | } |
26 | |
27 | void g(const boost::const_strict_lock_ptr<int> &v) { |
28 | std::cout<<"v="<<*v<<std::endl; |
29 | } |
30 | |
31 | bool checkIfMissingTrailingSlash(boost::synchronized_value<std::string> & path) |
32 | { |
33 | boost::strict_lock_ptr<std::string> u=path.synchronize(); |
34 | |
35 | return (u->empty() || (*u->rbegin()!='/')); |
36 | } |
37 | |
38 | int main() |
39 | { |
40 | { |
41 | boost::synchronized_value<int> v1; |
42 | *v1=42; |
43 | std::cout<<"v1="<<*v1<<std::endl; |
44 | f(v: v1); |
45 | int i=*v1; |
46 | std::cout<<"i="<<i<<std::endl; |
47 | |
48 | { |
49 | boost::strict_lock_ptr<int> u=v1.synchronize(); |
50 | |
51 | *u+=43; |
52 | std::cout<<"v1="<<*u<<std::endl; |
53 | g(v: u); |
54 | } |
55 | boost::synchronized_value<int> v2(2); |
56 | std::cout<<"v2="<<*v2<<std::endl; |
57 | v2 = 3; |
58 | std::cout<<"v2="<<*v2<<std::endl; |
59 | |
60 | boost::synchronized_value<int> v3(v2); |
61 | std::cout<<"v3="<<*v3<<std::endl; |
62 | v3 = v1; |
63 | std::cout<<"v3="<<*v3<<std::endl; |
64 | |
65 | std::cout<<"v2="<<*v3<<std::endl; |
66 | std::cout<<"v3="<<*v3<<std::endl; |
67 | swap(lhs&: v3,rhs&: v2); |
68 | v1.swap(rhs&: v2); |
69 | std::cout<<"v3="<<*v3<<std::endl; |
70 | } |
71 | { |
72 | boost::synchronized_value<std::string> s; |
73 | addTrailingSlashIfMissing(path&: s); |
74 | std::cout<<"s="<<std::string(*s)<<std::endl; |
75 | } |
76 | { |
77 | boost::synchronized_value<std::string> s; |
78 | s->append(s: "foo/"); |
79 | s.synchronize()->append(s: "foo"); |
80 | addTrailingSlashIfMissing(path&: s); |
81 | std::cout<<"s="<<std::string(*s)<<std::endl; |
82 | } |
83 | { |
84 | boost::synchronized_value<std::string> s; |
85 | s = std::string("foo/"); |
86 | std::cout<<"ss="<< s << std::endl; |
87 | } |
88 | { |
89 | boost::synchronized_value<std::string> s; |
90 | s = "foo/"; |
91 | std::cout<<"ss="<< s << std::endl; |
92 | } |
93 | { |
94 | boost::synchronized_value<std::string> s1("a"); |
95 | boost::synchronized_value<std::string> s2; |
96 | s2=s1; |
97 | std::cout<<"s1="<< s1 << std::endl; |
98 | std::cout<<"s2="<< s2 << std::endl; |
99 | } |
100 | { |
101 | boost::synchronized_value<std::string> s1("a"); |
102 | boost::synchronized_value<std::string> s2("b"); |
103 | std::cout<<"s1="<< s1 << std::endl; |
104 | std::cout<<"s2="<< s2 << std::endl; |
105 | swap(lhs&: s1,rhs&: s2); |
106 | std::cout<<"s1="<< s1 << std::endl; |
107 | std::cout<<"s2="<< s2 << std::endl; |
108 | } |
109 | #if ! defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS) |
110 | { |
111 | boost::synchronized_value<std::string> sts("a"); |
112 | std::string s(sts); |
113 | std::cout<<"ssts="<< s << std::endl; |
114 | } |
115 | #endif |
116 | { |
117 | boost::synchronized_value<int> s1(1); |
118 | boost::synchronized_value<int> s2(1); |
119 | BOOST_ASSERT(s1==s2); |
120 | BOOST_ASSERT(s1<=s2); |
121 | BOOST_ASSERT(s1>=s2); |
122 | BOOST_ASSERT(s1==1); |
123 | BOOST_ASSERT(s1<=1); |
124 | BOOST_ASSERT(s1>=1); |
125 | } |
126 | { |
127 | boost::synchronized_value<int> s1(1); |
128 | boost::synchronized_value<int> s2(2); |
129 | BOOST_ASSERT(s1!=s2); |
130 | BOOST_ASSERT(s1!=2); |
131 | BOOST_ASSERT(2!=s1); |
132 | } |
133 | { |
134 | boost::synchronized_value<int> s1(1); |
135 | boost::synchronized_value<int> s2(2); |
136 | BOOST_ASSERT(s1<s2); |
137 | BOOST_ASSERT(s1<=s2); |
138 | BOOST_ASSERT(s2>s1); |
139 | BOOST_ASSERT(s2>=s1); |
140 | BOOST_ASSERT(s1<2); |
141 | BOOST_ASSERT(s1<=2); |
142 | BOOST_ASSERT(s2>1); |
143 | BOOST_ASSERT(s2>=1); |
144 | } |
145 | return 0; |
146 | } |
147 | |
148 |