File: Registry.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 (103 lines) | stat: -rw-r--r-- 2,990 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
// Copyright (c) 2014-2018 Josh Blum
// SPDX-License-Identifier: BSL-1.0

#include <SoapySDR/Registry.hpp>
#include <mutex>

/***********************************************************************
 * Function table holds all registration entries by name
 **********************************************************************/
struct FunctionsEntry
{
    std::string modulePath;
    SoapySDR::FindFunction find;
    SoapySDR::MakeFunction make;
};

typedef std::map<std::string, FunctionsEntry> FunctionTable;

static FunctionTable &getFunctionTable(void)
{
    static FunctionTable table;
    return table;
}

static std::recursive_mutex &getRegistryMutex(void)
{
    static std::recursive_mutex mutex;
    return mutex;
}

/***********************************************************************
 * Module loader shared data structures
 **********************************************************************/
std::string &getModuleLoading(void);

std::map<std::string, SoapySDR::Kwargs> &getLoaderResults(void);

/***********************************************************************
 * Registry entry-point implementation
 **********************************************************************/
SoapySDR::Registry::Registry(const std::string &name, const FindFunction &find, const MakeFunction &make, const std::string &abi)
{
    std::lock_guard<std::recursive_mutex> lock(getRegistryMutex());

    //create an entry for the loader result
    std::string &errorMsg = getLoaderResults()[getModuleLoading()][name];

    //abi check
    if (abi != SOAPY_SDR_ABI_VERSION)
    {
        errorMsg = name + " failed ABI check: Library ABI=" SOAPY_SDR_ABI_VERSION ", Module ABI="+abi;
        return;
    }

    //duplicate check
    if (getFunctionTable().count(name) != 0)
    {
        errorMsg = "duplicate entry for " + name + " ("+getFunctionTable()[name].modulePath + ")";
        return;
    }

    //register functions
    FunctionsEntry entry;
    entry.modulePath = getModuleLoading();
    entry.find = find;
    entry.make = make;
    getFunctionTable()[name] = entry;
    _name = name;
}

SoapySDR::Registry::~Registry(void)
{
    //erase entry
    if (_name.empty()) return;
    getFunctionTable().erase(_name);
}

/***********************************************************************
 * Registry access API
 **********************************************************************/
SoapySDR::FindFunctions SoapySDR::Registry::listFindFunctions(void)
{
    std::lock_guard<std::recursive_mutex> lock(getRegistryMutex());

    FindFunctions functions;
    for (const auto &it : getFunctionTable())
    {
        functions[it.first] = it.second.find;
    }
    return functions;
}

SoapySDR::MakeFunctions SoapySDR::Registry::listMakeFunctions(void)
{
    std::lock_guard<std::recursive_mutex> lock(getRegistryMutex());

    MakeFunctions functions;
    for (const auto &it : getFunctionTable())
    {
        functions[it.first] = it.second.make;
    }
    return functions;
}