forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_tracker.h
50 lines (42 loc) · 1.78 KB
/
mod_tracker.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#pragma once
#ifndef CATA_SRC_MOD_TRACKER_H
#define CATA_SRC_MOD_TRACKER_H
#include <string>
#include "type_id.h"
/**
* Mod Tracking:
*
* These functions live here to provide various JSON-loaded entities
* with the ids of the mods that they are from.
*
* To subscribe, a JSON-loaded entity simply needs to have a
* 'std::vector<std::pair<(string or int)_id<T>, mod_id> src;' member.
* (As well as storing their id in an 'id' member of either string or int id type)
*
* If the entity is loaded with generic_factory, no further changes are needed.
* If the entity is not loaded with generic_factory, 'assign_src()' must be called sometime
* after the 'id' member has been assigned.
*/
/** Template magic to determine if the conditions above are satisfied */
template<typename T, typename = std::void_t<>>
struct has_src_member : std::false_type {};
template<typename T>
struct has_src_member<T, std::void_t<decltype( std::declval<T &>().src.emplace_back( std::declval<T &>().id, mod_id() ) )>> :
std::true_type {};
/** Dummy function, for if those conditions are not satisfied */
template < typename T, typename std::enable_if_t < !has_src_member<T>::value > * = nullptr >
void assign_src( T &, const std::string & )
{
}
/** If those conditions are satisfied, keep track of where this item has been modified */
template<typename T, typename std::enable_if_t<has_src_member<T>::value > * = nullptr>
void assign_src( T &def, const std::string &src )
{
// We need to make sure we're keeping where this entity has been loaded
// If the id this was last loaded with is not this one, discard the history and start again
if( !def.src.empty() && def.src.back().first != def.id ) {
def.src.clear();
}
def.src.emplace_back( def.id, mod_id( src ) );
}
#endif // CATA_SRC_MOD_TRACKER_H