Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
fixed_string.hpp
Go to the documentation of this file.
1#pragma once
2#include <fc/io/raw_fwd.hpp>
3
4
5namespace fc {
6
7
16 template<typename Storage = std::pair<uint64_t,uint64_t> >
17 class fixed_string {
18 public:
20 memset( (char*)&data, 0, sizeof(data) );
21 }
22 fixed_string( const fixed_string& c ):data(c.data){}
23
24 fixed_string( const std::string& str ) {
25 if( str.size() < sizeof(data) ) {
26 memset( (char*)&data, 0, sizeof(data) );
27 memcpy( (char*)&data, str.c_str(), str.size() );
28 } else {
29 memcpy( (char*)&data, str.c_str(), sizeof(data) );
30 }
31 }
32 fixed_string( const char* str ) {
33 memset( (char*)&data, 0, sizeof(data) );
34 auto l = strlen(str);
35 if( l < sizeof(data) ) {
36 memset( (char*)&data, 0, sizeof(data) );
37 memcpy( (char*)&data, str, l );
38 }
39 else {
40 memcpy( (char*)&data, str, sizeof(data) );
41 }
42 }
43
44 operator std::string()const {
45 const char* self = (const char*)&data;
46 return std::string( self, self + size() );
47 }
48
49 uint32_t size()const {
50 if( *(((const char*)&data)+sizeof(data) - 1) )
51 return sizeof(data);
52 return strnlen( (const char*)&data, sizeof(data) );
53 }
54 uint32_t length()const { return size(); }
55
57 data = str.data;
58 return *this;
59 }
60 fixed_string& operator=( const char* str ) {
61 return *this = fixed_string(str);
62 }
63
64 fixed_string& operator=( const std::string& str ) {
65 if( str.size() < sizeof(data) ) {
66 memset( (char*)&data, 0, sizeof(data) );
67 memcpy( (char*)&data, str.c_str(), str.size() );
68 }
69 else {
70 memcpy( (char*)&data, str.c_str(), sizeof(data) );
71 }
72 return *this;
73 }
74
75 friend std::string operator + ( const fixed_string& a, const std::string& b ) {
76 return std::string(a) + b;
77 }
78 friend std::string operator + ( const std::string& a, const fixed_string& b ) {
79 return a + std::string(b);
80 }
81
82 friend bool operator < ( const fixed_string& a, const fixed_string& b ) {
83 return a.data < b.data;
84 }
85 friend bool operator <= ( const fixed_string& a, const fixed_string& b ) {
86 return a.data <= b.data;
87 }
88 friend bool operator > ( const fixed_string& a, const fixed_string& b ) {
89 return a.data > b.data;
90 }
91 friend bool operator >= ( const fixed_string& a, const fixed_string& b ) {
92 return a.data >= b.data;
93 }
94 friend bool operator == ( const fixed_string& a, const fixed_string& b ) {
95 return a.data == b.data;
96 }
97 friend bool operator != ( const fixed_string& a, const fixed_string& b ) {
98 return a.data != b.data;
99 }
100
101 friend std::ostream& operator << ( std::ostream& out, const fixed_string& str ) {
102 return out << std::string(str);
103 }
104 //private:
105 Storage data;
106 };
107
108 namespace raw
109 {
110 template<typename Stream, typename Storage>
111 inline void pack( Stream& s, const fc::fixed_string<Storage>& u ) {
112 unsigned_int size = u.size();
113 pack( s, size );
114 s.write( (const char*)&u.data, size );
115 }
116
117 template<typename Stream, typename Storage>
119 unsigned_int size;
120 fc::raw::unpack( s, size );
121 if( size.value > 0 ) {
122 if( size.value > sizeof(Storage) ) {
123 s.read( (char*)&u.data, sizeof(Storage) );
124 char buf[1024];
125 size_t left = size.value - sizeof(Storage);
126 while( left >= 1024 )
127 {
128 s.read( buf, 1024 );
129 left -= 1024;
130 }
131 s.read( buf, left );
132
133 /*
134 s.seekp( s.tellp() + (size.value - sizeof(Storage)) );
135 char tmp;
136 size.value -= sizeof(storage);
137 while( size.value ){ s.read( &tmp, 1 ); --size.value; }
138 */
139 // s.skip( size.value - sizeof(Storage) );
140 } else {
141 s.read( (char*)&u.data, size.value );
142 }
143 }
144 }
145
146 /*
147 template<typename Stream, typename... Args>
148 inline void pack( Stream& s, const boost::multiprecision::number<Args...>& d ) {
149 s.write( (const char*)&d, sizeof(d) );
150 }
151
152 template<typename Stream, typename... Args>
153 inline void unpack( Stream& s, boost::multiprecision::number<Args...>& u ) {
154 s.read( (const char*)&u, sizeof(u) );
155 }
156 */
157 }
158}
159
160#include <fc/variant.hpp>
161namespace fc {
162 template<typename Storage>
164 v = std::string(s);
165 }
166
167 template<typename Storage>
169 s = v.as_string();
170 }
171}
uint32_t size() const
friend bool operator>(const fixed_string &a, const fixed_string &b)
fixed_string & operator=(const std::string &str)
fixed_string & operator=(const char *str)
friend bool operator<(const fixed_string &a, const fixed_string &b)
fixed_string(const char *str)
fixed_string & operator=(const fixed_string &str)
uint32_t length() const
friend bool operator==(const fixed_string &a, const fixed_string &b)
friend bool operator<=(const fixed_string &a, const fixed_string &b)
friend std::ostream & operator<<(std::ostream &out, const fixed_string &str)
friend std::string operator+(const fixed_string &a, const std::string &b)
friend bool operator>=(const fixed_string &a, const fixed_string &b)
fixed_string(const std::string &str)
friend bool operator!=(const fixed_string &a, const fixed_string &b)
fixed_string(const fixed_string &c)
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
string as_string() const
Definition variant.cpp:469
Concept for reading and writing characters.
void unpack(Stream &s, std::deque< T > &value)
Definition raw.hpp:540
void pack(Stream &s, const std::deque< T > &value)
Definition raw.hpp:531
namespace sysio::chain
Definition authority.cpp:3
void from_variant(const fc::variant &v, sysio::chain::chain_id_type &cid)
void to_variant(const sysio::chain::shared_public_key &var, fc::variant &vo)
Definition authority.cpp:4
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
unsigned int uint32_t
Definition stdint.h:126
uint32_t value
Definition varint.hpp:17
char * s
uint8_t buf[2048]
int l
memset(pInfo->slotDescription, ' ', 64)
memcpy((char *) pInfo->slotDescription, s, l)