BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface software
object.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cryptonomex, Inc., and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #pragma once
25 #include <boost/multiprecision/integer.hpp>
27 #include <fc/io/raw.hpp>
28 #include <fc/crypto/city.hpp>
29 
30 #define MAX_NESTING (200)
31 
32 namespace graphene { namespace db {
33 
62  class object
63  {
64  public:
65  object(){}
66  virtual ~object(){}
67 
68  // serialized
70 
72  virtual unique_ptr<object> clone()const = 0;
73  virtual void move_from( object& obj ) = 0;
74  virtual variant to_variant()const = 0;
75  virtual vector<char> pack()const = 0;
76  };
77 
85  template<typename DerivedClass>
86  class abstract_object : public object
87  {
88  public:
89  virtual unique_ptr<object> clone()const
90  {
91  return unique_ptr<object>(new DerivedClass( *static_cast<const DerivedClass*>(this) ));
92  }
93 
94  virtual void move_from( object& obj )
95  {
96  static_cast<DerivedClass&>(*this) = std::move( static_cast<DerivedClass&>(obj) );
97  }
98  virtual variant to_variant()const { return variant( static_cast<const DerivedClass&>(*this), MAX_NESTING ); }
99  virtual vector<char> pack()const { return fc::raw::pack( static_cast<const DerivedClass&>(*this) ); }
100  };
101 
102  typedef flat_map<uint8_t, object_id_type> annotation_map;
103 
108  template<typename DerivedClass>
109  class annotated_object : public abstract_object<DerivedClass>
110  {
111  public:
113  object_id_type get_annotation( uint8_t annotation_id_space )const
114  {
115  auto itr = annotations.find(annotation_id_space);
116  if( itr != annotations.end() ) return itr->second;
117  return object_id_type();
118  }
120  {
121  annotations[id.space()] = id;
122  }
123 
128  annotation_map annotations;
129  };
130 
131 } } // graphene::db
132 
133 // Without this, pack(object_id) tries to match the template for
134 // pack(boost::multiprecision::uint128_t). No idea why. :-(
135 namespace boost { namespace multiprecision { namespace detail {
136 template<typename To>
137 struct is_restricted_conversion<graphene::db::object,To> : public mpl::true_ {};
138 }}}
139 
virtual unique_ptr< object > clone() const =0
these methods are implemented for derived classes by inheriting abstract_object<DerivedClass> ...
void pack(Stream &s, const flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:11
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition: reflect.hpp:386
Definition: api.cpp:56
virtual vector< char > pack() const
Definition: object.hpp:99
virtual vector< char > pack() const =0
annotation_map annotations
Definition: object.hpp:128
virtual void move_from(object &obj)
Definition: object.hpp:94
FC_REFLECT_TYPENAME(fc::log_message)
object_id_type id
Definition: object.hpp:69
virtual unique_ptr< object > clone() const
these methods are implemented for derived classes by inheriting abstract_object<DerivedClass> ...
Definition: object.hpp:89
void set_annotation(object_id_type id)
Definition: object.hpp:119
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
Use the Curiously Recurring Template Pattern to automatically add the ability to clone, serialize, and move objects polymorphically.
Definition: object.hpp:86
object_id_type get_annotation(uint8_t annotation_id_space) const
Definition: object.hpp:113
virtual variant to_variant() const =0
#define MAX_NESTING
Definition: object.hpp:30
An object that is easily extended by providing pointers to other objects, one for each space...
Definition: object.hpp:109
virtual void move_from(object &obj)=0
virtual ~object()
Definition: object.hpp:66
#define FC_REFLECT_DERIVED_TEMPLATE(TEMPLATE_ARGS, TYPE, INHERITS, MEMBERS)
Definition: reflect.hpp:329
base for all database objects
Definition: object.hpp:62
virtual variant to_variant() const
Definition: object.hpp:98
flat_map< uint8_t, object_id_type > annotation_map
Definition: object.hpp:102