Skip to content

Commit 175860f

Browse files
author
ABW
committed
separate chain object macros and forward declarations from chainbase header
There is no need to pull all chainbase definitions when you just want definition of chain object.
1 parent 09c3d55 commit 175860f

6 files changed

Lines changed: 105 additions & 87 deletions

File tree

libraries/chain/include/hive/chain/database.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* Copyright (c) 2015 Cryptonomex, Inc., and contributors.
66
*/
77
#pragma once
8+
#include <chainbase/chainbase.hpp>
9+
810
#include <hive/chain/detail/state/global_property_object.hpp>
911
#include <hive/chain/node_property_object.hpp>
1012

libraries/chain/include/hive/chain/hive_object_types.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
#include <hive/chain/hive_fwd.hpp>
44

5-
#include <chainbase/chainbase.hpp>
5+
#include <chainbase/util/object.hpp>
6+
#include <chainbase/allocators.hpp>
67
#include <chainbase/util/object_id_serialization.hpp>
78

89
#include <hive/protocol/types.hpp>

libraries/chain/include/hive/chain/shared_authority.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <hive/chain/hive_object_types.hpp>
33

44
#include <hive/protocol/authority.hpp>
5-
#include <chainbase/chainbase.hpp>
5+
#include <chainbase/util/object.hpp>
66
#include <boost/interprocess/managed_mapped_file.hpp>
77

