sapling/cstore/store.h
Durham Goode a79aa29030 cstore: move uniondatapackstore holder to be a shared_ptr
Summary:
In a future patch we will want to pass the uniondatapackstore around to other
objects who will contribute to the lifetime. Let's change it to a shared_ptr so
that becomes easy.

Let's also make the destructor virtual, so we can pass different types of stores
around and have them be destructed correctly.

Test Plan: Ran the tests

Reviewers: #mercurial, simonfar

Reviewed By: simonfar

Subscribers: mjpieters

Differential Revision: https://phabricator.intern.facebook.com/D4603893

Signature: t1:4603893:1487847173:2fc3505032ea8c30cf9e0f76ac4e75d64513d87d
2017-02-23 14:03:04 -08:00

111 lines
1.9 KiB
C++

// store.h - c++ declarations for a data store
//
// Copyright 2017 Facebook, Inc.
//
// This software may be used and distributed according to the terms of the
// GNU General Public License version 2 or any later version.
//
// no-check-code
//
#ifndef STORE_H
#define STORE_H
#include "key.h"
class ConstantString {
friend class ConstantStringRef;
private:
char *_content;
size_t _size;
size_t _refCount;
ConstantString(char *content, size_t size) :
_content(content),
_size(size),
_refCount(1) {}
public:
~ConstantString() {
delete _content;
}
char *content() {
return _content;
}
size_t size() {
return _size;
}
void incref() {
_refCount++;
}
size_t decref() {
if (_refCount > 0) {
_refCount--;
}
return _refCount;
}
};
class ConstantStringRef {
private:
ConstantString *_str;
public:
explicit ConstantStringRef() :
_str(NULL) {
}
ConstantStringRef(char *str, size_t size) :
_str(new ConstantString(str, size)) {
}
ConstantStringRef(const ConstantStringRef &other) {
if (other._str) {
other._str->incref();
}
_str = other._str;
}
~ConstantStringRef() {
if (_str && _str->decref() == 0) {
delete _str;
}
}
ConstantStringRef& operator=(const ConstantStringRef &other) {
if (_str && _str->decref() == 0) {
delete _str;
}
_str = other._str;
if (_str) {
_str->incref();
}
return *this;
}
char *content() {
if (_str) {
return _str->content();
}
return NULL;
}
size_t size() {
if (_str) {
return _str->size();
}
return 0;
}
};
class Store {
public:
virtual ~Store() {}
virtual ConstantStringRef get(const Key &key) = 0;
};
#endif //STORE_H