.. _cppjava:
================================
Working with Phenopackets in C++
================================
Here we provide some guidance on how to work with Phenopackets in C++.
Generating the C++ files
~~~~~~~~~~~~~~~~~~~~~~~~
The maven build generates Java, C++, and Python code that can be directly used in other
projects. Therefore, if you have maven set up on your machine, the easiest way to generate
the C++ files is ::
$ mvn compile
$ mvn package
This will generate four files in the following location. ::
$ ls target/generated-sources/protobuf/cpp/
base.pb.cc phenopackets.pb.cc
base.pb.h phenopackets.pb.h
The other option is to use Google's ``protoc`` tool to generate the C++ files (The tool can be obtained
from the `Protobuf website `_ Install the tool
using commands appropriate to your system). The following commands
will generate identical files in a new directory called ``gen``. ::
$ mkdir gen
$ protoc \
--proto_path=src/main/proto/ \
--cpp_out=gen/ \
src/main/proto/phenopackets.proto src/main/proto/base.proto
The ``protoc`` command specifies the directory where the protobuf files are located (`--proto_path`), the
location of the directory to which the corresponding C++ files are to be written, and then passes the two
protobuf files.
Compiling and building Phenopackets
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The phenopacket code can be compiled and built using standard tools. Here we present a small example of
a C++ program that reads in a phenopacket JSON file from the command line and prints our some of the
information contained in it to the shell. The classes defined by the phenopacket are located within
namespace declarations that mirror the Java package names, and thus are extremly unlikely to
collide with other C++ identifiers. ::
#include
#include
#include
#include
#include
#include
#include "phenopackets.pb.h"
using namespace std;
int main(int argc, char ** argv) {
// check that user has passed a file.
if (argc!=2) {
cerr << "usage: ./phenopacket_demo phenopacket-file.json\n";
exit(EXIT_FAILURE);
}
string fileName=argv[1];
GOOGLE_PROTOBUF_VERIFY_VERSION;
stringstream sstr;
ifstream inFile;
inFile.open(fileName);
if (! inFile.good()) {
cerr << "Could not open Phenopacket file at " << fileName <<"\n";
return EXIT_FAILURE;
}
sstr << inFile.rdbuf();
string JSONstring = sstr.str();
::google::protobuf::util::JsonParseOptions options;
::org::phenopackets::schema::v1::Phenopacket phenopacket;
::google::protobuf::util::JsonStringToMessage(JSONstring,&phenopacket,options);
cout << "\n::: Reading Phenopacket at: " << fileName << " ::::\n\n";
cout << "\tsubject.id: "<`_.
phenotools
~~~~~~~~~~
A more complete C++ implementation that performs Q/C is being developed as
`phenotools `_.