Skip to content
Snippets Groups Projects

Minimal c interface

Merged Timm Fitschen requested to merge f-extern-c into dev
All threads resolved!
6 files
+ 45
45
Compare changes
  • Side-by-side
  • Inline
Files
6
+ 221
0
/*
* This file is a part of the CaosDB Project.
*
* Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com>
* Copyright (C) 2021 IndiScale GmbH <info@indiscale.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#ifndef CAOSDB_CONFIGURATION_H
#define CAOSDB_CONFIGURATION_H
#include <memory> // for unique_ptr
#include <string> // for string
#include "boost/filesystem/operations.hpp" // for exists
#include "boost/filesystem/path.hpp" // for path
#include "boost/json/object.hpp" // for object
#include "boost/json/value.hpp" // for value
#include "boost/json/value_ref.hpp" // for array, object
#include "caosdb/authentication.h" // for Authenticator, PlainPassw...
#include "caosdb/connection.h" // for ConnectionConfiguration, Certifi...
#include "caosdb/exceptions.h" // for ConfigurationError
#include "caosdb/utility.h" // for load_json_file
namespace caosdb::configuration {
using boost::filesystem::exists;
using boost::filesystem::path;
using boost::json::array;
using boost::json::object;
using boost::json::value;
using caosdb::authentication::Authenticator;
using caosdb::authentication::PlainPasswordAuthenticator;
using caosdb::connection::CertificateProvider;
using caosdb::connection::ConnectionConfiguration;
using caosdb::connection::ConnectionManager;
using caosdb::connection::InsecureConnectionConfiguration;
using caosdb::connection::PemFileCertificateProvider;
using caosdb::connection::TlsConnectionConfiguration;
using caosdb::exceptions::ConfigurationError;
using caosdb::utility::load_json_file;
/**
* Helper class (no state, just member functions) which should only be used by
* the ConfigurationManager to construct Connection instances from the stored
* configuration.
*/
class ConnectionConfigurationHelper {
public:
friend class ConfigurationManager;
private:
/**
* @param from - a single connection configuration.
*/
inline auto CreateCertificateProvider(const object &from) const
-> std::unique_ptr<CertificateProvider>;
/**
* @param from - a single connection configuration.
*/
auto CreateAuthenticator(const object &from) const
-> std::unique_ptr<Authenticator>;
/**
* @param from - a single connection configuration.
*/
auto
CreateConnectionConfiguration(const bool tls, const std::string &host,
const int port,
const CertificateProvider *certificate_provider,
const Authenticator *authenticator) const
-> std::unique_ptr<ConnectionConfiguration>;
/**
* @param from - a single connection configuration.
*/
auto IsTls(const object &from) const -> bool;
/**
* @param from - a single connection configuration.
*/
auto CreateConnectionConfiguration(const object &from) const
-> std::unique_ptr<ConnectionConfiguration>;
};
/**
* Reads the configuration file and keeps the configuration. Singleton.
*
* Currently, this class can only read a single configuration file. No merging
* or overwriting is supported.
*/
class ConfigurationManager {
public:
static ConfigurationManager &GetInstance() {
static ConfigurationManager instance;
return instance;
};
/**
* See mReset.
*/
inline static auto Reset() -> void { GetInstance().mReset(); }
/**
* See mClear.
*/
inline static auto Clear() -> void { GetInstance().mClear(); }
/**
* See mLoadSingleJSONConfiguration.
*/
inline static auto LoadSingleJSONConfiguration(const path &json_file)
-> void {
GetInstance().mLoadSingleJSONConfiguration(json_file);
}
/**
* See mGetConnectionConfiguration.
*/
inline static auto GetConnectionConfiguration(const std::string &name)
-> std::unique_ptr<ConnectionConfiguration> {
return GetInstance().mGetConnectionConfiguration(name);
}
/**
* Return the ConnectionConfiguration for the default connection.
*/
inline static auto GetDefaultConnectionConfiguration()
-> std::unique_ptr<ConnectionConfiguration> {
return GetInstance().mGetConnectionConfiguration(
GetInstance().mGetDefaultConnectionName());
}
/**
* See mGetDefaultConnectionName.
*/
inline static auto GetDefaultConnectionName() -> std::string {
return GetInstance().mGetDefaultConnectionName();
}
ConfigurationManager(ConfigurationManager const &) = delete;
void operator=(ConfigurationManager const &) = delete;
private:
value json_configuration;
ConnectionConfigurationHelper connection_configuration_helper;
inline ConfigurationManager() { InitializeDefaults(); };
/**
* Initialize this ConfigurationManager with the defaults.
*
* Currently, this means, that the ConfigurationManager attempts to load the
* first existing file from the LIBCAOSDB_CONFIGURATION_FILES_PRECEDENCE list
* of file locations.
*/
auto InitializeDefaults() -> void;
/**
* Return a json object representing the current configuration.
*/
auto GetConfiguration() const -> const object &;
/**
* Return the connection configurations.
*/
auto GetConnections() const -> const object &;
/**
* Return the configuration for the connection with the given name (as a json
* object).
*/
auto GetConnection(const std::string &name) const -> const object &;
/**
* Reset this ConfigurationManager.
*
* The current configuration is deleted and a new configuration is being
* loaded via InitializeDefaults.
*/
auto mReset() -> void;
/**
* Clear this ConfigurationManager.
*
* Afterwards, this ConfigurationManager is uninitilized.
*
* In contrast to mReset, this method only deletes the current configuration
* but does not load a new one via InitializeDefaults.
*/
auto mClear() -> void;
/**
* Load a configuration from a json file.
*/
auto mLoadSingleJSONConfiguration(const path &json_file) -> void;
/**
* Return the ConnectionConfiguration for the connection of the given name.
*/
auto mGetConnectionConfiguration(const std::string &name) const
-> std::unique_ptr<ConnectionConfiguration>;
/**
* Return the ConnectionConfiguration for the default connection.
*/
auto mGetDefaultConnectionName() const -> std::string;
};
} // namespace caosdb::configuration
#endif
Loading