Skip to content

File element.h#

File List > include > pisound-micro > element.h

Go to the documentation of this file

// SPDX-License-Identifier: LGPL-2.1-only
//
// libpisoundmicro - a utility library for Pisound Micro I/O expander capabilities.
// Copyright (c) 2017-2025 Vilniaus Blokas UAB, https://blokas.io/
//
// This file is part of libpisoundmicro.
//
// libpisoundmicro is free software: you can redistribute it and/or modify it under the terms of the
// GNU Lesser General Public License as published by the Free Software Foundation, version 2.1 of the License.
//
// libpisoundmicro is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
// for more details.
//
// You should have received a copy of the GNU Lesser General Public License along with libpisoundmicro. If not, see <https://www.gnu.org/licenses/>.

#ifndef PISOUND_MICRO_ELEMENT_H
#define PISOUND_MICRO_ELEMENT_H

#ifndef PISOUND_MICRO_H
#   error Please #include <pisound-micro.h>!
#endif

namespace upisnd
{

class UPISND_API Element
{
public:
    Element();

    explicit Element(upisnd_element_ref_t el);

    Element(const Element &rhs);
    Element &operator=(const Element &rhs);

    Element(Element &&rhs);
    Element& operator=(Element &&rhs);

    ~Element();

    static Element get(ElementName name);

    static Element setup(ElementName name, upisnd_setup_t setup);

    bool isValid() const;

    void release();

    const char *getName() const;

    upisnd_element_type_e getType() const;

    upisnd_pin_t getPin() const;

    ValueFd openValueFd(int flags) const;

    template <typename T>
    inline T as() const
    {
        if (classType<T>() == getType())
            return T(ref());
        return T();
    }

protected:
    inline upisnd_element_ref_t ref() const { return m_ref; }

    // Copy el without changing the refcount, for internal use.
    Element(upisnd_element_ref_t el, bool);

private:
    template <typename T>
    static upisnd_element_type_e classType() { return UPISND_ELEMENT_TYPE_INVALID; };

    upisnd_element_ref_t m_ref;
};

template <> upisnd_element_type_e Element::classType<Encoder>();
template <> upisnd_element_type_e Element::classType<AnalogInput>();
template <> upisnd_element_type_e Element::classType<Gpio>();
template <> upisnd_element_type_e Element::classType<Activity>();

} // namespace upisnd

#endif // PISOUND_MICRO_ELEMENT_H