aboutsummaryrefslogtreecommitdiffstats
path: root/include/usrp_uhd/gain_handler.hpp
blob: 786db993b6c2f6457c8984b8a5341a40350e1d8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
// Copyright 2010 Ettus Research LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#include <boost/shared_ptr.hpp>
#include <usrp_uhd/wax.hpp>
#include <usrp_uhd/props.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>

#ifndef INCLUDED_USRP_UHD_GAIN_HANDLER_HPP
#define INCLUDED_USRP_UHD_GAIN_HANDLER_HPP

namespace usrp_uhd{

class gain_handler{
public:
    typedef boost::shared_ptr<gain_handler> sptr;

    template <class T> gain_handler(
        wax::obj *wax_obj_ptr, const T &gain_prop,
        const T &gain_min_prop, const T &gain_max_prop,
        const T &gain_step_prop, const T &gain_names_prop
    ){
        _wax_obj_ptr = wax_obj_ptr;
        _gain_prop = gain_prop;
        _gain_min_prop = gain_min_prop;
        _gain_max_prop = gain_max_prop;
        _gain_step_prop = gain_step_prop;
        _gain_names_prop = gain_names_prop;
        _is_equal = boost::bind(&gain_handler::is_equal<T>, _1, _2);
    }

    ~gain_handler(void);

    /*!
     * Intercept gets for overall gain, min, max, step.
     * Ensures that the gain name is valid.
     * \return true for handled, false to pass on
     */
    bool intercept_get(const wax::obj &key, wax::obj &val);

    /*!
     * Intercept sets for overall gain.
     * Ensures that the gain name is valid.
     * Ensures that the new gain is within range.
     * \return true for handled, false to pass on
     */
    bool intercept_set(const wax::obj &key, const wax::obj &val);

private:

    wax::obj     *_wax_obj_ptr;
    wax::obj      _gain_prop;
    wax::obj      _gain_min_prop;
    wax::obj      _gain_max_prop;
    wax::obj      _gain_step_prop;
    wax::obj      _gain_names_prop;

    /*!
     * Verify that the key is valid:
     * If its a named prop for gain, ensure that name is valid.
     * If the name if not valid, throw a std::invalid_argument.
     * The name can only be valid if its in the list of gain names.
     */
    void _check_key(const wax::obj &key);

    /*
     * Private interface to test if two wax types are equal:
     * The constructor will bind an instance of this for a specific type.
     * This bound equals functions allows the intercept methods to be non-templated.
     */
    template <class T> static bool is_equal(const wax::obj &a, const wax::obj &b){
        try{
            return wax::cast<T>(a) == wax::cast<T>(b);
        }
        catch(const wax::bad_cast &){
            return false;
        }
    }
    boost::function<bool(const wax::obj &, const wax::obj &)> _is_equal;

};

} //namespace usrp_uhd

#endif /* INCLUDED_USRP_UHD_GAIN_HANDLER_HPP */