Falcon
Loading...
Searching...
No Matches
context.hpp
1// ---------------------------------------------------------------------
2// This file is part of falcon-core.
3//
4// Copyright (C) 2015, 2016, 2017 Neuro-Electronics Research Flanders
5//
6// Falcon-server is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Falcon-server is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with falcon-core. If not, see <http://www.gnu.org/licenses/>.
18// ---------------------------------------------------------------------
19
20#pragma once
21
22#include <atomic>
23#include <map>
24#include <string>
25#include <zmq.hpp>
26#include "utilities/string.hpp"
27
28class StorageContext {
29 public:
30 StorageContext(std::string default_context = "") : default_context_(default_context) {}
31
32 std::map<std::string, std::string> storage_map() const { return storage_map_; }
33
34 std::string default_context() const { return default_context_; }
35 void set_default_context(std::string c) { default_context_ = c; }
36
37 std::string storage_context(const std::string& c) { return storage_map_[c]; }
38
39 std::string resolve_path(const std::string& p) const {
40 return resolve_server_path(p, storage_map_, default_context_);
41 }
42 std::string resolve_path(const std::string& p, std::string default_context) const {
43 return resolve_server_path(p, storage_map_, default_context);
44 }
45
46 protected:
47 void add_storage_context(std::string key, std::string value) { storage_map_[key] = value; }
48
49 protected:
50 std::string default_context_;
51 std::map<std::string, std::string> storage_map_;
52};
53
54class GlobalContext : public StorageContext {
55 public:
56 GlobalContext(bool test_flag, const std::map<std::string, std::string>& uri)
57 : StorageContext(""), default_test_flag_(test_flag) {
58 zmq_context_ = zmq::context_t(1);
59 for (auto& it : uri) {
60 add_storage_context(it.first, it.second);
61 }
62 }
63
64 zmq::context_t& zmq() { return zmq_context_; }
65
66 bool test() const { return default_test_flag_.load(); }
67 void set_test(bool value) { default_test_flag_ = value; }
68
69 private:
70 zmq::context_t zmq_context_;
71 std::atomic<bool> default_test_flag_;
72};