File: ConverterRegistry.cpp

package info (click to toggle)
soapysdr 0.8.1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 948 kB
  • sloc: cpp: 5,378; ansic: 471; python: 311; sh: 21; makefile: 18
file content (155 lines) | stat: -rw-r--r-- 5,320 bytes parent folder | download | duplicates (4)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (c) 2017-2017 Coburn Wightman
// Copyright (c) 2018-2018 Josh Blum
// SPDX-License-Identifier: BSL-1.0

#include <SoapySDR/ConverterRegistry.hpp>
#include <algorithm>
#include <stdexcept>

void lateLoadDefaultConverters(void);

static SoapySDR::ConverterRegistry::FormatConverters formatConverters;

SoapySDR::ConverterRegistry::ConverterRegistry(const std::string &sourceFormat, const std::string &targetFormat, const FunctionPriority &priority, ConverterFunction converterFunction)
{
  if (formatConverters.count(sourceFormat) == 0)
    ;
  else if (formatConverters[sourceFormat].count(targetFormat) == 0)
    ;
  else if (formatConverters[sourceFormat][targetFormat].count(priority) != 0)
    {
      SoapySDR::logf(SOAPY_SDR_ERROR, "SoapySDR::ConverterRegistry(%s, %s, %s) duplicate registration", sourceFormat.c_str(), targetFormat.c_str(), std::to_string(priority).c_str());
      return;
    }
  
  formatConverters[sourceFormat][targetFormat][priority] = converterFunction;

  return;
}

std::vector<std::string> SoapySDR::ConverterRegistry::listTargetFormats(const std::string &sourceFormat)
{
  lateLoadDefaultConverters();

  std::vector<std::string> targets;

  if (formatConverters.count(sourceFormat) == 0)
    return targets;

  for(const auto &it:formatConverters[sourceFormat])
    {
      std::string targetFormat = it.first;
      targets.push_back(targetFormat);
    }
  
  std::sort(targets.begin(), targets.end());
  return targets;
}

std::vector<std::string> SoapySDR::ConverterRegistry::listSourceFormats(const std::string &targetFormat)
{
  lateLoadDefaultConverters();

  std::vector<std::string> sources;

  for(const auto &it:formatConverters)
    {
      std::string sourceFormat = it.first;
      if (formatConverters[sourceFormat].count(targetFormat) > 0)
        sources.push_back(sourceFormat);
    }
  
  std::sort(sources.begin(), sources.end());
  return sources;
}

std::vector<SoapySDR::ConverterRegistry::FunctionPriority> SoapySDR::ConverterRegistry::listPriorities(const std::string &sourceFormat, const std::string &targetFormat)
{
  lateLoadDefaultConverters();

  std::vector<FunctionPriority> priorities;
  
  if (formatConverters.count(sourceFormat) == 0)
    ;
  else if (formatConverters[sourceFormat].count(targetFormat) == 0)
    ;
  else if (formatConverters[sourceFormat][targetFormat].size() == 0)
    ;
  else
    {
      for(const auto &it:formatConverters[sourceFormat][targetFormat])
        {
          FunctionPriority priority = it.first;
          priorities.push_back(priority);
        }
    }
  
  return priorities;
  
}

SoapySDR::ConverterRegistry::ConverterFunction SoapySDR::ConverterRegistry::getFunction(const std::string &sourceFormat, const std::string &targetFormat)
{
  lateLoadDefaultConverters();

  if (formatConverters.count(sourceFormat) == 0)
    {
      throw std::runtime_error("ConverterRegistry::getFunction() conversion source not registered; "
                               "sourceFormat="+sourceFormat+", targetFormat="+targetFormat);
    }
  
  if (formatConverters[sourceFormat].count(targetFormat) == 0)
    {
      throw std::runtime_error("ConverterRegistry::getFunction() conversion target not registered; "
                               "sourceFormat="+sourceFormat+", targetFormat="+targetFormat);
    }

  if (formatConverters[sourceFormat][targetFormat].size() == 0)
    {
      throw std::runtime_error("ConverterRegistry::getFunction() no functions found for registered conversion; "
                               "sourceFormat="+sourceFormat+", targetFormat="+targetFormat);
    }

  return formatConverters[sourceFormat][targetFormat].rbegin()->second;
}

SoapySDR::ConverterRegistry::ConverterFunction SoapySDR::ConverterRegistry::getFunction(const std::string &sourceFormat, const std::string &targetFormat, const FunctionPriority &priority)
{
  lateLoadDefaultConverters();

  if (formatConverters.count(sourceFormat) == 0)
    {
      throw std::runtime_error("ConverterRegistry::getFunction() conversion source not registered; "
                               "sourceFormat="+sourceFormat+", targetFormat="+targetFormat+", priority="+std::to_string(priority));
    }

  if (formatConverters[sourceFormat].count(targetFormat) == 0)
    {
      throw std::runtime_error("ConverterRegistry::getFunction() conversion target not registered; "
                               "sourceFormat="+sourceFormat+", targetFormat="+targetFormat+", priority="+std::to_string(priority));
    }

  if (formatConverters[sourceFormat][targetFormat].count(priority) == 0)
    {
      throw std::runtime_error("ConverterRegistry::getFunction() conversion priority not registered; "
                               "sourceFormat="+sourceFormat+", targetFormat="+targetFormat+", priority="+std::to_string(priority));
    }

  return formatConverters[sourceFormat][targetFormat][priority];
}

std::vector<std::string> SoapySDR::ConverterRegistry::listAvailableSourceFormats(void)
{
    lateLoadDefaultConverters();

    std::vector<std::string> sources;
    for (const auto &it : formatConverters)
    {
        if (std::find(sources.begin(), sources.end(), it.first) == sources.end())
        {
            sources.push_back(it.first);
        }
    }
    std::sort(sources.begin(), sources.end());
    return sources;
}