Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / test / optvalue.cxx
blob6d1ad86e753559a5141185b11430edf4c484025e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include "config_clang.h"
12 template <typename Type> class OptValue
14 public:
15 OptValue()
16 : maValue()
17 , mbHasValue(false)
20 explicit OptValue(const Type& rValue)
21 : maValue(rValue)
22 , mbHasValue(true)
26 bool has_value() const { return mbHasValue; }
27 bool operator!() const { return !mbHasValue; }
29 const Type& value() const { return maValue; }
30 const Type& value_or(const Type& rDefValue) const { return mbHasValue ? maValue : rDefValue; }
32 Type& operator*() { return maValue; }
33 Type& emplace()
35 mbHasValue = true;
36 maValue = Type();
37 return maValue;
40 OptValue& operator=(const Type& rValue)
42 maValue = rValue;
43 mbHasValue = true;
44 return *this;
46 bool operator==(const OptValue& rValue) const
48 return ((!mbHasValue && rValue.mbHasValue == false)
49 || (mbHasValue == rValue.mbHasValue && maValue == rValue.maValue));
52 private:
53 Type maValue;
54 bool mbHasValue;
57 struct AttributeList
59 OptValue<int> getInteger();
62 namespace test1
64 void foo(AttributeList& rAttrs)
66 // expected-error@+1 {{call to OptValue::value() [loplugin:optvalue]}}
67 rAttrs.getInteger().value();
69 // no warning expected
70 OptValue<int> x;
71 x.value();
75 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */