本文整理汇总了C++中optional类的典型用法代码示例。如果您正苦于以下问题:C++ optional类的具体用法?C++ optional怎么用?C++ optional使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了optional类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
void
ThreadedLoader::load_patch(bool merge,
const Glib::ustring& document_uri,
optional<Path> engine_parent,
optional<Symbol> engine_symbol,
optional<GraphObject::Properties> engine_data)
{
_mutex.lock();
Ingen::Shared::World* world = App::instance().world();
Glib::ustring engine_base = "";
if (engine_parent) {
if (merge)
engine_base = engine_parent.get().str();
else
engine_base = engine_parent.get().base();
}
_events.push_back(
sigc::hide_return(
sigc::bind(sigc::mem_fun(world->parser().get(),
&Ingen::Serialisation::Parser::parse_file),
App::instance().world(),
App::instance().world()->engine().get(),
document_uri,
engine_parent,
engine_symbol,
engine_data)));
whip();
_mutex.unlock();
}
示例2: LD
/// Stores a file generated by a test case into the database as a BLOB.
///
/// \param name The name of the file to store in the database. This needs to be
/// unique per test case. The caller is free to decide what names to use
/// for which files. For example, it might make sense to always call
/// __STDOUT__ the stdout of the test case so that it is easy to locate.
/// \param path The path to the file to be stored.
/// \param test_case_id The identifier of the test case this file belongs to.
///
/// \return The identifier of the stored file, or none if the file was empty.
///
/// \throw store::error If there are problems writing to the database.
optional< int64_t >
store::write_transaction::put_test_case_file(const std::string& name,
const fs::path& path,
const int64_t test_case_id)
{
LD(F("Storing %s (%s) of test case %s") % name % path % test_case_id);
try {
const optional< int64_t > file_id = put_file(_pimpl->_db, path);
if (!file_id) {
LD("Not storing empty file");
return none;
}
sqlite::statement stmt = _pimpl->_db.create_statement(
"INSERT INTO test_case_files (test_case_id, file_name, file_id) "
"VALUES (:test_case_id, :file_name, :file_id)");
stmt.bind(":test_case_id", test_case_id);
stmt.bind(":file_name", name);
stmt.bind(":file_id", file_id.get());
stmt.step_without_results();
return optional< int64_t >(_pimpl->_db.last_insert_rowid());
} catch (const sqlite::error& e) {
throw error(e.what());
}
}
示例3: cowsay
void cowsay(Console & drawConsole, const string & name,
const optional<string> & text,
const optional<int> & xOffset,
bool noSkip)
{
const auto image = loadImage(name + ".bmp");
ConsoleStuff console;
if (text)
{
auto wordWidth = image.Width() / console.GetFontSize().X;
if (text.get().size() < wordWidth)
{
wordWidth = text.get().size();
}
wordBubble(text.get(), (image.Width() / console.GetFontSize().X));
}
const int linesToSkip = noSkip ? 0
: image.Height() / console.GetFontSize().Y;
for (size_t n = 0; n < linesToSkip; ++ n) {
std::cout << "\n";
}
auto textCoordinates = console.GetTextCoordinates();
auto c = console.ToPixelCoordinates(
{textCoordinates.X + xOffset.get_value_or(0),
textCoordinates.Y - linesToSkip});
drawCow(drawConsole, c, image);
}
示例4: main
int main()
{
{
const optional<X> opt; ((void)opt);
ASSERT_NOT_NOEXCEPT(opt.value());
ASSERT_SAME_TYPE(decltype(opt.value()), X const&);
}
{
constexpr optional<X> opt(in_place);
static_assert(opt.value().test() == 3, "");
}
{
const optional<X> opt(in_place);
assert(opt.value().test() == 3);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
{
const optional<X> opt;
try
{
(void)opt.value();
assert(false);
}
catch (const bad_optional_access&)
{
}
}
#endif
}
示例5: test_function_value_for
void test_function_value_for()
{
optional<T> o0;
optional<T> o1(1);
const optional<T> oC(2);
try
{
T& v = o1.value();
BOOST_TEST(v == 1);
}
catch(...)
{
BOOST_TEST(false);
}
try
{
T const& v = oC.value();
BOOST_TEST(v == 2);
}
catch(...)
{
BOOST_TEST(false);
}
BOOST_TEST_THROWS(o0.value(), boost::bad_optional_access);
}
示例6:
void
ThreadedLoader::load_graph(bool merge,
const Glib::ustring& document_uri,
optional<Raul::Path> engine_parent,
optional<Raul::Symbol> engine_symbol,
optional<Node::Properties> engine_data)
{
_mutex.lock();
Ingen::World* world = _app.world();
Glib::ustring engine_base = "";
if (engine_parent) {
if (merge)
engine_base = engine_parent.get();
else
engine_base = engine_parent.get().base();
}
_events.push_back(
sigc::hide_return(
sigc::bind(sigc::mem_fun(world->parser().get(),
&Ingen::Serialisation::Parser::parse_file),
_app.world(),
_app.world()->interface().get(),
document_uri,
engine_parent,
engine_symbol,
engine_data)));
_mutex.unlock();
_sem.post();
}
示例7: TEST
TEST(Optional, Constexpr)
{
constexpr auto string = "abcdefgh";
constexpr optional<int> a{32};
constexpr optional<decltype(string)> b{string};
constexpr optional<float> c{3.14f};
constexpr optional<int> d{};
EXPECT_THAT(*a, Eq(32));
EXPECT_THAT(*b, Eq(string));
EXPECT_THAT(*c, FloatEq(3.14f));
EXPECT_FALSE(d);
constexpr int i = *a;
constexpr int j = a.value();
EXPECT_THAT(i, Eq(*a));
EXPECT_THAT(j, Eq(*a));
struct Literal { };
static_assert(std::is_literal_type<optional<int>>::value, "Type error");
static_assert(std::is_literal_type<optional<float>>::value, "Type error");
static_assert(std::is_literal_type<optional<Literal>>::value, "Type error");
}
示例8: get_abi_serializer
static const abi_serializer& get_abi_serializer() {
static optional<abi_serializer> _abi_serializer;
if (!_abi_serializer) {
_abi_serializer.emplace(eosio_contract_abi(abi_def()));
}
return *_abi_serializer;
}
示例9: mergeAndOverride
void
CachePolicy::mergeAndOverride(const optional<CachePolicy>& rhs)
{
if ( rhs.isSet() )
{
mergeAndOverride( rhs.get() );
}
}
示例10: if
bool operator==( const optional<T>& left, const optional<T>& right ) {
if ( left && right ) {
return left.value( ) == right.value( );
}
else if ( !left && !right ) {
return true;
}
return false;
}
示例11: return
bool const operator==(optional const & other) const {
if (!valid() && !other.valid()) {
return true;
}
if (valid() ^ other.valid()) {
return false;
}
return ((**this) == (*other));
}
示例12: Source_location
Source_location(
string filename,
optional<int> linenumber = {},
optional<int> character_position = {},
optional<string> function_name = {}
):
m_filename{ move( filename ) },
m_linenumber{ linenumber.value_or( -1 ) },
m_character_position{ character_position.value_or( -1 ) },
m_function_name{ move( function_name ).value_or( "" ) }
{}
示例13: VerifyCommandHandling
// Вспомогательная функция для проверки работы команды command
// Она принимает ожидаемый номер канала expectedChannel и expectedOutput
void VerifyCommandHandling(const string& command, const optional<int> & expectedChannel, const string& expectedOutput)
{
// Предварительно очищаем содержимое выходного потока
output = stringstream();
input = stringstream();
BOOST_CHECK(input << command);
BOOST_CHECK(remoteControl.HandleCommand());
BOOST_CHECK_EQUAL(tv.IsTurnedOn(), expectedChannel.is_initialized());
BOOST_CHECK_EQUAL(tv.GetChannel(), expectedChannel.get_value_or(0));
BOOST_CHECK(input.eof());
BOOST_CHECK_EQUAL(output.str(), expectedOutput);
}
示例14: affect
static void affect( value& v, optional< T > const& opt )
{
if( opt.is_none() )
{
v = Val_int( 0 );
}
else
{
v = caml_alloc_tuple(1);
field_affectation_management< T >::affect_field(v, 0, opt.get_value() );
}
}
示例15: swap
void swap(optional& other) {
if (other.valid() && valid()) {
swap(**this, *other);
}
else if (!other.valid() && !valid()) {
return;
}
optional& source = other ? *this : other;
optional& target = other ? other : *this;
target.unchecked_place(std::move(*source));
source.unchecked_destroy();
}