diff options
-rw-r--r-- | bindings/cpp/upb/def.hpp | 49 | ||||
-rw-r--r-- | bindings/cpp/upb/pb/glue.hpp | 23 | ||||
-rw-r--r-- | bindings/cpp/upb/upb.hpp | 30 | ||||
-rw-r--r-- | tests/test_cpp.cc | 22 |
4 files changed, 124 insertions, 0 deletions
diff --git a/bindings/cpp/upb/def.hpp b/bindings/cpp/upb/def.hpp new file mode 100644 index 0000000..6500243 --- /dev/null +++ b/bindings/cpp/upb/def.hpp @@ -0,0 +1,49 @@ +/* + * upb - a minimalist implementation of protocol buffers. + * + * Copyright (c) 2011 Google Inc. See LICENSE for details. + * Author: Josh Haberman <jhaberman@gmail.com> + * + * The set of upb::*Def classes and upb::SymbolTable allow for defining and + * manipulating schema information (as defined in .proto files). + */ + +#ifndef UPB_DEF_HPP +#define UPB_DEF_HPP + +#include "upb/def.h" + +namespace upb { + +class MessageDef : public upb_msgdef { + public: + static MessageDef* Cast(upb_msgdef *md) { return (MessageDef*)md; } + + void Unref() { return upb_msgdef_unref(this); } + + private: + MessageDef(); + ~MessageDef(); +}; + +class SymbolTable : public upb_symtab { + public: + static SymbolTable* Cast(upb_symtab *s) { return (SymbolTable*)s; } + static SymbolTable* New() { return Cast(upb_symtab_new()); } + + void Unref() { return upb_symtab_unref(this); } + + // If the given name refers to a message in this symbol table, returns a new + // ref to that MessageDef object, otherwise returns NULL. + MessageDef* LookupMessage(const char *name) { + return MessageDef::Cast(upb_symtab_lookupmsg(this, name)); + } + + private: + SymbolTable(); + ~SymbolTable(); +}; + +} // namespace upb + +#endif diff --git a/bindings/cpp/upb/pb/glue.hpp b/bindings/cpp/upb/pb/glue.hpp new file mode 100644 index 0000000..be072a7 --- /dev/null +++ b/bindings/cpp/upb/pb/glue.hpp @@ -0,0 +1,23 @@ +/* + * upb - a minimalist implementation of protocol buffers. + * + * Copyright (c) 2011 Google Inc. See LICENSE for details. + * Author: Josh Haberman <jhaberman@gmail.com> + */ + +#ifndef UPB_PB_GLUE_HPP +#define UPB_PB_GLUE_HPP + +#include "upb/upb.hpp" +#include "upb/pb/glue.h" + +namespace upb { + +bool LoadDescriptorFileIntoSymtab(SymbolTable* s, const char *fname, + Status* status) { + return upb_load_descriptor_file_into_symtab(s, fname, status); +} + +} // namespace upb + +#endif diff --git a/bindings/cpp/upb/upb.hpp b/bindings/cpp/upb/upb.hpp new file mode 100644 index 0000000..460fb43 --- /dev/null +++ b/bindings/cpp/upb/upb.hpp @@ -0,0 +1,30 @@ +/* + * upb - a minimalist implementation of protocol buffers. + * + * Copyright (c) 2011 Google Inc. See LICENSE for details. + * Author: Josh Haberman <jhaberman@gmail.com> + */ + +#ifndef UPB_HPP +#define UPB_HPP + +#include "upb/upb.h" + +namespace upb { + +class Status : public upb_status { + public: + Status() { upb_status_init(this); } + ~Status() { upb_status_uninit(this); } + + const char *GetString() const { return upb_status_getstr(this); } +}; + +INLINE std::ostream& operator<<(std::ostream& out, const Status& status) { + out << status.GetString(); + return out; +} + +} // namespace upb + +#endif diff --git a/tests/test_cpp.cc b/tests/test_cpp.cc new file mode 100644 index 0000000..ac5a3be --- /dev/null +++ b/tests/test_cpp.cc @@ -0,0 +1,22 @@ + +#include <iostream> +#include "upb/def.hpp" +#include "upb/pb/glue.hpp" + +static void TestSymbolTable() { + upb::SymbolTable *s = upb::SymbolTable::New(); + upb::Status status; + if (!upb::LoadDescriptorFileIntoSymtab(s, "tests/test.proto.pb", &status)) { + std::cerr << "Couldn't load descriptor: " << status; + exit(1); + } + upb::MessageDef *md = s->LookupMessage("A"); + assert(md); + + s->Unref(); + md->Unref(); +} + +int main() { + TestSymbolTable(); +} |