Cleanup: Move asset library reference C++ wrapper to own files

Although currently only the asset list code uses the asset library
reference wrapper, it can stand on its own and may be used in more
places in the future. So I prefer to give it its own source & header
file.

Also removed unused includes, added proper namespaces as per our C++
style guidelines, and removed an unnecessary TODO comment.
This commit is contained in:
Julian Eisel 2021-07-19 18:26:10 +02:00
parent ebe32e01e1
commit 582c5530b6
4 changed files with 101 additions and 47 deletions

View File

@ -34,6 +34,9 @@ set(SRC
asset_list.cc
asset_ops.cc
asset_temp_id_consumer.cc
intern/asset_library_reference.cc
intern/asset_library_reference.hh
)
set(LIB

View File

@ -26,12 +26,8 @@
#include <optional>
#include <string>
#include "BKE_asset.h"
#include "BKE_context.h"
#include "BKE_screen.h"
#include "BLI_function_ref.hh"
#include "BLI_hash.hh"
#include "BLI_map.hh"
#include "BLI_path_util.h"
#include "BLI_utility_mixins.hh"
@ -43,7 +39,6 @@
#include "ED_asset.h"
#include "ED_fileselect.h"
#include "ED_screen.h"
#include "WM_api.h"
#include "WM_types.h"
@ -51,48 +46,9 @@
/* XXX uses private header of file-space. */
#include "../space_file/filelist.h"
using namespace blender;
#include "intern/asset_library_reference.hh"
/**
* Wrapper to add logic to the AssetLibraryReference DNA struct.
*/
class AssetLibraryReferenceWrapper {
const AssetLibraryReference reference_;
public:
/* Intentionally not `explicit`, allow implicit conversion for convenience. Might have to be
* NOLINT */
AssetLibraryReferenceWrapper(const AssetLibraryReference &reference);
~AssetLibraryReferenceWrapper() = default;
friend bool operator==(const AssetLibraryReferenceWrapper &a,
const AssetLibraryReferenceWrapper &b);
uint64_t hash() const;
};
AssetLibraryReferenceWrapper::AssetLibraryReferenceWrapper(const AssetLibraryReference &reference)
: reference_(reference)
{
}
bool operator==(const AssetLibraryReferenceWrapper &a, const AssetLibraryReferenceWrapper &b)
{
return (a.reference_.type == b.reference_.type) && (a.reference_.type == ASSET_LIBRARY_CUSTOM) ?
(a.reference_.custom_library_index == b.reference_.custom_library_index) :
true;
}
uint64_t AssetLibraryReferenceWrapper::hash() const
{
uint64_t hash1 = DefaultHash<decltype(reference_.type)>{}(reference_.type);
if (reference_.type != ASSET_LIBRARY_CUSTOM) {
return hash1;
}
uint64_t hash2 = DefaultHash<decltype(reference_.custom_library_index)>{}(
reference_.custom_library_index);
return hash1 ^ (hash2 * 33); /* Copied from DefaultHash for std::pair. */
}
namespace blender::ed::asset {
/* -------------------------------------------------------------------- */
/** \name Asset list API
@ -469,10 +425,14 @@ AssetListStorage::AssetListMap &AssetListStorage::global_storage()
/** \} */
} // namespace blender::ed::asset
/* -------------------------------------------------------------------- */
/** \name C-API
* \{ */
using namespace blender::ed::asset;
/**
* Invoke asset list reading, potentially in a parallel job. Won't wait until the job is done,
* and may return earlier.
@ -506,7 +466,6 @@ bool ED_assetlist_storage_has_list_for_library(const AssetLibraryReference *libr
return AssetListStorage::lookup_list(*library_reference) != nullptr;
}
/* TODO expose AssetList with an iterator? */
void ED_assetlist_iterate(const AssetLibraryReference *library_reference, AssetListIterFn fn)
{
AssetList *list = AssetListStorage::lookup_list(*library_reference);

View File

@ -0,0 +1,46 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "BLI_hash.hh"
#include "asset_library_reference.hh"
namespace blender::ed::asset {
AssetLibraryReferenceWrapper::AssetLibraryReferenceWrapper(const AssetLibraryReference &reference)
: AssetLibraryReference(reference)
{
}
bool operator==(const AssetLibraryReferenceWrapper &a, const AssetLibraryReferenceWrapper &b)
{
return (a.type == b.type) && (a.type == ASSET_LIBRARY_CUSTOM) ?
(a.custom_library_index == b.custom_library_index) :
true;
}
uint64_t AssetLibraryReferenceWrapper::hash() const
{
uint64_t hash1 = DefaultHash<decltype(type)>{}(type);
if (type != ASSET_LIBRARY_CUSTOM) {
return hash1;
}
uint64_t hash2 = DefaultHash<decltype(custom_library_index)>{}(custom_library_index);
return hash1 ^ (hash2 * 33); /* Copied from DefaultHash for std::pair. */
}
} // namespace blender::ed::asset

View File

@ -0,0 +1,46 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** \file
* \ingroup edasset
*
* Utility to extend #AssetLibraryReference with C++ functionality (operators, hash function, etc).
*/
#pragma once
#include <cstdint>
#include "DNA_asset_types.h"
namespace blender::ed::asset {
/**
* Wrapper to add logic to the AssetLibraryReference DNA struct.
*/
class AssetLibraryReferenceWrapper : public AssetLibraryReference {
public:
/* Intentionally not `explicit`, allow implicit conversion for convenience. Might have to be
* NOLINT */
AssetLibraryReferenceWrapper(const AssetLibraryReference &reference);
~AssetLibraryReferenceWrapper() = default;
friend bool operator==(const AssetLibraryReferenceWrapper &a,
const AssetLibraryReferenceWrapper &b);
uint64_t hash() const;
};
} // namespace blender::ed::asset