summaryrefslogtreecommitdiff
path: root/bindings
diff options
context:
space:
mode:
authorJoshua Haberman <jhaberman@gmail.com>2011-09-04 22:40:42 -0700
committerJoshua Haberman <jhaberman@gmail.com>2011-09-04 22:40:42 -0700
commitf226554fa5b64c2a152f39e52fd4a56acc06f527 (patch)
treecb4b42c6797233c5b3590915f6fe43ab346cbcb7 /bindings
parent621c0cdcb5efc4f7c2382031becded018ef0b62b (diff)
Fleshed out C++ def wrappers some.
Diffstat (limited to 'bindings')
-rw-r--r--bindings/cpp/upb/def.hpp105
-rw-r--r--bindings/cpp/upb/upb.hpp6
2 files changed, 110 insertions, 1 deletions
diff --git a/bindings/cpp/upb/def.hpp b/bindings/cpp/upb/def.hpp
index d64625c..edec56a 100644
--- a/bindings/cpp/upb/def.hpp
+++ b/bindings/cpp/upb/def.hpp
@@ -11,21 +11,124 @@
#ifndef UPB_DEF_HPP
#define UPB_DEF_HPP
+#include <algorithm>
+#include <string>
+#include <vector>
#include "upb/def.h"
+#include "upb/upb.hpp"
namespace upb {
+class MessageDef;
+
+class FieldDef : public upb_fielddef {
+ public:
+ static FieldDef* Cast(upb_fielddef *f) { return (FieldDef*)f; }
+ static const FieldDef* Cast(const upb_fielddef *f) { return (FieldDef*)f; }
+
+ static FieldDef* New() { return Cast(upb_fielddef_new()); }
+ FieldDef* Dup() { return Cast(upb_fielddef_dup(this)); }
+
+ // Read accessors -- may be called at any time.
+ uint8_t type() const { return upb_fielddef_type(this); }
+ uint8_t label() const { return upb_fielddef_label(this); }
+ int32_t number() const { return upb_fielddef_number(this); }
+ std::string name() const { return std::string(upb_fielddef_name(this)); }
+ Value default_() const { return upb_fielddef_default(this); }
+ Value bound_value() const { return upb_fielddef_fval(this); }
+
+ MessageDef* message() { return (MessageDef*)upb_fielddef_msgdef(this); }
+ const MessageDef* message() const { return (MessageDef*)upb_fielddef_msgdef(this); }
+ //Def* subdef() { return upb_fielddef_subdef(this); }
+ //const Def* subdef() { return upb_fielddef_subdef(this); }
+
+ // Returns true if this FieldDef is finalized
+ bool IsFinalized() const { return upb_fielddef_finalized(this); }
+ struct _upb_accessor_vtbl *accessor() const {
+ return upb_fielddef_accessor(this);
+ }
+ std::string type_name() const {
+ return std::string(upb_fielddef_typename(this));
+ }
+
+ // Write accessors -- may not be called once the FieldDef is finalized.
+
+ private:
+ FieldDef();
+ ~FieldDef();
+};
+
class MessageDef : public upb_msgdef {
public:
// Converting from C types to C++ wrapper types.
static MessageDef* Cast(upb_msgdef *md) { return (MessageDef*)md; }
static const MessageDef* Cast(const upb_msgdef *md) {
- return (const MessageDef*)md;
+ return (MessageDef*)md;
}
+ static MessageDef* New() { return Cast(upb_msgdef_new()); }
+ MessageDef* Dup() { return Cast(upb_msgdef_dup(this)); }
+
void Ref() const { upb_msgdef_ref(this); }
void Unref() const { upb_msgdef_unref(this); }
+ // Read accessors -- may be called at any time.
+
+ // The total size of in-memory messages created with this MessageDef.
+ uint16_t instance_size() const { return upb_msgdef_size(this); }
+
+ // The number of "hasbit" bytes in a message instance.
+ uint8_t hasbit_bytes() const { return upb_msgdef_hasbit_bytes(this); }
+
+ uint32_t extension_start() const { return upb_msgdef_extstart(this); }
+ uint32_t extension_end() const { return upb_msgdef_extend(this); }
+
+ // Write accessors. May only be called before the msgdef is in a symtab.
+
+ void set_instance_size(uint16_t size) { upb_msgdef_setsize(this, size); }
+ void set_hasbit_bytes(uint16_t size) { upb_msgdef_setsize(this, size); }
+ bool SetExtensionRange(uint32_t start, uint32_t end) {
+ return upb_msgdef_setextrange(this, start, end);
+ }
+
+ // Adds a set of fields (upb_fielddef objects) to a msgdef. Caller retains
+ // its ref on the fielddef. May only be done before the msgdef is in a
+ // symtab (requires upb_def_ismutable(m) for the msgdef). The fielddef's
+ // name and number must be set, and the message may not already contain any
+ // field with this name or number, and this fielddef may not be part of
+ // another message, otherwise false is returned and no action is performed.
+ bool AddFields(FieldDef** f, int n) {
+ return upb_msgdef_addfields(this, (upb_fielddef**)f, n);
+ }
+ bool AddFields(const std::vector<FieldDef*>& fields) {
+ FieldDef *arr[fields.size()];
+ std::copy(fields.begin(), fields.end(), arr);
+ return AddFields(arr, fields.size());
+ }
+
+ // Lookup fields by name or number, returning NULL if no such field exists.
+ FieldDef* FindFieldByName(const char *name) {
+ return FieldDef::Cast(upb_msgdef_ntof(this, name));
+ }
+ FieldDef* FindFieldByName(const std::string& name) {
+ return FieldDef::Cast(upb_msgdef_ntof(this, name.c_str()));
+ }
+ FieldDef* FindFieldByNumber(uint32_t num) {
+ return FieldDef::Cast(upb_msgdef_itof(this, num));
+ }
+
+ const FieldDef* FindFieldByName(const char *name) const {
+ return FindFieldByName(name);
+ }
+ const FieldDef* FindFieldByName(const std::string& name) const {
+ return FindFieldByName(name);
+ }
+ const FieldDef* FindFieldByNumber(uint32_t num) const {
+ return FindFieldByNumber(num);
+ }
+
+ // TODO: iteration over fields.
+
private:
MessageDef();
~MessageDef();
diff --git a/bindings/cpp/upb/upb.hpp b/bindings/cpp/upb/upb.hpp
index 460fb43..4fb337d 100644
--- a/bindings/cpp/upb/upb.hpp
+++ b/bindings/cpp/upb/upb.hpp
@@ -20,6 +20,12 @@ class Status : public upb_status {
const char *GetString() const { return upb_status_getstr(this); }
};
+class Value : public upb_value {
+ public:
+ Value(const upb_value& val) { *this = val; }
+ Value() {}
+};
+
INLINE std::ostream& operator<<(std::ostream& out, const Status& status) {
out << status.GetString();
return out;
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback