Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Scoped Storage and static Storage class#205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
coder137 merged 9 commits intomainfrombetter_storage
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletionbootstrap/include/bootstrap/build_buildcc.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -77,7 +77,7 @@ class BuildBuildCC {
const BaseToolchain &toolchain_;
TargetEnv env_;

PersistentStorage storage_;
ScopedStorage storage_;
};

} // namespace buildcc
Expand Down
2 changes: 1 addition & 1 deletionbuildcc/buildcc.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,6 +27,7 @@
#include "env/host_compiler.h"
#include "env/util.h"
#include "env/command.h"
#include "env/storage.h"

// Base
#include "toolchain/toolchain.h"
Expand DownExpand Up@@ -57,6 +58,5 @@
// BuildCC Modules
#include "args/args.h"
#include "args/register.h"
#include "args/persistent_storage.h"

#endif
9 changes: 0 additions & 9 deletionsbuildcc/lib/args/CMakeLists.txt
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -41,20 +41,12 @@ target_link_libraries(test_register PRIVATE
mock_args
)

add_executable(test_persistent_storage
test/test_persistent_storage.cpp
)
target_link_libraries(test_persistent_storage PRIVATE mock_args)

add_test(NAME test_args COMMAND test_args
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test
)
add_test(NAME test_register COMMAND test_register
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test
)
add_test(NAME test_persistent_storage COMMAND test_persistent_storage
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test
)
endif()

