Revision 52e20f9e host/lib/convert/convert_impl.cpp

b/host/lib/convert/convert_impl.cpp
18 18
#include <uhd/convert.hpp>
19 19
#include <uhd/utils/log.hpp>
20 20
#include <uhd/utils/static.hpp>
21
#include <uhd/types/dict.hpp>
21 22
#include <uhd/exception.hpp>
23
#include <boost/cstdint.hpp>
24
#include <boost/format.hpp>
25
#include <complex>
22 26

  
23 27
using namespace uhd;
24 28

  
25
#include "convert_pred.hpp"
29
bool convert::operator==(const convert::id_type &lhs, const convert::id_type &rhs){
30
    return
31
        (lhs.input_markup == rhs.input_markup) and
32
        (lhs.num_inputs == rhs.num_inputs) and
33
        (lhs.output_markup == rhs.output_markup) and
34
        (lhs.num_outputs == rhs.num_outputs) and
35
        (lhs.args == rhs.args)
36
    ;
37
}
38

  
39
std::string convert::id_type::to_pp_string(void) const{
40
    return str(boost::format(
41
        "conversion ID\n"
42
        "  Input markup: %s\n"
43
        "  Num inputs: %d\n"
44
        "  Output markup: %s\n"
45
        "  Num outputs: %d\n"
46
        "  Optional args: %s\n"
47
    )
48
        % this->input_markup
49
        % this->num_inputs
50
        % this->output_markup
51
        % this->num_outputs
52
        % this->args
53
    );
54
}
26 55

  
27 56
/***********************************************************************
28 57
 * Define types for the function tables
......
30 59
struct fcn_table_entry_type{
31 60
    convert::priority_type prio;
32 61
    convert::function_type fcn;
33
    fcn_table_entry_type(void)
34
    : prio(convert::PRIORITY_EMPTY), fcn(NULL){
35
        /* NOP */
36
    }
37 62
};
38
typedef std::vector<fcn_table_entry_type> fcn_table_type;
39 63

  
40 64
/***********************************************************************
41 65
 * Setup the table registry
42 66
 **********************************************************************/
43
UHD_SINGLETON_FCN(fcn_table_type, get_cpu_to_otw_table);
44
UHD_SINGLETON_FCN(fcn_table_type, get_otw_to_cpu_table);
45

  
46
fcn_table_type &get_table(dir_type dir){
47
    switch(dir){
48
    case DIR_OTW_TO_CPU: return get_otw_to_cpu_table();
49
    case DIR_CPU_TO_OTW: return get_cpu_to_otw_table();
50
    }
51
    UHD_THROW_INVALID_CODE_PATH();
52
}
67
typedef uhd::dict<convert::id_type, fcn_table_entry_type> fcn_table_type;
68
UHD_SINGLETON_FCN(fcn_table_type, get_table);
53 69

  
54 70
/***********************************************************************
55 71
 * The registry functions
56 72
 **********************************************************************/
57 73
void uhd::convert::register_converter(
58
    const std::string &markup,
74
    const id_type &id,
59 75
    function_type fcn,
60 76
    priority_type prio
61 77
){
62
    //extract the predicate and direction from the markup
63
    dir_type dir;
64
    pred_type pred = make_pred(markup, dir);
65

  
66 78
    //get a reference to the function table
67
    fcn_table_type &table = get_table(dir);
68

  
69
    //resize the table so that its at least pred+1
70
    if (table.size() <= pred) table.resize(pred+1);
79
    fcn_table_type &table = get_table();
71 80

  
72 81
    //register the function if higher priority
73
    if (table[pred].prio < prio){
74
        table[pred].fcn = fcn;
75
        table[pred].prio = prio;
82
    if (not table.has_key(id) or table[id].prio < prio){
83
        table[id].fcn = fcn;
84
        table[id].prio = prio;
76 85
    }
77 86

  
78 87
    //----------------------------------------------------------------//
79
    UHD_LOGV(always) << "register_converter: " << markup << std::endl
88
    UHD_LOGV(always) << "register_converter: " << id.to_pp_string() << std::endl
80 89
        << "    prio: " << prio << std::endl
81
        << "    pred: " << pred << std::endl
82
        << "    dir: " << dir << std::endl
83 90
        << std::endl
84 91
    ;
85 92
    //----------------------------------------------------------------//
......
88 95
/***********************************************************************
89 96
 * The converter functions
90 97
 **********************************************************************/
91
const convert::function_type &convert::get_converter_cpu_to_otw(
92
    const io_type_t &io_type,
93
    const otw_type_t &otw_type,
94
    size_t num_input_buffs,
95
    size_t num_output_buffs
96
){
97
    pred_type pred = make_pred(io_type, otw_type, num_input_buffs, num_output_buffs);
98
    return get_cpu_to_otw_table().at(pred).fcn;
98
convert::function_type convert::get_converter(const id_type &id){
99
    if (get_table().has_key(id)) return get_table()[id].fcn;
100
    throw uhd::key_error("Cannot find a conversion routine for " + id.to_pp_string());
99 101
}
100 102

  
101
const convert::function_type &convert::get_converter_otw_to_cpu(
102
    const io_type_t &io_type,
103
    const otw_type_t &otw_type,
104
    size_t num_input_buffs,
105
    size_t num_output_buffs
103
/***********************************************************************
104
 * Mappings for item markup to byte size for all items we can
105
 **********************************************************************/
106
typedef uhd::dict<std::string, size_t> item_size_type;
107
UHD_SINGLETON_FCN(item_size_type, get_item_size_table);
108

  
109
void register_bytes_per_item(
110
    const std::string &markup, const size_t size
106 111
){
107
    pred_type pred = make_pred(io_type, otw_type, num_input_buffs, num_output_buffs);
108
    return get_otw_to_cpu_table().at(pred).fcn;
112
    get_item_size_table()[markup] = size;
113
}
114

  
115
size_t convert::get_bytes_per_item(const std::string &markup){
116
    if (get_item_size_table().has_key(markup)) return get_item_size_table()[markup];
117
    throw uhd::key_error("Cannot find an item size " + markup);
118
}
119

  
120
UHD_STATIC_BLOCK(convert_register_item_sizes){
121
    //register standard complex types
122
    get_item_size_table()["fc64"] = sizeof(std::complex<double>);
123
    get_item_size_table()["fc32"] = sizeof(std::complex<float>);
124
    get_item_size_table()["sc32"] = sizeof(std::complex<boost::int32_t>);
125
    get_item_size_table()["sc16"] = sizeof(std::complex<boost::int16_t>);
126
    get_item_size_table()["sc8"] = sizeof(std::complex<boost::int8_t>);
127

  
128
    //register standard real types
129
    get_item_size_table()["f64"] = sizeof(double);
130
    get_item_size_table()["f32"] = sizeof(float);
131
    get_item_size_table()["s32"] = sizeof(boost::int32_t);
132
    get_item_size_table()["s16"] = sizeof(boost::int16_t);
133
    get_item_size_table()["s8"] = sizeof(boost::int8_t);
109 134
}

Also available in: Unified diff