Skip to content

Commit 42a521e

Browse files
committed
Add more example code to cxx_modules_cheatsheet.md
1 parent 8589b36 commit 42a521e

1 file changed

Lines changed: 170 additions & 2 deletions

File tree

docs/cxx_modules_cheatsheeet.md

Lines changed: 170 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,189 @@ export int foo();
7575
---
7676

7777
## 7️⃣ Header-Only Library Example
78+
79+
### the boost/any/modules/boost_any.cppm
80+
7881
```cpp
82+
// Copyright (c) 2016-2025 Antony Polukhin
83+
//
84+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
85+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
86+
7987
module;
88+
89+
#include <boost/assert.hpp>
8090
#include <boost/config.hpp>
91+
#include <boost/throw_exception.hpp>
8192

82-
#ifdef USE_STD_MODULE
93+
// FIXME(CK): #include <boost/type_index.hpp>
94+
import boost.type_index;
95+
96+
#ifdef BOOST_ANY_USE_STD_MODULE
8397
import std;
8498
#else
8599
#include <memory>
100+
#include <stdexcept>
101+
#include <typeinfo>
86102
#include <type_traits>
103+
#include <utility>
87104
#endif
88105

106+
#define BOOST_ANY_INTERFACE_UNIT
107+
89108
export module boost.any;
90-
#include <boost/any.hpp> // exported
109+
110+
#ifdef __clang__
111+
# pragma clang diagnostic ignored "-Winclude-angled-in-module-purview"
112+
#endif
113+
114+
#include <boost/any.hpp>
115+
#include <boost/any/basic_any.hpp>
116+
#include <boost/any/unique_any.hpp>
117+
91118
```
92119

120+
### the boost/any.hpp
121+
122+
```cpp
123+
// See http://www.boost.org/libs/any for Documentation.
124+
125+
#ifndef BOOST_ANY_INCLUDED
126+
#define BOOST_ANY_INCLUDED
127+
128+
#include <boost/any/detail/config.hpp>
129+
130+
#if !defined(BOOST_USE_MODULES) || defined(BOOST_ANY_INTERFACE_UNIT)
131+
132+
#ifndef BOOST_ANY_INTERFACE_UNIT
133+
#include <boost/config.hpp>
134+
#ifdef BOOST_HAS_PRAGMA_ONCE
135+
# pragma once
136+
#endif
137+
138+
#include <memory> // for std::addressof
139+
#include <type_traits>
140+
141+
#include <boost/throw_exception.hpp>
142+
#include <boost/type_index.hpp>
143+
144+
#endif // #ifndef BOOST_ANY_INTERFACE_UNIT
145+
146+
#include <boost/any/bad_any_cast.hpp>
147+
#include <boost/any/fwd.hpp>
148+
#include <boost/any/detail/placeholder.hpp>
149+
150+
namespace boost {
151+
152+
BOOST_ANY_BEGIN_MODULE_EXPORT
153+
154+
/// \brief A class whose instances can hold instances of any
155+
/// type that satisfies \forcedlink{ValueType} requirements.
156+
class any
157+
{
158+
public:
159+
160+
/// \post this->empty() is true.
161+
constexpr any() noexcept
162+
: content(0)
163+
{
164+
}
165+
166+
/// Makes a copy of `value`, so
167+
168+
/// ... #### more code ###
169+
170+
private: // representation
171+
template<typename ValueType>
172+
friend ValueType * unsafe_any_cast(any *) noexcept;
173+
174+
friend class boost::anys::unique_any;
175+
176+
placeholder * content;
177+
/// @endcond
178+
};
179+
180+
/// Exchange of the contents of `lhs` and `rhs`.
181+
/// \throws Nothing.
182+
inline void swap(any & lhs, any & rhs) noexcept
183+
{
184+
lhs.swap(rhs);
185+
}
186+
187+
/// ... ### more code ###
188+
189+
/// \returns ValueType stored in `operand`
190+
/// \throws boost::bad_any_cast if `operand` does not contain
191+
/// specified ValueType.
192+
template<typename ValueType>
193+
ValueType any_cast(any & operand)
194+
{
195+
using nonref = typename std::remove_reference<ValueType>::type;
196+
197+
nonref * result = boost::any_cast<nonref>(std::addressof(operand));
198+
if(!result)
199+
boost::throw_exception(bad_any_cast());
200+
201+
// Attempt to avoid construction of a temporary object in cases when
202+
// `ValueType` is not a reference. Example:
203+
// `static_cast<std::string>(*result);`
204+
// which is equal to `std::string(*result);`
205+
typedef typename std::conditional<
206+
std::is_reference<ValueType>::value,
207+
ValueType,
208+
typename std::add_lvalue_reference<ValueType>::type
209+
>::type ref_type;
210+
211+
#ifdef BOOST_MSVC
212+
# pragma warning(push)
213+
# pragma warning(disable: 4172) // "returning address of local variable or temporary" but *result is not local!
214+
#endif
215+
return static_cast<ref_type>(*result);
216+
#ifdef BOOST_MSVC
217+
# pragma warning(pop)
218+
#endif
219+
}
220+
221+
/// \returns `ValueType` stored in `operand`
222+
/// \throws boost::bad_any_cast if `operand` does not contain
223+
/// specified `ValueType`.
224+
template<typename ValueType>
225+
inline ValueType any_cast(const any & operand)
226+
{
227+
using nonref = typename std::remove_reference<ValueType>::type;
228+
return boost::any_cast<const nonref &>(const_cast<any &>(operand));
229+
}
230+
231+
/// \returns `ValueType` stored in `operand`, leaving the `operand` empty.
232+
/// \throws boost::bad_any_cast if `operand` does not contain
233+
/// specified `ValueType`.
234+
template<typename ValueType>
235+
inline ValueType any_cast(any&& operand)
236+
{
237+
static_assert(
238+
std::is_rvalue_reference<ValueType&&>::value /*true if ValueType is rvalue or just a value*/
239+
|| std::is_const< typename std::remove_reference<ValueType>::type >::value,
240+
"boost::any_cast shall not be used for getting nonconst references to temporary objects"
241+
);
242+
return boost::any_cast<ValueType>(operand);
243+
}
244+
245+
BOOST_ANY_END_MODULE_EXPORT
246+
247+
}
248+
249+
// Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
250+
// Copyright Antony Polukhin, 2013-2025.
251+
//
252+
// Distributed under the Boost Software License, Version 1.0. (See
253+
// accompanying file LICENSE_1_0.txt or copy at
254+
// http://www.boost.org/LICENSE_1_0.txt)
255+
256+
#endif // #if !defined(BOOST_USE_MODULES) || defined(BOOST_ANY_INTERFACE_UNIT)
257+
258+
#endif
259+
260+
```
93261
---
94262
95263
## 8️⃣ Quick Rules Summary

0 commit comments

Comments
 (0)