88
namespace hive { namespace chain {

libraries/chain/include/hive/chain/util/decoded_types_data_storage.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

33
#include <chainbase/allocators.hpp>
4-
#include <chainbase/chainbase.hpp>
4+
#include <chainbase/util/object.hpp>
55

66
#include <fc/crypto/ripemd160.hpp>
77
#include <fc/crypto/sha256.hpp>

libraries/chainbase/include/chainbase/chainbase.hpp

Lines changed: 2 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <chainbase/allocators.hpp>
2323
#include <chainbase/state_snapshot_support.hpp>
24+
#include <chainbase/util/object.hpp>
2425
#include <chainbase/util/object_id.hpp>
2526

2627
#include <fc/exception/exception.hpp>
@@ -179,89 +180,6 @@ namespace chainbase {
179180
struct needs_remove_check<T, std::void_t<decltype(std::declval<const T&>().check_on_remove())>>
180181
: std::true_type {};
181182

182-
template<uint16_t TypeNumber, typename Derived, typename HasDynamicAlloc = std::false_type, typename EnableNoUndo = std::false_type>
183-
struct object
184-
{
185-
typedef oid<Derived> id_type;
186-
typedef oid_ref<Derived> id_ref_type;
187-
enum { type_id = TypeNumber };
188-
/*
189-
Mark chain object class with true_type when it has elements that need to be passed an allocator.
190-
It adds code that collects dynamic allocation for benchmarks.
191-
*/
192-
typedef HasDynamicAlloc has_dynamic_alloc_t;
193-
/*
194-
Do not mark chain object class with it unless you know what you are doing! It enables "no undo"
195-
destructions of objects, but optimized for very specific scenario, where you are guaranteed that
196-
creation/modification of object prior of its "no undo" destruction is not present on different
197-
active revision of undo stack. If used in different scenario, rare forking events can lead to
198-
crashes or inconsistent state.
199-
Example: comment_object is not modified and there is 7 day grace period between creation and destruction
200-
during migration to archive, so there is no waiting undo element when migration is performed;
201-
the same is not true for volatile_comment_object
202-
*/
203-
typedef EnableNoUndo enable_no_undo_t;
204-
};
205-
206-
/** this class is ment to be specified to enable lookup of index type by object type using
207-
* the SET_INDEX_TYPE macro.
208-
**/
209-
template<typename T>
210-
struct get_index_type {};
211-
212-
/**
213-
* This macro must be used at global scope and OBJECT_TYPE and INDEX_TYPE must be fully qualified
214-
*/
215-
#define CHAINBASE_SET_INDEX_TYPE( OBJECT_TYPE, INDEX_TYPE ) \
216-
namespace chainbase { template<> struct get_index_type<OBJECT_TYPE> { typedef INDEX_TYPE type; }; }
217-
218-
#define CHAINBASE_OBJECT_1( object_class ) CHAINBASE_OBJECT_false( object_class )
219-
#define CHAINBASE_OBJECT_2( object_class, allow_default ) CHAINBASE_OBJECT_##allow_default( object_class )
220-
#define CHAINBASE_OBJECT_true( object_class ) CHAINBASE_OBJECT_COMMON( object_class ); public: object_class() : id(0) {} private:
221-
#define CHAINBASE_OBJECT_COMMON( object_class ) \
222-
private: \
223-
id_type id; \
224-
object_class( const object_class& source ) = default; \
225-
/* problem with this being private? you most likely did \
226-
auto chain_object = db.get(...); \
227-
instead of \
228-
auto& chain_object_ref = db.get(...); \
229-
In case you actually need copy, use copy_chain_object() below \
230-
*/ \
231-
object_class& operator= ( const object_class& source ) = default; \
232-
public: \
233-
id_type get_id() const { return id; } \
234-
object_class( object_class&& source ) = default; \
235-
object_class& operator= ( object_class&& source ) = default; \
236-
object_class copy_chain_object() const { return *this; } \
237-
friend class fc::reflector< object_class >
238-
239-
#define CHAINBASE_OBJECT_false( object_class ) CHAINBASE_OBJECT_COMMON( object_class ); object_class() = delete; \
240-
private:
241-
242-
/**
243-
* use at the start of any class derived from chainbase::object<>, f.e.:
244-
* CHAINBASE_OBJECT( account_object ) or
245-
* CHAINBASE_OBJECT( dynamic_global_property_object, true )
246-
* first parameter is a class name, second (true or false, default false) tells if default constructor should be allowed
247-
*/
248-
#define CHAINBASE_OBJECT( ... ) BOOST_PP_OVERLOAD(CHAINBASE_OBJECT_,__VA_ARGS__)(__VA_ARGS__)
249-
250-
251-
#define CHAINBASE_ALLOCATED_MEMBERS( r, init, member ) , member( init )
252-
#define CHAINBASE_DEFAULT_CONSTRUCTOR( OBJECT_TYPE, ALLOCATED_MEMBERS... ) \
253-
template<typename Constructor, typename Allocator> \
254-
OBJECT_TYPE( Allocator&& a, uint64_t _id, Constructor&& c ) \
255-
: id( _id ) BOOST_PP_SEQ_FOR_EACH( CHAINBASE_ALLOCATED_MEMBERS, a, ALLOCATED_MEMBERS ) \
256-
{ c(*this); }
257-
258-
#define CHAINBASE_UNPACK_CONSTRUCTOR( OBJECT_TYPE, ALLOCATED_MEMBERS... ) \
259-
private: template<typename Allocator> \
260-
OBJECT_TYPE( Allocator&& a, uint64_t _id, std::function<void(OBJECT_TYPE&)> unpackFn) \
261-
: id( _id ) BOOST_PP_SEQ_FOR_EACH( CHAINBASE_ALLOCATED_MEMBERS, a, ALLOCATED_MEMBERS ) \
262-
{ unpackFn(*this); } \
263-
template <class T> friend class chainbase::generic_index
264-
265183
class int_incrementer
266184
{
267185
public:
@@ -1483,5 +1401,5 @@ namespace chainbase {
14831401
bool _at_least_one_index_is_created_now = false;
14841402
};
14851403

1486-
} // namepsace chainbase
1404+
} // namespace chainbase
14871405

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#pragma once
2+
3+
#include <chainbase/util/object_id.hpp>
4+
#include <chainbase/allocator_helpers.hpp>
5+
6+
#include <boost/preprocessor/seq/for_each.hpp>
7+
#include <boost/preprocessor/facilities/overload.hpp>
8+
9+
#include <functional>
10+
11+
namespace chainbase {
12+
13+
template<typename MultiIndexType> class generic_index;
14+
15+
template<uint16_t TypeNumber, typename Derived, typename HasDynamicAlloc = std::false_type, typename EnableNoUndo = std::false_type>
16+
struct object
17+
{
18+
typedef oid<Derived> id_type;
19+
typedef oid_ref<Derived> id_ref_type;
20+
enum { type_id = TypeNumber };
21+
/*
22+
Mark chain object class with true_type when it has elements that need to be passed an allocator.
23+
It adds code that collects dynamic allocation for benchmarks.
24+
*/
25+
typedef HasDynamicAlloc has_dynamic_alloc_t;
26+
/*
27+
Do not mark chain object class with it unless you know what you are doing! It enables "no undo"
28+
destructions of objects, but optimized for very specific scenario, where you are guaranteed that
29+
creation/modification of object prior of its "no undo" destruction is not present on different
30+
active revision of undo stack. If used in different scenario, rare forking events can lead to
31+
crashes or inconsistent state.
32+
Example: comment_object is not modified and there is 7 day grace period between creation and destruction
33+
during migration to archive, so there is no waiting undo element when migration is performed;
34+
the same is not true for volatile_comment_object
35+
*/
36+
typedef EnableNoUndo enable_no_undo_t;
37+
};
38+
39+
/** this class is ment to be specified to enable lookup of index type by object type using
40+
* the SET_INDEX_TYPE macro.
41+
**/
42+
template<typename T>
43+
struct get_index_type {};
44+
45+
/**
46+
* This macro must be used at global scope and OBJECT_TYPE and INDEX_TYPE must be fully qualified
47+
*/
48+
#define CHAINBASE_SET_INDEX_TYPE( OBJECT_TYPE, INDEX_TYPE ) \
49+
namespace chainbase { template<> struct get_index_type<OBJECT_TYPE> { typedef INDEX_TYPE type; }; }
50+
51+
#define CHAINBASE_OBJECT_1( object_class ) CHAINBASE_OBJECT_false( object_class )
52+
#define CHAINBASE_OBJECT_2( object_class, allow_default ) CHAINBASE_OBJECT_##allow_default( object_class )
53+
#define CHAINBASE_OBJECT_true( object_class ) CHAINBASE_OBJECT_COMMON( object_class ); public: object_class() : id(0) {} private:
54+
#define CHAINBASE_OBJECT_COMMON( object_class ) \
55+
private: \
56+
id_type id; \
57+
object_class( const object_class& source ) = default; \
58+
/* problem with this being private? you most likely did \
59+
auto chain_object = db.get(...); \
60+
instead of \
61+
auto& chain_object_ref = db.get(...); \
62+
In case you actually need copy, use copy_chain_object() below \
63+
*/ \
64+
object_class& operator= ( const object_class& source ) = default; \
65+
public: \
66+
id_type get_id() const { return id; } \
67+
object_class( object_class&& source ) = default; \
68+
object_class& operator= ( object_class&& source ) = default; \
69+
object_class copy_chain_object() const { return *this; } \
70+
friend class fc::reflector< object_class >
71+
72+
#define CHAINBASE_OBJECT_false( object_class ) CHAINBASE_OBJECT_COMMON( object_class ); object_class() = delete; \
73+
private:
74+
75+
/**
76+
* use at the start of any class derived from chainbase::object<>, f.e.:
77+
* CHAINBASE_OBJECT( account_object ) or
78+
* CHAINBASE_OBJECT( dynamic_global_property_object, true )
79+
* first parameter is a class name, second (true or false, default false) tells if default constructor should be allowed
80+
*/
81+
#define CHAINBASE_OBJECT( ... ) BOOST_PP_OVERLOAD(CHAINBASE_OBJECT_,__VA_ARGS__)(__VA_ARGS__)
82+
83+
#define CHAINBASE_ALLOCATED_MEMBERS( r, init, member ) , member( init )
84+
#define CHAINBASE_DEFAULT_CONSTRUCTOR( OBJECT_TYPE, ALLOCATED_MEMBERS... ) \
85+
template<typename Constructor, typename Allocator> \
86+
OBJECT_TYPE( Allocator&& a, uint64_t _id, Constructor&& c ) \
87+
: id( _id ) BOOST_PP_SEQ_FOR_EACH( CHAINBASE_ALLOCATED_MEMBERS, a, ALLOCATED_MEMBERS ) \
88+
{ c(*this); }
89+
90+
#define CHAINBASE_UNPACK_CONSTRUCTOR( OBJECT_TYPE, ALLOCATED_MEMBERS... ) \
91+
private: template<typename Allocator> \
92+
OBJECT_TYPE( Allocator&& a, uint64_t _id, std::function<void(OBJECT_TYPE&)> unpackFn) \
93+
: id( _id ) BOOST_PP_SEQ_FOR_EACH( CHAINBASE_ALLOCATED_MEMBERS, a, ALLOCATED_MEMBERS ) \
94+
{ unpackFn(*this); } \
95+
template <class T> friend class chainbase::generic_index
96+
97+
} // namespace chainbase

0 commit comments

Comments
 (0)