Skip to content

Commit

Permalink
add SIGNAL_CONNECTOR macro
Browse files Browse the repository at this point in the history
  • Loading branch information
srouquette committed Sep 5, 2015
1 parent 9474a21 commit fe47060
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 102 deletions.
11 changes: 11 additions & 0 deletions include/common/macro.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) 2014 Sylvain Rouquette
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef INCLUDE_COMMON_MACRO_H_
#define INCLUDE_COMMON_MACRO_H_

#define SIGNAL_CONNECTOR(sig) void sig(signal_t::slot_type callback) { sig##_.connect(callback); }

#endif // INCLUDE_COMMON_MACRO_H_
23 changes: 0 additions & 23 deletions include/filesystem/property.h

This file was deleted.

30 changes: 0 additions & 30 deletions include/filesystem/property_status.h

This file was deleted.

11 changes: 6 additions & 5 deletions include/filesystem/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef INCLUDE_FILESYSTEM_STORAGE_H_
#define INCLUDE_FILESYSTEM_STORAGE_H_

#include "common/macro.h"
#include "filesystem/forward_decl.h"
#include "platform/pragma.h"

Expand All @@ -27,7 +28,7 @@ namespace fs = FILESYSTEM_NAMESPACE;
class Storage : public std::enable_shared_from_this<Storage> {
public:
friend Entry;
using signal_t = boost::signals2::signal<void (const Entry&)>;
using signal_t = boost::signals2::signal<void (const Entry&)>;

explicit Storage(const std::string& scheme);
Storage(const Storage&) = default;
Expand All @@ -39,9 +40,9 @@ class Storage : public std::enable_shared_from_this<Storage> {
const std::string& scheme() const;
virtual entry_ptr_t resolve(const std::string& url);

void on_create(signal_t::slot_type callback);
void on_delete(signal_t::slot_type callback);
void on_content_update(signal_t::slot_type callback);
SIGNAL_CONNECTOR(on_create);
SIGNAL_CONNECTOR(on_delete);
SIGNAL_CONNECTOR(on_update);

protected:
virtual entry_ptr_t create(const std::string& url, const fs::file_status& status);
Expand All @@ -58,7 +59,7 @@ class Storage : public std::enable_shared_from_this<Storage> {

signal_t on_create_;
signal_t on_delete_;
signal_t on_content_update_;
signal_t on_update_;
};

} // namespace filesystem
Expand Down
9 changes: 9 additions & 0 deletions include/filesystem/url_resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
#ifndef INCLUDE_FILESYSTEM_URL_RESOLVER_H_
#define INCLUDE_FILESYSTEM_URL_RESOLVER_H_

#include "common/macro.h"
#include "filesystem/forward_decl.h"

#include <boost/signals2.hpp>

#include <map>
#include <string>
#include <mutex> // NOLINT
Expand All @@ -17,12 +20,18 @@ namespace kodama { namespace filesystem {

class UrlResolver {
public:
using signal_t = boost::signals2::signal<void (const Storage&)>;

void add(const storage_ptr_t& storage);
entry_ptr_t resolve(const std::string& url) const;

SIGNAL_CONNECTOR(on_add);

private:
std::map<std::string, storage_ptr_t> storages_;
mutable std::mutex mutex_;

signal_t on_add_;
};

} // namespace filesystem
Expand Down
1 change: 0 additions & 1 deletion src/filesystem/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include "filesystem/entry.h"
#include "filesystem/exception.h"
#include "filesystem/property.h"
#include "filesystem/storage.h"


Expand Down
24 changes: 0 additions & 24 deletions src/filesystem/property_status.cpp

This file was deleted.

15 changes: 1 addition & 14 deletions src/filesystem/storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "filesystem/storage.h"
#include "filesystem/entry.h"
#include "filesystem/exception.h"
#include "filesystem/property_status.h"

namespace kodama { namespace filesystem {
namespace fs = FILESYSTEM_NAMESPACE;
Expand All @@ -17,7 +16,7 @@ Storage::Storage(const std::string& scheme)
, scheme_{ scheme }
, on_create_{}
, on_delete_{}
, on_content_update_{}
, on_update_{}
{}

Storage::~Storage()
Expand All @@ -44,18 +43,6 @@ entry_ptr_t Storage::resolve(const std::string& url) {
return entry;
}

void Storage::on_create(signal_t::slot_type callback) {
on_create_.connect(callback);
}

void Storage::on_delete(signal_t::slot_type callback) {
on_delete_.connect(callback);
}

void Storage::on_content_update(signal_t::slot_type callback) {
on_content_update_.connect(callback);
}

entry_ptr_t Storage::create(const std::string& url, const fs::file_status& status) {
return std::make_shared<Entry>(shared_from_this(), url, status, Entry::key{});
}
Expand Down
2 changes: 1 addition & 1 deletion src/filesystem/url_resolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace fs = FILESYSTEM_NAMESPACE;

void UrlResolver::add(const storage_ptr_t& storage) {
std::lock_guard<std::mutex> lock{ mutex_ };
storages_.emplace(storage->scheme(), storage);
storages_.insert(std::make_pair(storage->scheme(), storage));
}

entry_ptr_t UrlResolver::resolve(const std::string& url) const {
Expand Down
1 change: 0 additions & 1 deletion test/filesystem/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include "filesystem/entry.h"
#include "filesystem/exception.h"
#include "filesystem/property_status.h"
#include "test/filesystem/mock/storage.h"
#include "gmock/gmock.h"

Expand Down
5 changes: 3 additions & 2 deletions test/filesystem/pattern/storage_pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ TEST_P(StoragePattern, on_delete_signal) {
// TODO(Syl): delete entry from teh cache
}

TEST_P(StoragePattern, on_content_update_signal) {
TEST_P(StoragePattern, on_update_signal) {
auto storage = GetParam().storage();
auto path = GetParam().create_dir(DIRNAME);
std::string url, expected = storage->scheme() + path;
storage->on_content_update([&url](const Entry& entry) { url = entry.url(); });
storage->on_update([&url](const Entry& entry) { url = entry.url(); });
// TODO(Syl): list directory content and update entry
}

Expand All @@ -68,6 +68,7 @@ TEST_P(StoragePattern, use_cache) {
auto path = GetParam().create_dir(DIRNAME);
auto url = storage->scheme() + path;
ASSERT_EQ(url, storage->resolve(url)->url());
// a second time to check the cache
ASSERT_EQ(url, storage->resolve(url)->url());
}

Expand Down
2 changes: 1 addition & 1 deletion wscript
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ def build(bld):
src/app/*
res/{platform}.rc
'''.format(**bld.env)),
use = 'BOOST GCOV kodama_filesystem')
use = 'BOOST kodama_filesystem')

0 comments on commit fe47060

Please sign in to comment.