summaryrefslogtreecommitdiffstats
path: root/staging_vespalib
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2016-11-28 13:58:51 +0100
committerHenning Baldersheim <balder@yahoo-inc.com>2016-12-12 02:55:39 +0100
commitaf3f0048f62e84e36bee3a69396d0e26894a51f0 (patch)
tree95b4a6beb3dfa5f165818753dd80b6b7e43093b5 /staging_vespalib
parent390ab5ab4e529746f33732e4f07d9ab581dbc5bd (diff)
Reduce unneccessary code incluscion.
Diffstat (limited to 'staging_vespalib')
-rw-r--r--staging_vespalib/src/vespa/vespalib/objects/deserializer.h23
-rw-r--r--staging_vespalib/src/vespa/vespalib/objects/deserializer.hpp33
-rw-r--r--staging_vespalib/src/vespa/vespalib/objects/fieldbase.h3
-rw-r--r--staging_vespalib/src/vespa/vespalib/objects/identifiable.cpp140
-rw-r--r--staging_vespalib/src/vespa/vespalib/objects/identifiable.h166
-rw-r--r--staging_vespalib/src/vespa/vespalib/objects/identifiable.hpp157
-rw-r--r--staging_vespalib/src/vespa/vespalib/objects/nboserializer.cpp5
-rw-r--r--staging_vespalib/src/vespa/vespalib/objects/nboserializer.h6
-rw-r--r--staging_vespalib/src/vespa/vespalib/objects/serializer.h21
-rw-r--r--staging_vespalib/src/vespa/vespalib/objects/serializer.hpp33
-rw-r--r--staging_vespalib/src/vespa/vespalib/objects/visit.cpp2
-rw-r--r--staging_vespalib/src/vespa/vespalib/objects/visit.h83
-rw-r--r--staging_vespalib/src/vespa/vespalib/objects/visit.hpp78
13 files changed, 418 insertions, 332 deletions
diff --git a/staging_vespalib/src/vespa/vespalib/objects/deserializer.h b/staging_vespalib/src/vespa/vespalib/objects/deserializer.h
index 3e3eb3cc526..e25e08e67c9 100644
--- a/staging_vespalib/src/vespa/vespalib/objects/deserializer.h
+++ b/staging_vespalib/src/vespa/vespalib/objects/deserializer.h
@@ -6,8 +6,7 @@
#include <vector>
#include <stdint.h>
-namespace vespalib
-{
+namespace vespalib {
class Identifiable;
@@ -57,25 +56,9 @@ public:
Deserializer & operator >> (double & value) { return get(_unspecifiedField, value); }
Deserializer & operator >> (string & value) { return get(_unspecifiedField, value); }
template <typename T>
- Deserializer & operator >> (vespalib::Array<T> & v) {
- uint32_t sz;
- get(_sizeField, sz);
- v.resize(sz);
- for(size_t i(0); i < sz; i++) {
- (*this) >> v[i];
- }
- return *this;
- }
+ Deserializer & operator >> (vespalib::Array<T> & v);
template <typename T>
- Deserializer & operator >> (std::vector<T> & v) {
- uint32_t sz;
- get(_sizeField, sz);
- v.resize(sz);
- for(size_t i(0); i < sz; i++) {
- (*this) >> v[i];
- }
- return *this;
- }
+ Deserializer & operator >> (std::vector<T> & v);
};
diff --git a/staging_vespalib/src/vespa/vespalib/objects/deserializer.hpp b/staging_vespalib/src/vespa/vespalib/objects/deserializer.hpp
new file mode 100644
index 00000000000..4d01994461a
--- /dev/null
+++ b/staging_vespalib/src/vespa/vespalib/objects/deserializer.hpp
@@ -0,0 +1,33 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+#pragma once
+
+#include "deserializer.h"
+
+namespace vespalib {
+
+template <typename T>
+Deserializer &
+Deserializer::operator >> (vespalib::Array<T> & v) {
+ uint32_t sz;
+ get(_sizeField, sz);
+ v.resize(sz);
+ for(size_t i(0); i < sz; i++) {
+ (*this) >> v[i];
+ }
+ return *this;
+}
+
+template <typename T>
+Deserializer &
+Deserializer::operator >> (std::vector<T> & v) {
+ uint32_t sz;
+ get(_sizeField, sz);
+ v.resize(sz);
+ for(size_t i(0); i < sz; i++) {
+ (*this) >> v[i];
+ }
+ return *this;
+}
+
+}
+
diff --git a/staging_vespalib/src/vespa/vespalib/objects/fieldbase.h b/staging_vespalib/src/vespa/vespalib/objects/fieldbase.h
index 9592566b7d4..e3f55161b77 100644
--- a/staging_vespalib/src/vespa/vespalib/objects/fieldbase.h
+++ b/staging_vespalib/src/vespa/vespalib/objects/fieldbase.h
@@ -3,8 +3,7 @@
#include <vespa/vespalib/stllike/string.h>
-namespace vespalib
-{
+namespace vespalib {
class IFieldBase
{
diff --git a/staging_vespalib/src/vespa/vespalib/objects/identifiable.cpp b/staging_vespalib/src/vespa/vespalib/objects/identifiable.cpp
index c6459219fc4..f142fedade5 100644
--- a/staging_vespalib/src/vespa/vespalib/objects/identifiable.cpp
+++ b/staging_vespalib/src/vespa/vespalib/objects/identifiable.cpp
@@ -1,20 +1,89 @@
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/fastos/fastos.h>
+#include "identifiable.hpp"
#include <cassert>
#include <vespa/vespalib/util/stringfmt.h>
#include <stdexcept>
#include <algorithm>
-#include <vespa/vespalib/objects/identifiable.h>
+#include <vespa/vespalib/objects/nbostream.h>
#include "objectdumper.h"
#include "visit.h"
#include "objectpredicate.h"
#include "objectoperation.h"
#include <vespa/vespalib/util/classname.h>
+#include <vespa/vespalib/stllike/hash_set.h>
+
namespace vespalib {
-Identifiable::Register * Identifiable::_register = NULL;
-Identifiable::ILoader * Identifiable::_classLoader = NULL;
+namespace {
+
+class Register {
+public:
+ using RuntimeClass = Identifiable::RuntimeClass;
+ Register();
+ ~Register();
+ bool append(RuntimeClass * c);
+ bool erase(RuntimeClass * c);
+ const RuntimeClass * classFromId(unsigned id) const;
+ const RuntimeClass * classFromName(const char * name) const;
+ const char * id2Name(unsigned id) const;
+ unsigned name2Id(const char * name) const;
+ bool empty() const { return _listById.empty(); }
+private:
+ struct GetId { uint32_t operator() (const RuntimeClass * f) const { return f->id(); } };
+ struct HashId { size_t operator() (const RuntimeClass * f) const { return f->id(); } };
+ struct EqualId { bool operator() (const RuntimeClass * a, const RuntimeClass * b) const { return a->id() == b->id(); } };
+ struct GetName { const char * operator() (const RuntimeClass * f) const { return f->name(); } };
+ struct HashName { size_t operator() (const RuntimeClass * f) const { return hashValue(f->name()); } };
+ struct EqualName { bool operator() (const RuntimeClass * a, const RuntimeClass * b) const { return strcmp(a->name(), b->name()) == 0; } };
+ typedef hash_set<RuntimeClass *, HashId, EqualId> IdList;
+ typedef hash_set<RuntimeClass *, HashName, EqualName> NameList;
+ IdList _listById;
+ NameList _listByName;
+};
+
+Register::Register() :
+ _listById(),
+ _listByName()
+{ }
+
+Register::~Register() { }
+
+bool Register::erase(Identifiable::RuntimeClass * c)
+{
+ _listById.erase(c);
+ _listByName.erase(c);
+ return true;
+}
+
+bool Register::append(Identifiable::RuntimeClass * c)
+{
+ bool ok((_listById.find(c) == _listById.end()) && ((_listByName.find(c) == _listByName.end())));
+ if (ok) {
+ _listById.insert(c);
+ _listByName.insert(c);
+ }
+ return ok;
+}
+
+const Identifiable::RuntimeClass * Register::classFromId(unsigned id) const
+{
+ IdList::const_iterator it(_listById.find<uint32_t, GetId, hash<uint32_t>, std::equal_to<uint32_t> >(id));
+ return (it != _listById.end()) ? *it : NULL;
+}
+
+const Identifiable::RuntimeClass * Register::classFromName(const char *name) const
+{
+ NameList::const_iterator it(_listByName.find<const char *, GetName, hash<const char *>, std::equal_to<const char *> >(name));
+ return (it != _listByName.end()) ? *it : NULL;
+}
+
+Register * _register = nullptr;
+
+}
+
+Identifiable::ILoader * Identifiable::_classLoader = nullptr;
FieldBase Identifiable::hasObjectField("hasObject");
FieldBase Identifiable::sizeField("size");
FieldBase Identifiable::classIdField("classId");
@@ -22,6 +91,16 @@ FieldBase Identifiable::objectField("object");
IMPLEMENT_IDENTIFIABLE(Identifiable, Identifiable);
+const Identifiable::RuntimeClass *
+Identifiable::classFromId(unsigned id) {
+ return _register->classFromId(id);
+}
+
+const Identifiable::RuntimeClass *
+Identifiable::classFromName(const char * name) {
+ return _register->classFromName(name);
+}
+
Identifiable::RuntimeClass::RuntimeClass(RuntimeInfo * info_) :
_rt(info_)
{
@@ -36,23 +115,23 @@ Identifiable::RuntimeClass::RuntimeClass(RuntimeInfo * info_) :
}
}
}
- if (Identifiable::_register == NULL) {
- Identifiable::_register = new Register();
+ if (_register == NULL) {
+ _register = new Register();
}
- if (! Identifiable::_register->append(this)) {
- const RuntimeClass * old = Identifiable::_register->classFromId(id());
+ if (! _register->append(this)) {
+ const RuntimeClass * old = _register->classFromId(id());
throw std::runtime_error(make_string("Duplicate Identifiable object(%s, %s, %d) being registered. Choose a unique id. Object (%s, %s, %d) is using it.", name(), info(), id(), old->name(), old->info(), old->id()));
}
}
Identifiable::RuntimeClass::~RuntimeClass()
{
- if ( ! Identifiable::_register->erase(this) ) {
+ if ( ! _register->erase(this) ) {
assert(0);
}
- if (Identifiable::_register->empty()) {
- delete Identifiable::_register;
- Identifiable::_register = NULL;
+ if (_register->empty()) {
+ delete _register;
+ _register = NULL;
}
}
@@ -63,23 +142,6 @@ bool Identifiable::RuntimeClass::inherits(unsigned cid) const
return (cid == cur->_id);
}
-Identifiable::Register::Register() :
- _listById(),
- _listByName()
-{
-}
-
-Identifiable::Register::~Register()
-{
-}
-
-bool Identifiable::Register::erase(Identifiable::RuntimeClass * c)
-{
- _listById.erase(c);
- _listByName.erase(c);
- return true;
-}
-
class SortById : public std::binary_function<const Identifiable::RuntimeClass *, const Identifiable::RuntimeClass *, bool> {
public:
bool operator() (const Identifiable::RuntimeClass * x, const Identifiable::RuntimeClass * y) const {
@@ -94,28 +156,6 @@ public:
}
};
-bool Identifiable::Register::append(Identifiable::RuntimeClass * c)
-{
- bool ok((_listById.find(c) == _listById.end()) && ((_listByName.find(c) == _listByName.end())));
- if (ok) {
- _listById.insert(c);
- _listByName.insert(c);
- }
- return ok;
-}
-
-const Identifiable::RuntimeClass * Identifiable::Register::classFromId(unsigned id) const
-{
- IdList::const_iterator it(_listById.find<uint32_t, GetId, hash<uint32_t>, std::equal_to<uint32_t> >(id));
- return (it != _listById.end()) ? *it : NULL;
-}
-
-const Identifiable::RuntimeClass * Identifiable::Register::classFromName(const char *name) const
-{
- NameList::const_iterator it(_listByName.find<const char *, GetName, hash<const char *>, std::equal_to<const char *> >(name));
- return (it != _listByName.end()) ? *it : NULL;
-}
-
Serializer & operator << (Serializer & os, const Identifiable & obj)
{
os.put(Identifiable::classIdField, obj.getClass().id());
diff --git a/staging_vespalib/src/vespa/vespalib/objects/identifiable.h b/staging_vespalib/src/vespa/vespalib/objects/identifiable.h
index 67f5917c68d..33c936afbd4 100644
--- a/staging_vespalib/src/vespa/vespalib/objects/identifiable.h
+++ b/staging_vespalib/src/vespa/vespalib/objects/identifiable.h
@@ -28,8 +28,6 @@
#include "objectvisitor.h"
#include <vespa/vespalib/util/memory.h>
-#include <vespa/vespalib/util/linkedptr.h>
-#include <vespa/vespalib/stllike/hash_set.h>
#define IDENTIFIABLE_CLASSID(cclass) CID_##cclass
#define IDENTIFIABLE_CLASSID_NS(ns, cclass) CID_##ns##_##cclass
@@ -124,8 +122,7 @@
virtual vespalib::Deserializer & onDeserialize(vespalib::Deserializer & is);
-namespace vespalib
-{
+namespace vespalib {
class ObjectPredicate;
class ObjectOperation;
@@ -233,12 +230,12 @@ public:
* Given the unique registered id of a class it will look up the object describing it.
* @return object describing the class.
*/
- static const RuntimeClass * classFromId(unsigned id) { return _register->classFromId(id); }
+ static const RuntimeClass * classFromId(unsigned id);
/**
* Given the unique registered name of a class it will look up the object describing it.
* @return object describing the class.
*/
- static const RuntimeClass * classFromName(const char * name) { return _register->classFromName(name); }
+ static const RuntimeClass * classFromName(const char * name);
/**
* Here you can provide an optional classloader.
*/
@@ -402,165 +399,8 @@ private:
virtual Serializer & onSerialize(Serializer & os) const;
virtual Deserializer & onDeserialize(Deserializer & is);
- class Register {
- public:
- Register();
- ~Register();
- bool append(RuntimeClass * c);
- bool erase(RuntimeClass * c);
- const RuntimeClass * classFromId(unsigned id) const;
- const RuntimeClass * classFromName(const char * name) const;
- const char * id2Name(unsigned id) const;
- unsigned name2Id(const char * name) const;
- bool empty() const { return _listById.empty(); }
- private:
- struct GetId { uint32_t operator() (const RuntimeClass * f) const { return f->id(); } };
- struct HashId { size_t operator() (const RuntimeClass * f) const { return f->id(); } };
- struct EqualId { bool operator() (const RuntimeClass * a, const RuntimeClass * b) const { return a->id() == b->id(); } };
- struct GetName { const char * operator() (const RuntimeClass * f) const { return f->name(); } };
- struct HashName { size_t operator() (const RuntimeClass * f) const { return hashValue(f->name()); } };
- struct EqualName { bool operator() (const RuntimeClass * a, const RuntimeClass * b) const { return strcmp(a->name(), b->name()) == 0; } };
- typedef hash_set<RuntimeClass *, HashId, EqualId> IdList;
- typedef hash_set<RuntimeClass *, HashName, EqualName> NameList;
- IdList _listById;
- NameList _listByName;
- };
- static Register * _register;
static ILoader * _classLoader;
};
-template <typename T>
-Serializer & Identifiable::serialize(const T & v, Serializer & os) {
- uint32_t sz(v.size());
- os.put(sizeField, sz);
- for(size_t i(0); i < sz; i++) {
- v[i].serialize(os);
- }
- return os;
-}
-
-template <typename T>
-Deserializer & Identifiable::deserialize(T & v, Deserializer & is) {
- uint32_t sz(0);
- is.get(sizeField, sz);
- v.resize(sz);
- for(size_t i(0); i < sz; i++) {
- v[i].deserialize(is);
- }
- return is;
-}
-
-template <typename T>
-class IdentifiablePtr : public CloneablePtr<T>
-{
-public:
- IdentifiablePtr(const T &t) : CloneablePtr<T>(t.clone()) {}
- IdentifiablePtr(T * p=NULL) : CloneablePtr<T>(p) { }
- int cmp(const IdentifiablePtr<T> &rhs) const {
- const T *a = this->get();
- const T *b = rhs.get();
- if (a == 0) {
- return (b == 0) ? 0 : -1;
- }
- return (b == 0) ? 1 : a->cmp(*b);
- }
- bool operator < (const IdentifiablePtr<T> &rhs) const { return (cmp(rhs) < 0); }
- bool operator > (const IdentifiablePtr<T> &rhs) const { return (cmp(rhs) > 0); }
- bool operator == (const IdentifiablePtr<T> &rhs) const { return (cmp(rhs) == 0); }
- bool operator != (const IdentifiablePtr<T> &rhs) const { return (cmp(rhs) != 0); }
- Serializer & serialize(Serializer & os) const {
- if (this->get()) {
- os.put(Identifiable::hasObjectField, uint8_t(1)) << *this->get();
- } else {
- os.put(Identifiable::hasObjectField, uint8_t(0));
- }
- return os;
- }
- Deserializer & deserialize(Deserializer & is) {
- uint8_t hasObject;
- is.get(Identifiable::hasObjectField, hasObject);
- if (hasObject) {
- this->reset(static_cast<T *>(Identifiable::create(is).release()));
- }
- return is;
- }
- friend Serializer & operator << (Serializer & os, const IdentifiablePtr<T> & agg) { return agg.serialize(os); }
- friend Deserializer & operator >> (Deserializer & is, IdentifiablePtr<T> & agg) { return agg.deserialize(is); }
-};
-
-template <typename T>
-class IdentifiableSharedPtr : public std::shared_ptr<T>
-{
-public:
- IdentifiableSharedPtr(const T &t) : std::shared_ptr<T>(t.clone()) {}
- IdentifiableSharedPtr(T * p=NULL) : std::shared_ptr<T>(p) { }
- int cmp(const IdentifiableSharedPtr<T> &rhs) const {
- const T *a = this->get();
- const T *b = rhs.get();
- if (a == 0) {
- return (b == 0) ? 0 : -1;
- }
- return (b == 0) ? 1 : a->cmp(*b);
- }
- bool operator < (const IdentifiableSharedPtr<T> &rhs) const {
- return (cmp(rhs) < 0);
- }
- Serializer & serialize(Serializer & os) const {
- if (this->get()) {
- os.put(Identifiable::hasObjectField, uint8_t(1)) << *this->get();
- } else {
- os.put(Identifiable::hasObjectField, uint8_t(0));
- }
- return os;
- }
- Deserializer & deserialize(Deserializer & is) {
- uint8_t hasObject;
- is.get(Identifiable::hasObjectField, hasObject);
- if (hasObject) {
- reset(static_cast<T *>(Identifiable::create(is).release()));
- }
- return is;
- }
- friend Serializer & operator << (Serializer & os, const IdentifiableSharedPtr<T> & agg) { return agg.serialize(os); }
- friend Deserializer & operator >> (Deserializer & is, IdentifiableSharedPtr<T> & agg) { return agg.deserialize(is); }
-};
-
-template <typename T>
-class IdentifiableLinkedPtr : public LinkedPtr<T>
-{
-public:
- IdentifiableLinkedPtr(const T &t) : LinkedPtr<T>(t.clone()) {}
- IdentifiableLinkedPtr(T * p=NULL) : LinkedPtr<T>(p) { }
- int cmp(const IdentifiableLinkedPtr<T> &rhs) const {
- const T *a = this->get();
- const T *b = rhs.get();
- if (a == 0) {
- return (b == 0) ? 0 : -1;
- }
- return (b == 0) ? 1 : a->cmp(*b);
- }
- bool operator < (const IdentifiableLinkedPtr<T> &rhs) const {
- return (cmp(rhs) < 0);
- }
- Serializer & serialize(Serializer & os) const {
- if (this->get()) {
- os.put(Identifiable::hasObjectField, uint8_t(1)) << *this->get();
- } else {
- os.put(Identifiable::hasObjectField, uint8_t(0));
- }
- return os;
- }
- Deserializer & deserialize(Deserializer & is) {
- uint8_t hasObject;
- is.get(Identifiable::hasObjectField, hasObject);
- if (hasObject) {
- this->reset(static_cast<T *>(Identifiable::create(is).release()));
- }
- return is;
- }
- friend Serializer & operator << (Serializer & os, const IdentifiableLinkedPtr<T> & agg) { return agg.serialize(os); }
- friend Deserializer & operator >> (Deserializer & is, IdentifiableLinkedPtr<T> & agg) { return agg.deserialize(is); }
-};
-
}
diff --git a/staging_vespalib/src/vespa/vespalib/objects/identifiable.hpp b/staging_vespalib/src/vespa/vespalib/objects/identifiable.hpp
new file mode 100644
index 00000000000..617d631328b
--- /dev/null
+++ b/staging_vespalib/src/vespa/vespalib/objects/identifiable.hpp
@@ -0,0 +1,157 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+#pragma once
+/**
+ * \class vespalib::Identifiable
+ * \ingroup util
+ *
+ * \brief Superclass for objects adding some runtime type information.
+ *
+ * This class is a superclass used by many other classes to add some runtime
+ * information.
+ *
+ * This can be used to verify type to be able to do cheap static casts
+ * instead of dynamic casts. It can be used to identify type of object, and
+ * it can also be used to generate an object of the given type (If it's not
+ * an identifiable abstract type).
+ *
+ */
+
+#include "identifiable.h"
+#include <vespa/vespalib/util/linkedptr.h>
+
+namespace vespalib {
+
+template <typename T>
+Serializer & Identifiable::serialize(const T & v, Serializer & os) {
+ uint32_t sz(v.size());
+ os.put(sizeField, sz);
+ for(size_t i(0); i < sz; i++) {
+ v[i].serialize(os);
+ }
+ return os;
+}
+
+template <typename T>
+Deserializer & Identifiable::deserialize(T & v, Deserializer & is) {
+ uint32_t sz(0);
+ is.get(sizeField, sz);
+ v.resize(sz);
+ for(size_t i(0); i < sz; i++) {
+ v[i].deserialize(is);
+ }
+ return is;
+}
+
+template <typename T>
+class IdentifiablePtr : public CloneablePtr<T>
+{
+public:
+ IdentifiablePtr(const T &t) : CloneablePtr<T>(t.clone()) {}
+ IdentifiablePtr(T * p=NULL) : CloneablePtr<T>(p) { }
+ int cmp(const IdentifiablePtr<T> &rhs) const {
+ const T *a = this->get();
+ const T *b = rhs.get();
+ if (a == 0) {
+ return (b == 0) ? 0 : -1;
+ }
+ return (b == 0) ? 1 : a->cmp(*b);
+ }
+ bool operator < (const IdentifiablePtr<T> &rhs) const { return (cmp(rhs) < 0); }
+ bool operator > (const IdentifiablePtr<T> &rhs) const { return (cmp(rhs) > 0); }
+ bool operator == (const IdentifiablePtr<T> &rhs) const { return (cmp(rhs) == 0); }
+ bool operator != (const IdentifiablePtr<T> &rhs) const { return (cmp(rhs) != 0); }
+ Serializer & serialize(Serializer & os) const {
+ if (this->get()) {
+ os.put(Identifiable::hasObjectField, uint8_t(1)) << *this->get();
+ } else {
+ os.put(Identifiable::hasObjectField, uint8_t(0));
+ }
+ return os;
+ }
+ Deserializer & deserialize(Deserializer & is) {
+ uint8_t hasObject;
+ is.get(Identifiable::hasObjectField, hasObject);
+ if (hasObject) {
+ this->reset(static_cast<T *>(Identifiable::create(is).release()));
+ }
+ return is;
+ }
+ friend Serializer & operator << (Serializer & os, const IdentifiablePtr<T> & agg) { return agg.serialize(os); }
+ friend Deserializer & operator >> (Deserializer & is, IdentifiablePtr<T> & agg) { return agg.deserialize(is); }
+};
+
+template <typename T>
+class IdentifiableSharedPtr : public std::shared_ptr<T>
+{
+public:
+ IdentifiableSharedPtr(const T &t) : std::shared_ptr<T>(t.clone()) {}
+ IdentifiableSharedPtr(T * p=NULL) : std::shared_ptr<T>(p) { }
+ int cmp(const IdentifiableSharedPtr<T> &rhs) const {
+ const T *a = this->get();
+ const T *b = rhs.get();
+ if (a == 0) {
+ return (b == 0) ? 0 : -1;
+ }
+ return (b == 0) ? 1 : a->cmp(*b);
+ }
+ bool operator < (const IdentifiableSharedPtr<T> &rhs) const {
+ return (cmp(rhs) < 0);
+ }
+ Serializer & serialize(Serializer & os) const {
+ if (this->get()) {
+ os.put(Identifiable::hasObjectField, uint8_t(1)) << *this->get();
+ } else {
+ os.put(Identifiable::hasObjectField, uint8_t(0));
+ }
+ return os;
+ }
+ Deserializer & deserialize(Deserializer & is) {
+ uint8_t hasObject;
+ is.get(Identifiable::hasObjectField, hasObject);
+ if (hasObject) {
+ reset(static_cast<T *>(Identifiable::create(is).release()));
+ }
+ return is;
+ }
+ friend Serializer & operator << (Serializer & os, const IdentifiableSharedPtr<T> & agg) { return agg.serialize(os); }
+ friend Deserializer & operator >> (Deserializer & is, IdentifiableSharedPtr<T> & agg) { return agg.deserialize(is); }
+};
+
+template <typename T>
+class IdentifiableLinkedPtr : public LinkedPtr<T>
+{
+public:
+ IdentifiableLinkedPtr(const T &t) : LinkedPtr<T>(t.clone()) {}
+ IdentifiableLinkedPtr(T * p=NULL) : LinkedPtr<T>(p) { }
+ int cmp(const IdentifiableLinkedPtr<T> &rhs) const {
+ const T *a = this->get();
+ const T *b = rhs.get();
+ if (a == 0) {
+ return (b == 0) ? 0 : -1;
+ }
+ return (b == 0) ? 1 : a->cmp(*b);
+ }
+ bool operator < (const IdentifiableLinkedPtr<T> &rhs) const {
+ return (cmp(rhs) < 0);
+ }
+ Serializer & serialize(Serializer & os) const {
+ if (this->get()) {
+ os.put(Identifiable::hasObjectField, uint8_t(1)) << *this->get();
+ } else {
+ os.put(Identifiable::hasObjectField, uint8_t(0));
+ }
+ return os;
+ }
+ Deserializer & deserialize(Deserializer & is) {
+ uint8_t hasObject;
+ is.get(Identifiable::hasObjectField, hasObject);
+ if (hasObject) {
+ this->reset(static_cast<T *>(Identifiable::create(is).release()));
+ }
+ return is;
+ }
+ friend Serializer & operator << (Serializer & os, const IdentifiableLinkedPtr<T> & agg) { return agg.serialize(os); }
+ friend Deserializer & operator >> (Deserializer & is, IdentifiableLinkedPtr<T> & agg) { return agg.deserialize(is); }
+};
+
+}
diff --git a/staging_vespalib/src/vespa/vespalib/objects/nboserializer.cpp b/staging_vespalib/src/vespa/vespalib/objects/nboserializer.cpp
index 00e7a9c7edd..38dca76a0cd 100644
--- a/staging_vespalib/src/vespa/vespalib/objects/nboserializer.cpp
+++ b/staging_vespalib/src/vespa/vespalib/objects/nboserializer.cpp
@@ -1,9 +1,14 @@
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/fastos/fastos.h>
#include "nboserializer.h"
+#include <vespa/vespalib/objects/nbostream.h>
namespace vespalib {
+const char * NBOSerializer::peek() const {
+ return _stream.peek();
+}
+
NBOSerializer &NBOSerializer::put(const IFieldBase &, bool value) {
_stream << value;
return *this;
diff --git a/staging_vespalib/src/vespa/vespalib/objects/nboserializer.h b/staging_vespalib/src/vespa/vespalib/objects/nboserializer.h
index bac768fec2c..9288757508e 100644
--- a/staging_vespalib/src/vespa/vespalib/objects/nboserializer.h
+++ b/staging_vespalib/src/vespa/vespalib/objects/nboserializer.h
@@ -3,9 +3,11 @@
#include <vespa/vespalib/objects/serializer.h>
#include <vespa/vespalib/objects/deserializer.h>
-#include <vespa/vespalib/objects/nbostream.h>
namespace vespalib {
+
+class nbostream;
+
class NBOSerializer : public Serializer, public Deserializer {
public:
NBOSerializer(nbostream &stream) : _stream(stream) { }
@@ -27,7 +29,7 @@ public:
virtual NBOSerializer &get(const IFieldBase &field, float &value);
virtual NBOSerializer &get(const IFieldBase &field, string &value);
- const char *peek() const { return _stream.peek(); }
+ const char *peek() const;
const nbostream &getStream() const { return _stream; }
nbostream &getStream() { return _stream; }
diff --git a/staging_vespalib/src/vespa/vespalib/objects/serializer.h b/staging_vespalib/src/vespa/vespalib/objects/serializer.h
index c846123ce45..5e67b968946 100644
--- a/staging_vespalib/src/vespa/vespalib/objects/serializer.h
+++ b/staging_vespalib/src/vespa/vespalib/objects/serializer.h
@@ -6,8 +6,7 @@
#include <vector>
#include <stdint.h>
-namespace vespalib
-{
+namespace vespalib {
class Identifiable;
@@ -43,23 +42,9 @@ public:
Serializer & operator << (double value) { return put(_unspecifiedField, value); }
Serializer & operator << (const stringref & value) { return put(_unspecifiedField, value); }
template <typename T>
- Serializer & operator << (const vespalib::Array<T> & v) {
- uint32_t sz(v.size());
- put(_sizeField, sz);
- for(size_t i(0); i < sz; i++) {
- (*this) << v[i];
- }
- return *this;
- }
+ Serializer & operator << (const vespalib::Array<T> & v);
template <typename T>
- Serializer & operator << (const std::vector<T> & v) {
- uint32_t sz(v.size());
- put(_sizeField, sz);
- for(size_t i(0); i < sz; i++) {
- (*this) << v[i];
- }
- return *this;
- }
+ Serializer & operator << (const std::vector<T> & v);
};
}
diff --git a/staging_vespalib/src/vespa/vespalib/objects/serializer.hpp b/staging_vespalib/src/vespa/vespalib/objects/serializer.hpp
new file mode 100644
index 00000000000..4b632249679
--- /dev/null
+++ b/staging_vespalib/src/vespa/vespalib/objects/serializer.hpp
@@ -0,0 +1,33 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+#pragma once
+
+#include <vespa/vespalib/objects/fieldbase.h>
+#include <vespa/vespalib/util/array.h>
+#include <vector>
+#include <stdint.h>
+
+namespace vespalib {
+
+template <typename T>
+Serializer &
+Serializer::operator << (const vespalib::Array<T> & v) {
+ uint32_t sz(v.size());
+ put(_sizeField, sz);
+ for(size_t i(0); i < sz; i++) {
+ (*this) << v[i];
+ }
+ return *this;
+}
+template <typename T>
+Serializer &
+Serializer::operator << (const std::vector<T> & v) {
+ uint32_t sz(v.size());
+ put(_sizeField, sz);
+ for(size_t i(0); i < sz; i++) {
+ (*this) << v[i];
+ }
+ return *this;
+}
+
+}
+
diff --git a/staging_vespalib/src/vespa/vespalib/objects/visit.cpp b/staging_vespalib/src/vespa/vespalib/objects/visit.cpp
index 3b160961a01..fc266d5130d 100644
--- a/staging_vespalib/src/vespa/vespalib/objects/visit.cpp
+++ b/staging_vespalib/src/vespa/vespalib/objects/visit.cpp
@@ -1,6 +1,6 @@
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/fastos/fastos.h>
-#include "visit.h"
+#include "visit.hpp"
void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, const vespalib::Identifiable *obj) {
if (obj != 0) {
diff --git a/staging_vespalib/src/vespa/vespalib/objects/visit.h b/staging_vespalib/src/vespa/vespalib/objects/visit.h
index 42826c7ff74..fa5bd8f177d 100644
--- a/staging_vespalib/src/vespa/vespalib/objects/visit.h
+++ b/staging_vespalib/src/vespa/vespalib/objects/visit.h
@@ -1,11 +1,12 @@
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once
-#include <string>
-#include <vector>
-#include <vespa/vespalib/util/stringfmt.h>
-#include "objectvisitor.h"
-#include "identifiable.h"
+#include <vespa/vespalib/stllike/string.h>
+
+namespace vespalib {
+ class Identifiable;
+ class ObjectVisitor;
+}
void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, const vespalib::Identifiable *obj);
void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, const vespalib::Identifiable &obj);
@@ -21,75 +22,5 @@ void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, uint64_t
void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, float value);
void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, double value);
void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, const vespalib::string &value);
-void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, const std::string &value);
+void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, const vespalib::stringref &value);
void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, const char *value);
-
-template<typename T>
-void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, const vespalib::CloneablePtr<T> &ptr) {
- if (ptr.get()) {
- visit(self, name, *ptr);
- } else {
- self.visitNull(name);
- }
-}
-
-template<typename T>
-void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, const std::shared_ptr<T> &ptr) {
- if (ptr.get()) {
- visit(self, name, *ptr);
- } else {
- self.visitNull(name);
- }
-}
-
-template<typename T>
-void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, const std::unique_ptr<T> &ptr) {
- if (ptr.get()) {
- visit(self, name, *ptr);
- } else {
- self.visitNull(name);
- }
-}
-
-template<typename T>
-void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, const vespalib::LinkedPtr<T> &ptr) {
- if (ptr.get()) {
- visit(self, name, *ptr);
- } else {
- self.visitNull(name);
- }
-}
-
-template<typename T>
-void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, const vespalib::IdentifiablePtr<T> &ptr) {
- visit(self, name, ptr.get());
-}
-
-template<typename T>
-void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, const vespalib::IdentifiableLinkedPtr<T> &ptr) {
- visit(self, name, ptr.get());
-}
-
-template<typename T>
-void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, const vespalib::IdentifiableSharedPtr<T> &ptr) {
- visit(self, name, ptr.get());
-}
-
-template<typename T>
-void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, const std::vector<T> &list) {
- self.openStruct(name, "std::vector");
- for (uint32_t i = 0; i < list.size(); ++i) {
- visit(self, vespalib::make_string("[%u]", i), list[i]);
- }
- self.closeStruct();
-}
-
-template<typename T>
-void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, const vespalib::Array<T> &list) {
- self.openStruct(name, "vespalib::Array");
- for (uint32_t i = 0; i < list.size(); ++i) {
- visit(self, vespalib::make_string("[%u]", i), list[i]);
- }
- self.closeStruct();
-}
-
diff --git a/staging_vespalib/src/vespa/vespalib/objects/visit.hpp b/staging_vespalib/src/vespa/vespalib/objects/visit.hpp
new file mode 100644
index 00000000000..ca4cd5972bd
--- /dev/null
+++ b/staging_vespalib/src/vespa/vespalib/objects/visit.hpp
@@ -0,0 +1,78 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+#pragma once
+
+#include "visit.h"
+#include <vector>
+#include <vespa/vespalib/util/stringfmt.h>
+#include "objectvisitor.h"
+#include "identifiable.hpp"
+
+template<typename T>
+void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, const vespalib::CloneablePtr<T> &ptr) {
+ if (ptr.get()) {
+ visit(self, name, *ptr);
+ } else {
+ self.visitNull(name);
+ }
+}
+
+template<typename T>
+void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, const std::shared_ptr<T> &ptr) {
+ if (ptr.get()) {
+ visit(self, name, *ptr);
+ } else {
+ self.visitNull(name);
+ }
+}
+
+template<typename T>
+void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, const std::unique_ptr<T> &ptr) {
+ if (ptr.get()) {
+ visit(self, name, *ptr);
+ } else {
+ self.visitNull(name);
+ }
+}
+
+template<typename T>
+void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, const vespalib::LinkedPtr<T> &ptr) {
+ if (ptr.get()) {
+ visit(self, name, *ptr);
+ } else {
+ self.visitNull(name);
+ }
+}
+
+template<typename T>
+void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, const vespalib::IdentifiablePtr<T> &ptr) {
+ visit(self, name, ptr.get());
+}
+
+template<typename T>
+void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, const vespalib::IdentifiableLinkedPtr<T> &ptr) {
+ visit(self, name, ptr.get());
+}
+
+template<typename T>
+void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, const vespalib::IdentifiableSharedPtr<T> &ptr) {
+ visit(self, name, ptr.get());
+}
+
+template<typename T>
+void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, const std::vector<T> &list) {
+ self.openStruct(name, "std::vector");
+ for (uint32_t i = 0; i < list.size(); ++i) {
+ visit(self, vespalib::make_string("[%u]", i), list[i]);
+ }
+ self.closeStruct();
+}
+
+template<typename T>
+void visit(vespalib::ObjectVisitor &self, const vespalib::string &name, const vespalib::Array<T> &list) {
+ self.openStruct(name, "vespalib::Array");
+ for (uint32_t i = 0; i < list.size(); ++i) {
+ visit(self, vespalib::make_string("[%u]", i), list[i]);
+ }
+ self.closeStruct();
+}
+