Files
spark/src/lib/config/config.cpp
T

133 lines
3.6 KiB
C++

#ifndef CONFIG_H_
#define CONFIG_H_
#include "config.h"
#include "../os/os.h"
#include "ConfigurationState.h"
#include "lib/collections/Contains.h"
#include <google/protobuf/descriptor.h>
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/dynamic_message.h>
#include <google/protobuf/text_format.h>
#include <string>
//#include <boost/thread.hpp>
namespace proto = google::protobuf;
namespace bts {
namespace config {
using std::string;
internal::ConfigurationState config_state;
//////////////////////////////////////////
// Public API
//////////////////////////////////////////
extern "C" BTS_EXPORT void initialize(const char *config_source) {
config_state.initialize_settings(string{config_source});
}
extern "C" BTS_EXPORT bool initialize_nothrow(
const char *config_source, char *error_message, size_t len) {
try {
initialize(config_source);
} catch (std::exception &e) {
std::string msg = e.what();
if (msg.size() < len) {
os::strncpy(error_message, msg.c_str(), len);
}
return false;
}
return true;
}
// extern "C"
const proto::Message &
get_message(const google::protobuf::Descriptor *descriptor) {
return config_state.get_message(descriptor);
}
std::vector<const google::protobuf::FieldDescriptor *> get_fields(
const std::vector<std::string> keys,
const std::vector<std::string> excluded_keys) {
return config_state.get_fields(keys, excluded_keys);
}
std::vector<const google::protobuf::Message *> get_messages(
const std::vector<std::string> keys,
const std::vector<std::string> excluded_keys,
std::vector<std::string> *unknown_keys) {
return config_state.get_messages(keys, excluded_keys, unknown_keys);
}
namespace interop {
//////////////////////////////////////////////////
// Functions used for interop to other languages
/////////////////////////////////////////////////
std::string getbuffer_serialized_buffer;
extern "C" BTS_EXPORT const char *
get_buffer(const char *key_name, int &out_length) {
google::protobuf::DescriptorPool const *pool =
google::protobuf::DescriptorPool::generated_pool();
const proto::Descriptor *message_desc =
pool->FindMessageTypeByName(key_name);
const proto::Message &message = get_message(message_desc);
getbuffer_serialized_buffer.clear();
message.SerializePartialToString(&getbuffer_serialized_buffer);
out_length = getbuffer_serialized_buffer.size();
return getbuffer_serialized_buffer.c_str();
}
extern "C" BTS_EXPORT const proto::Message *get_named(const char *key_name) {
return config_state.try_get_message_or_default(key_name);
}
BTS_EXPORT std::string get_macro_string(const char *name) {
return config_state.get_macro_string(name);
}
} // namespace interop
namespace internal {
//////////////////////////////////////////
// Functions used in testing
//////////////////////////////////////////
extern "C" BTS_EXPORT void initialize_for_test(const char *config) {
initialize_for_test(std::string(config), false);
}
void initialize_for_test(const std::string &values, bool print_errors) {
config_state.reset();
config_state.load_string(values, print_errors);
}
extern "C" BTS_EXPORT void reset() { config_state.reset(); }
extern "C" BTS_EXPORT bool check_rules(char *error_msg, size_t len) {
try {
config_state.check_rules();
} catch (std::exception &e) {
std::string msg = e.what();
if (msg.size() < len) {
os::strncpy(error_msg, msg.c_str(), len);
}
return false;
}
return true;
}
} // namespace internal
} // namespace config
} // namespace bts
#endif