set(ARGS_SRCS
Expand All@@ -64,7 +56,6 @@ set(ARGS_SRCS
src/tasks.cpp
include/args/args.h
include/args/register.h
include/args/persistent_storage.h
)

if(${BUILDCC_BUILD_AS_SINGLE_LIB})
Expand Down
60 changes: 0 additions & 60 deletionsbuildcc/lib/args/test/test_persistent_storage.cpp
View file
Open in desktop

This file was deleted.

8 changes: 8 additions & 0 deletionsbuildcc/lib/env/CMakeLists.txt
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@ if (${TESTING})

src/env.cpp
src/task_state.cpp
src/storage.cpp

src/command.cpp
mock/execute.cpp
Expand DownExpand Up@@ -38,10 +39,14 @@ if (${TESTING})
add_executable(test_command test/test_command.cpp)
target_link_libraries(test_command PRIVATE mock_env)

add_executable(test_storage test/test_storage.cpp)
target_link_libraries(test_storage PRIVATE mock_env)

add_test(NAME test_static_project COMMAND test_static_project)
add_test(NAME test_env_util COMMAND test_env_util)
add_test(NAME test_task_state COMMAND test_task_state)
add_test(NAME test_command COMMAND test_command)
add_test(NAME test_storage COMMAND test_storage)
endif()

set(ENV_SRCS
Expand All@@ -64,6 +69,9 @@ set(ENV_SRCS
src/command.cpp
src/execute.cpp
include/env/command.h

src/storage.cpp
include/env/storage.h
)

if(${BUILDCC_BUILD_AS_SINGLE_LIB})
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,10 +14,11 @@
* limitations under the License.
*/

#ifndefARGS_PERSISTENT_STORAGE_H_
#defineARGS_PERSISTENT_STORAGE_H_
#ifndefENV_STORAGE_H_
#defineENV_STORAGE_H_

#include <functional>
#include <memory>
#include <string>
#include <typeinfo>
#include <unordered_map>
Expand All@@ -28,17 +29,19 @@

namespace buildcc {

classPersistentStorage {
classScopedStorage {
public:
PersistentStorage() {}
~PersistentStorage() {
ScopedStorage() {}
~ScopedStorage() {
for (const auto &ptr_iter : ptrs_) {
ptr_iter.second.destructor();
}
ptrs_.clear();
env::assert_fatal(ptrs_.empty(), "Memory not deallocated");
}

ScopedStorage(const ScopedStorage &) = delete;

template <typename T, typename... Params>
T &Add(const std::string &identifier, Params &&...params) {
T *ptr = new T(std::forward<Params>(params)...);
Expand DownExpand Up@@ -69,7 +72,7 @@ class PersistentStorage {
// https://stackoverflow.com/questions/123758/how-do-i-remove-code-duplication-between-similar-const-and-non-const-member-func/123995
template <typename T> T &Ref(const std::string &identifier) {
return const_cast<T &>(
static_cast<constPersistentStorage &>(*this).ConstRef<T>(identifier));
static_cast<constScopedStorage &>(*this).ConstRef<T>(identifier));
}

private:
Expand All@@ -90,6 +93,34 @@ class PersistentStorage {
std::unordered_map<std::string, PtrMetadata> ptrs_;
};

class Storage {
public:
Storage() = delete;
Storage(const Storage &) = delete;
Storage(Storage &&) = delete;

static void Init() { internal_ = std::make_unique<ScopedStorage>(); }
static void Deinit() { internal_.reset(nullptr); }

template <typename T, typename... Params>
static T &Add(const std::string &identifier, Params &&...params) {
return internal_->Add<T, Params...>(identifier,
std::forward<Params>(params)...);
}

template <typename T>
static const T &ConstRef(const std::string &identifier) {
return internal_->ConstRef<T>(identifier);
}

template <typename T> static T &Ref(const std::string &identifier) {
return internal_->Ref<T>(identifier);
}

private:
static std::unique_ptr<ScopedStorage> internal_;
};

} // namespace buildcc

#endif
23 changes: 23 additions & 0 deletionsbuildcc/lib/env/src/storage.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2021-2022 Niket Naidu. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "env/storage.h"

namespace buildcc {

std::unique_ptr<ScopedStorage> Storage::internal_;

}
91 changes: 91 additions & 0 deletionsbuildcc/lib/env/test/test_storage.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
#include "env/storage.h"

// NOTE, Make sure all these includes are AFTER the system and header includes
#include "CppUTest/CommandLineTestRunner.h"
#include "CppUTest/MemoryLeakDetectorNewMacros.h"
#include "CppUTest/TestHarness.h"
#include "CppUTest/Utest.h"
#include "CppUTestExt/MockSupport.h"

// clang-format off
TEST_GROUP(ScopedStorageTestGroup)
{
};

TEST_GROUP(StorageTestGroup)
{
void setup() {
buildcc::Storage::Init();
}
void teardown() {
buildcc::Storage::Deinit();
}
};
// clang-format on

class BigObj {};

class BigObjWithParameters {
public:
BigObjWithParameters(const std::string &name, int id, const BigObj &obj)
: name_(name) {
(void)id;
(void)obj;
}

const std::string &GetName() const { return name_; }

private:
std::string name_;
};

static BigObj obj;

TEST(ScopedStorageTestGroup, BasicUsage) {
buildcc::ScopedStorage storage;
storage.Add<BigObjWithParameters>("identifier", "name", 10, obj);
storage.Add<BigObjWithParameters>("identifier2", "name2", 12, obj);

// Usage
storage.ConstRef<BigObjWithParameters>("identifier").GetName();
storage.Ref<BigObjWithParameters>("identifier2").GetName();

// Automatic cleanup here
}

TEST(ScopedStorageTestGroup, IncorrectUsage) {
buildcc::ScopedStorage storage;
storage.Add<BigObjWithParameters>("identifier", "name", 10, obj);

// We try to cast to a different type!
CHECK_THROWS(std::exception, storage.Ref<BigObj>("identifier"));

// We use a wrong identifier
CHECK_THROWS(std::exception,
storage.Ref<BigObjWithParameters>("identifier2"));
}

std::string &toReference(std::string *pointer) { return *pointer; }

TEST(ScopedStorageTestGroup, NullptrDelete) {
buildcc::ScopedStorage storage;
storage.Remove<std::string>(nullptr);
}

TEST(StorageTestGroup, BasicUsage) {
buildcc::Storage::Add<BigObjWithParameters>("identifier", "name", 10, obj);
buildcc::Storage::Add<BigObjWithParameters>("identifier2", "name2", 12, obj);

// Usage
const auto &bigobj =
buildcc::Storage::ConstRef<BigObjWithParameters>("identifier").GetName();
const auto &bigobj2 =
buildcc::Storage::Ref<BigObjWithParameters>("identifier2").GetName();

STRCMP_EQUAL(bigobj.c_str(), "name");
STRCMP_EQUAL(bigobj2.c_str(), "name2");
}

int main(int ac, char **av) {
return CommandLineTestRunner::RunAllTests(ac, av);
}
2 changes: 1 addition & 1 deletionbuildexe/include/buildexe/build_env_setup.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -66,7 +66,7 @@ class BuildEnvSetup {
const BuildExeArgs &buildexe_args_;

ArgToolchainState state_;
PersistentStorage storage_;
ScopedStorage storage_;
};

} // namespace buildcc
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp