"Fossies" - the Fresh Open Source Software Archive

Member "icinga2-2.10.5/lib/base/value.hpp" (23 May 2019, 8296 Bytes) of package /linux/misc/icinga2-2.10.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "value.hpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.10.0_vs_2.10.1.

    1 /******************************************************************************
    2  * Icinga 2                                                                   *
    3  * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/)      *
    4  *                                                                            *
    5  * This program is free software; you can redistribute it and/or              *
    6  * modify it under the terms of the GNU General Public License                *
    7  * as published by the Free Software Foundation; either version 2             *
    8  * of the License, or (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 Foundation     *
   17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
   18  ******************************************************************************/
   19 
   20 #ifndef VALUE_H
   21 #define VALUE_H
   22 
   23 #include "base/object.hpp"
   24 #include "base/string.hpp"
   25 #include <boost/variant/variant.hpp>
   26 #include <boost/variant/get.hpp>
   27 #include <boost/throw_exception.hpp>
   28 
   29 namespace icinga
   30 {
   31 
   32 typedef double Timestamp;
   33 
   34 /**
   35  * The type of a Value.
   36  *
   37  * @ingroup base
   38  */
   39 enum ValueType
   40 {
   41     ValueEmpty = 0,
   42     ValueNumber = 1,
   43     ValueBoolean = 2,
   44     ValueString = 3,
   45     ValueObject = 4
   46 };
   47 
   48 /**
   49  * A type that can hold an arbitrary value.
   50  *
   51  * @ingroup base
   52  */
   53 class Value
   54 {
   55 public:
   56     Value() = default;
   57     Value(std::nullptr_t);
   58     Value(int value);
   59     Value(unsigned int value);
   60     Value(long value);
   61     Value(unsigned long value);
   62     Value(long long value);
   63     Value(unsigned long long value);
   64     Value(double value);
   65     Value(bool value);
   66     Value(const String& value);
   67     Value(String&& value);
   68     Value(const char *value);
   69     Value(const Value& other);
   70     Value(Value&& other);
   71     Value(Object *value);
   72     Value(const intrusive_ptr<Object>& value);
   73 
   74     template<typename T>
   75     Value(const intrusive_ptr<T>& value)
   76         : Value(static_pointer_cast<Object>(value))
   77     {
   78         static_assert(!std::is_same<T, Object>::value, "T must not be Object");
   79     }
   80 
   81     bool ToBool() const;
   82 
   83     operator double() const;
   84     operator String() const;
   85 
   86     Value& operator=(const Value& other);
   87     Value& operator=(Value&& other);
   88 
   89     bool operator==(bool rhs) const;
   90     bool operator!=(bool rhs) const;
   91 
   92     bool operator==(int rhs) const;
   93     bool operator!=(int rhs) const;
   94 
   95     bool operator==(double rhs) const;
   96     bool operator!=(double rhs) const;
   97 
   98     bool operator==(const char *rhs) const;
   99     bool operator!=(const char *rhs) const;
  100 
  101     bool operator==(const String& rhs) const;
  102     bool operator!=(const String& rhs) const;
  103 
  104     bool operator==(const Value& rhs) const;
  105     bool operator!=(const Value& rhs) const;
  106 
  107     template<typename T>
  108     operator intrusive_ptr<T>() const
  109     {
  110         if (IsEmpty() && !IsString())
  111             return intrusive_ptr<T>();
  112 
  113         if (!IsObject())
  114             BOOST_THROW_EXCEPTION(std::runtime_error("Cannot convert value of type '" + GetTypeName() + "' to an object."));
  115 
  116         const auto& object = Get<Object::Ptr>();
  117 
  118         ASSERT(object);
  119 
  120         intrusive_ptr<T> tobject = dynamic_pointer_cast<T>(object);
  121 
  122         if (!tobject)
  123             BOOST_THROW_EXCEPTION(std::bad_cast());
  124 
  125         return tobject;
  126     }
  127 
  128     bool IsEmpty() const;
  129     bool IsScalar() const;
  130     bool IsNumber() const;
  131     bool IsBoolean() const;
  132     bool IsString() const;
  133     bool IsObject() const;
  134 
  135     template<typename T>
  136     bool IsObjectType() const
  137     {
  138         if (!IsObject())
  139             return false;
  140 
  141         return dynamic_cast<T *>(Get<Object::Ptr>().get());
  142     }
  143 
  144     ValueType GetType() const;
  145 
  146     void Swap(Value& other);
  147 
  148     String GetTypeName() const;
  149 
  150     Type::Ptr GetReflectionType() const;
  151 
  152     Value Clone() const;
  153 
  154     template<typename T>
  155     const T& Get() const
  156     {
  157         return boost::get<T>(m_Value);
  158     }
  159 
  160 private:
  161     boost::variant<boost::blank, double, bool, String, Object::Ptr> m_Value;
  162 };
  163 
  164 extern template const double& Value::Get<double>() const;
  165 extern template const bool& Value::Get<bool>() const;
  166 extern template const String& Value::Get<String>() const;
  167 extern template const Object::Ptr& Value::Get<Object::Ptr>() const;
  168 
  169 extern Value Empty;
  170 
  171 Value operator+(const Value& lhs, const char *rhs);
  172 Value operator+(const char *lhs, const Value& rhs);
  173 
  174 Value operator+(const Value& lhs, const String& rhs);
  175 Value operator+(const String& lhs, const Value& rhs);
  176 
  177 Value operator+(const Value& lhs, const Value& rhs);
  178 Value operator+(const Value& lhs, double rhs);
  179 Value operator+(double lhs, const Value& rhs);
  180 Value operator+(const Value& lhs, int rhs);
  181 Value operator+(int lhs, const Value& rhs);
  182 
  183 Value operator-(const Value& lhs, const Value& rhs);
  184 Value operator-(const Value& lhs, double rhs);
  185 Value operator-(double lhs, const Value& rhs);
  186 Value operator-(const Value& lhs, int rhs);
  187 Value operator-(int lhs, const Value& rhs);
  188 
  189 Value operator*(const Value& lhs, const Value& rhs);
  190 Value operator*(const Value& lhs, double rhs);
  191 Value operator*(double lhs, const Value& rhs);
  192 Value operator*(const Value& lhs, int rhs);
  193 Value operator*(int lhs, const Value& rhs);
  194 
  195 Value operator/(const Value& lhs, const Value& rhs);
  196 Value operator/(const Value& lhs, double rhs);
  197 Value operator/(double lhs, const Value& rhs);
  198 Value operator/(const Value& lhs, int rhs);
  199 Value operator/(int lhs, const Value& rhs);
  200 
  201 Value operator%(const Value& lhs, const Value& rhs);
  202 Value operator%(const Value& lhs, double rhs);
  203 Value operator%(double lhs, const Value& rhs);
  204 Value operator%(const Value& lhs, int rhs);
  205 Value operator%(int lhs, const Value& rhs);
  206 
  207 Value operator^(const Value& lhs, const Value& rhs);
  208 Value operator^(const Value& lhs, double rhs);
  209 Value operator^(double lhs, const Value& rhs);
  210 Value operator^(const Value& lhs, int rhs);
  211 Value operator^(int lhs, const Value& rhs);
  212 
  213 Value operator&(const Value& lhs, const Value& rhs);
  214 Value operator&(const Value& lhs, double rhs);
  215 Value operator&(double lhs, const Value& rhs);
  216 Value operator&(const Value& lhs, int rhs);
  217 Value operator&(int lhs, const Value& rhs);
  218 
  219 Value operator|(const Value& lhs, const Value& rhs);
  220 Value operator|(const Value& lhs, double rhs);
  221 Value operator|(double lhs, const Value& rhs);
  222 Value operator|(const Value& lhs, int rhs);
  223 Value operator|(int lhs, const Value& rhs);
  224 
  225 Value operator<<(const Value& lhs, const Value& rhs);
  226 Value operator<<(const Value& lhs, double rhs);
  227 Value operator<<(double lhs, const Value& rhs);
  228 Value operator<<(const Value& lhs, int rhs);
  229 Value operator<<(int lhs, const Value& rhs);
  230 
  231 Value operator>>(const Value& lhs, const Value& rhs);
  232 Value operator>>(const Value& lhs, double rhs);
  233 Value operator>>(double lhs, const Value& rhs);
  234 Value operator>>(const Value& lhs, int rhs);
  235 Value operator>>(int lhs, const Value& rhs);
  236 
  237 bool operator<(const Value& lhs, const Value& rhs);
  238 bool operator<(const Value& lhs, double rhs);
  239 bool operator<(double lhs, const Value& rhs);
  240 bool operator<(const Value& lhs, int rhs);
  241 bool operator<(int lhs, const Value& rhs);
  242 
  243 bool operator>(const Value& lhs, const Value& rhs);
  244 bool operator>(const Value& lhs, double rhs);
  245 bool operator>(double lhs, const Value& rhs);
  246 bool operator>(const Value& lhs, int rhs);
  247 bool operator>(int lhs, const Value& rhs);
  248 
  249 bool operator<=(const Value& lhs, const Value& rhs);
  250 bool operator<=(const Value& lhs, double rhs);
  251 bool operator<=(double lhs, const Value& rhs);
  252 bool operator<=(const Value& lhs, int rhs);
  253 bool operator<=(int lhs, const Value& rhs);
  254 
  255 bool operator>=(const Value& lhs, const Value& rhs);
  256 bool operator>=(const Value& lhs, double rhs);
  257 bool operator>=(double lhs, const Value& rhs);
  258 bool operator>=(const Value& lhs, int rhs);
  259 bool operator>=(int lhs, const Value& rhs);
  260 
  261 std::ostream& operator<<(std::ostream& stream, const Value& value);
  262 std::istream& operator>>(std::istream& stream, Value& value);
  263 
  264 }
  265 
  266 extern template class boost::variant<boost::blank, double, bool, icinga::String, icinga::Object::Ptr>;
  267 
  268 #endif /* VALUE_H */