Skip to content

Commit 2c0e48d

Browse files
committed
added cppmpl namespace
1 parent f723ef9 commit 2c0e48d

File tree

9 files changed

+75
-34
lines changed

9 files changed

+75
-34
lines changed

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ set (CMAKE_EXPORT_COMPILE_COMMANDS 1)
1818

1919
## Compile and create a library. STATIC is default unless BUILD_SHARED_LIBS
2020
## is on.
21-
add_library (cpp_plot
22-
src/cpp_plot.cc
21+
add_library (cpp_mpl
22+
src/cpp_mpl.cc
2323
src/RequestSink.cc
2424
src/ipython_protocol.cc)
2525

26-
set (EXTRA_LIBS ${EXTRA_LIBS} cpp_plot)
26+
set (EXTRA_LIBS ${EXTRA_LIBS} cpp_mpl)
2727

2828
## Libraries to link with
2929
target_link_libraries (${EXAMPLE_BIN}

README.md

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
* [Usage](#usage)
55
* [Prereqs](#prereqs)
66
* [Building](#building)
7-
* [Example](#example)
7+
* [Running the Example](#running-the-example)
88

99

1010
# About
1111

12-
An easy-to-use library for simple plotting from C++ via a ZeroMQ bridge to
13-
an [IPython](http://ipython.org/) kernel.
12+
An easy-to-use **C++11** library for simple plotting from C++ via a ZeroMQ
13+
bridge to an [IPython](http://ipython.org/) kernel.
1414

1515
It provides the ability to send [NumPy](http://www.numpy.org/) array
1616
compatible data to an IPython kernel session as well as execute arbitrary
@@ -43,29 +43,39 @@ Here we create some 1D data and plot it. The numpy.array "MyData" will be
4343
available for working with in the IPython session, even after the C++ program
4444
finishes.
4545

46+
All library code lives in the <tt>cppmpl</tt> namespace.
47+
4648
```c++
47-
CppMatplotlib mpl{"/path/to/kernel-NNN.json"};
48-
mpl.Connect();
49-
50-
// Create a nice curve
51-
std::vector<NumpyArray::dtype> raw_data;
52-
double x = 0.0;
53-
while (x < 3.14159 * 4) {
54-
raw_data.push_back(std::sin(x));
55-
x += 0.05;
49+
#include "cpp_mpl.hpp"
50+
51+
int main() {
52+
// ...
53+
54+
cppmpl::CppMatplotlib mpl{"/path/to/kernel-NNN.json"};
55+
mpl.Connect();
56+
57+
// Create a nice curve
58+
std::vector<cppmpl::NumpyArray::dtype> raw_data;
59+
double x = 0.0;
60+
while (x < 3.14159 * 4) {
61+
raw_data.push_back(std::sin(x));
62+
x += 0.05;
63+
}
64+
65+
// Send it to IPython for plotting
66+
cppmpl::NumpyArray data("MyData", raw_data);
67+
mpl.SendData(data);
68+
mpl.RunCode("plot(MyData)\n"
69+
"title('f(x) = sin(x)')\n"
70+
"xlabel('x')\n"
71+
"ylabel('f(x)')\n");
72+
73+
// NOTE: if you want to store the python in an external file, use the
74+
// convenience function LoadFile("my_code.py"), as in,
75+
// mpl.RunCode(cppmpl::LoadFile("plotting_code.py"));
76+
77+
// ...
5678
}
57-
58-
// Send it to IPython for plotting
59-
NumpyArray data("MyData", raw_data);
60-
mpl.SendData(data);
61-
mpl.RunCode("plot(MyData)\n"
62-
"title('f(x) = sin(x)')\n"
63-
"xlabel('x')\n"
64-
"ylabel('f(x)')\n");
65-
66-
// NOTE: if you want to store the python in an external file, use the
67-
// convenience function LoadFile("my_code.py"), as in,
68-
// mpl.RunCode(LoadFile("plotting_code.py"));
6979
```
7080

7181
And the result is ![Screenshot](screenshot.png?raw=true)
@@ -92,6 +102,17 @@ In [84]: print MyData[9]
92102
[ 1.73986214]
93103
```
94104

105+
## Compiling / Linking
106+
107+
When linking against <tt>cpp_plot.{a,so}</tt>, you also need to link against
108+
libzmq, libjsoncpp, libuuid, and libcripto:
109+
110+
```
111+
$ g++ my_prog.cc -std=c++11 /path/to/libcpp_plot.a -ljsoncpp -lzmq -luuid -lcrypto
112+
```
113+
114+
Or just modify CMakeFiles.txt...
115+
95116

96117
# Prereqs
97118

src/RequestSink.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include "RequestSink.hpp"
1010

11+
namespace cppmpl {
12+
1113
RequestSink::RequestSink(const std::string &url) :
1214
context_{1},
1315
socket_{context_, ZMQ_REQ},
@@ -42,3 +44,4 @@ bool RequestSink::Connect(void) {
4244
return true;
4345
}
4446

47+
}

src/RequestSink.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include <zmq.hpp>
1313

14+
namespace cppmpl {
15+
1416
//======================================================================
1517
/** \brief This class wraps a ZeroMQ request-response socket connection.
1618
*
@@ -63,3 +65,5 @@ class RequestSink {
6365
const std::string url_;
6466
bool connected_;
6567
};
68+
69+
}

src/cpp_plot.cc renamed to src/cpp_mpl.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313

1414
#include <zmq.hpp>
1515

16-
#include "cpp_plot.hpp"
16+
#include "cpp_mpl.hpp"
1717

1818
#include "ipython_protocol.hpp"
1919
#include "RequestSink.hpp"
2020

21+
namespace cppmpl {
22+
2123
// Names of the python variables
2224
static const std::string THREAD_VAR_NAME{"cpp_ipython_listener_thread"};
2325
static const std::string PORT_VAR_NAME{"cpp_ipython_listener_thread_port"};
@@ -118,3 +120,5 @@ bool CppMatplotlib::SendData(const NumpyArray &data) {
118120
void CppMatplotlib::RunCode(const std::string &code) {
119121
upSession_->Shell().RunCode(code);
120122
}
123+
124+
} // namespace

src/cpp_plot.hpp renamed to src/cpp_mpl.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
#include <string>
1313
#include <vector>
1414

15+
namespace cppmpl {
16+
1517
// Forward declarations
1618
struct IPyKernelConfig;
1719
class IPythonSession;
1820
class RequestSink;
1921

20-
2122
// Reads an entire file into a string
2223
//--------------------------------------------------
2324
/** \brief Reads an entire file into a string. Make sure you have enough
@@ -238,3 +239,5 @@ class CppMatplotlib {
238239
std::unique_ptr<RequestSink> upData_conn_;
239240
std::unique_ptr<IPythonSession> upSession_;
240241
};
242+
243+
} // namespace

src/ipython_protocol.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include "ipython_protocol.hpp"
1313

14+
namespace cppmpl {
15+
1416
/// Delimeter used by the iPython messaging protocol to separate ZMQ
1517
/// identities from message data.
1618
static const std::string DELIM{"<IDS|MSG>"};
@@ -288,4 +290,4 @@ void IPythonSession::Connect (void) {
288290
shell_connection_.Connect();
289291
}
290292

291-
293+
} // namespace

src/ipython_protocol.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ extern "C" {
2424
#include <openssl/hmac.h>
2525
#include <zmq.hpp>
2626

27+
namespace cppmpl {
28+
2729
// forward declarations
2830
struct IPyKernelConfig;
2931
struct IPythonMessage;
@@ -358,3 +360,5 @@ class IPythonSession {
358360
zmq::context_t zmq_context_;
359361
ShellConnection shell_connection_;
360362
};
363+
364+
} // namespace

src/main.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <string>
1212
#include <vector>
1313

14-
#include "cpp_plot.hpp"
14+
#include "cpp_mpl.hpp"
1515

1616
int main(int argc, char **argv) {
1717
if (argc < 2) {
@@ -20,18 +20,18 @@ int main(int argc, char **argv) {
2020
exit(-1);
2121
}
2222

23-
CppMatplotlib mpl{argv[1]};
23+
cppmpl::CppMatplotlib mpl{argv[1]};
2424
mpl.Connect();
2525

26-
std::vector<NumpyArray::dtype> raw_data;
26+
std::vector<cppmpl::NumpyArray::dtype> raw_data;
2727

2828
double x = 0.0;
2929
while (x < 3.14159 * 4) {
3030
raw_data.push_back(std::sin(x));
3131
x += 0.05;
3232
}
3333

34-
NumpyArray data("A", raw_data);
34+
cppmpl::NumpyArray data("A", raw_data);
3535
mpl.SendData(data);
3636
mpl.RunCode("plot(A)\n"
3737
"title('f(x) = sin(x)')\n"

0 commit comments

Comments
 (0)