zmqpp  4.1.2
C++ bindings for 0mq (libzmq)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
context.hpp
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5  *
6  * This file is part of zmqpp.
7  * Copyright (c) 2011-2015 Contributors as noted in the AUTHORS file.
8  */
9 
17 #ifndef ZMQPP_CONTEXT_HPP_
18 #define ZMQPP_CONTEXT_HPP_
19 
20 #include <cassert>
21 
22 #include <zmq.h>
23 
24 #include "compatibility.hpp"
25 #include "exception.hpp"
26 #if (ZMQ_VERSION_MAJOR > 3) || ((ZMQ_VERSION_MAJOR == 3) && (ZMQ_VERSION_MINOR >= 2))
27 #include "context_options.hpp"
28 #endif
29 
30 namespace zmqpp
31 {
32 
46 class context
47 {
48 public:
49 
50 #if (ZMQ_VERSION_MAJOR < 3) || ((ZMQ_VERSION_MAJOR == 3) && (ZMQ_VERSION_MINOR < 2))
51 
63  context(int const& threads = 1)
64 #else
65 
73 #endif
74  : _context(nullptr)
75  {
76 #if (ZMQ_VERSION_MAJOR < 3) || ((ZMQ_VERSION_MAJOR == 3) && (ZMQ_VERSION_MINOR < 2))
77  _context = zmq_init(threads);
78 #else
79  _context = zmq_ctx_new();
80 #endif
81 
82  if (nullptr == _context)
83  {
84  throw zmq_internal_exception();
85  }
86  }
87 
97  {
98  if (nullptr != _context)
99  {
100  terminate();
101  }
102  }
103 
112  : _context(source._context)
113  {
114  source._context = nullptr;
115  }
116 
125  {
126  std::swap( _context, source._context );
127  return *this;
128  }
129 
138  void terminate();
139 
140 #if (ZMQ_VERSION_MAJOR > 3) || ((ZMQ_VERSION_MAJOR == 3) && (ZMQ_VERSION_MINOR >= 2))
141 
147  void set(context_option const option, int const value);
148 
155  int get(context_option const option);
156 #endif
157 
168  operator bool() const NOEXCEPT
169  {
170  return nullptr != _context;
171  }
172 
178  operator void*() const NOEXCEPT
179  {
180  return _context;
181  }
182 
183 private:
184  void* _context;
185 
186  // No copy - private and not implemented
188  context& operator=(context const&) NOEXCEPT ZMQPP_EXPLICITLY_DELETED;
189 };
190 
191 }
192 
193 #endif /* ZMQPP_CONTEXT_HPP_ */
void set(context_option const option, int const value)
Set the value of an option in the underlaying zmq context.
Definition: context.cpp:38
void terminate()
Terminate the current context.
Definition: context.cpp:22
context()
Initialise the 0mq context.
Definition: context.hpp:72
#define ZMQPP_EXPLICITLY_DELETED
Definition: compatibility.hpp:100
C++ wrapper around zmq.
Definition: actor.cpp:29
ZMQPP_COMPARABLE_ENUM context_option
possible Context options in zmq
Definition: context_options.hpp:28
Represents internal zmq errors.
Definition: exception.hpp:100
#define NOEXCEPT
Definition: compatibility.hpp:104
The context class represents internal zmq context and io threads.
Definition: context.hpp:46
~context() NOEXCEPT
Closes the 0mq context.
Definition: context.hpp:96
void * _context
Definition: context.hpp:184
context & operator=(context &&source) NOEXCEPT
Move supporting operator.
Definition: context.hpp:124
context(context &&source) NOEXCEPT
Move supporting constructor.
Definition: context.hpp:111