Program Listing for File status.h

Return to documentation for file (src/misc/pv/status.h)

/* status.h */
/*
 * Copyright information and license terms for this software can be
 * found in the file LICENSE that is included with the distribution
 */
#ifndef STATUS_H
#define STATUS_H

#include <ostream>

#include <pv/serialize.h>
#include <pv/byteBuffer.h>
#include <pv/sharedPtr.h>

#include <shareLib.h>

namespace epics { namespace pvData {

        class epicsShareClass Status : public epics::pvData::Serializable {
            public:
                POINTER_DEFINITIONS(Status);
                enum StatusType {
                    STATUSTYPE_OK,
                    STATUSTYPE_WARNING,
                    STATUSTYPE_ERROR,
                    STATUSTYPE_FATAL
                };

                static const char* StatusTypeName[];

                static Status Ok;

            static inline Status warn(const std::string& m) { return Status(STATUSTYPE_WARNING, m); }
            static inline Status error(const std::string& m) { return Status(STATUSTYPE_ERROR, m); }
            static inline Status fatal(const std::string& m) { return Status(STATUSTYPE_FATAL, m); }

            Status() :m_statusType(STATUSTYPE_OK) {}

            Status(StatusType type, std::string const & message);

            Status(StatusType type, std::string const & message, std::string const & stackDump);

            virtual ~Status() {}

            inline StatusType getType() const { return m_statusType; }

            inline const std::string& getMessage() const { return m_message; }

            inline const std::string& getStackDump() const { return m_stackDump; }

            inline bool isOK() const {
                return (m_statusType == STATUSTYPE_OK);
            }

            inline bool isSuccess() const {
                return (m_statusType == STATUSTYPE_OK || m_statusType == STATUSTYPE_WARNING);
            }

#if __cplusplus>=201103L
            FORCE_INLINE explicit operator bool() const {
                return isSuccess();
            }
#else
        private:
            typedef bool (Status::*truth_type)() const;
        public:
            FORCE_INLINE operator truth_type() const {
                return isSuccess() ? &Status::isSuccess : 0;
            }
#endif

            void maximize(const Status& o);

            FORCE_INLINE Status& operator|=(const Status& o) {
                maximize(o);
                return *this;
            }

            void serialize(ByteBuffer *buffer, SerializableControl *flusher) const;
            void deserialize(ByteBuffer *buffer, DeserializableControl *flusher);

            void dump(std::ostream& o) const;

            private:

            StatusType m_statusType;
            std::string m_message;
            std::string m_stackDump;

        };

        FORCE_INLINE std::ostream& operator<<(std::ostream& o, const Status& status) {
            status.dump(o);
            return o;
        }

        FORCE_INLINE std::ostream& operator<<(std::ostream& o, const Status::StatusType& statusType) {
            o << Status::StatusTypeName[statusType];
            return o;
        }

}}
#endif  /* STATUS_H */