My Project
Loading...
Searching...
No Matches
lintree.h
Go to the documentation of this file.
1#ifndef _singthread_lintree_h
2#define _singthread_lintree_h
3
4#include <string>
5#include <vector>
6#include <cstring>
7#include <Singular/ipid.h>
8
9namespace LinTree {
10
11class LinTree;
12
13typedef void (*LinTreeEncodeFunc)(LinTree &lintree, leftv val);
14typedef leftv (*LinTreeDecodeFunc)(LinTree &lintree);
15typedef void (*LinTreeRefFunc)(LinTree &lintree, int by);
16
17extern std::vector<LinTreeEncodeFunc> encoders;
18extern std::vector<LinTreeDecodeFunc> decoders;
19extern std::vector<LinTreeRefFunc> refupdaters;
20
21void install(int typ, LinTreeEncodeFunc enc, LinTreeDecodeFunc dec,
22 LinTreeRefFunc ref);
23
24class LinTree {
25private:
26 std::string &memory;
27 size_t cursor;
28 const char * error;
29 void *last_ring;
30public:
31 LinTree();
32 LinTree(const LinTree &other);
33 ~LinTree();
34 LinTree(std::string &source);
35 LinTree& operator=(const LinTree &other);
36 void rewind() { cursor = 0; }
37 void clear() { memory.clear(); cursor = 0; error = NULL; last_ring = NULL; }
38 void mark_error(const char *s) {
39 error = s;
40 }
41 int has_error() {
42 return error != NULL;
43 }
44 const char *error_msg() {
45 return error;
46 }
47 template<typename T>
48 T get() {
49 T result;
50 memcpy(&result, memory.c_str() + cursor, sizeof(T));
51 cursor += sizeof(T);
52 return result;
53 }
54 template<typename T>
56 T result;
57 memcpy(&result, memory.c_str() + cursor - sizeof(T), sizeof(T));
58 return result;
59 }
60 template<typename T>
61 void put(T data) {
62 memory.append((const char *) &data, sizeof(T));
63 }
64 template<typename T>
65 void skip() {
66 cursor += sizeof(T);
67 }
68 int get_int() {
69 return get<int>();
70 }
71 size_t get_size() {
72 return get<size_t>();
73 }
74 void put_int(int code) {
75 put(code);
76 }
77 void skip_int() {
78 skip<int>();
79 }
80 const char *get_bytes(size_t n) {
81 const char *result = memory.c_str() + cursor;
82 cursor += n;
83 return result;
84 }
85 const char *get_addr() {
86 return memory.c_str() + cursor;
87 }
88 void put_bytes(char *p, size_t n) {
89 memory.append(p, n);
90 }
91 char *reserve_bytes(size_t n) {
92 size_t pos = memory.size();
93 memory.reserve(n);
94 for (size_t i = 0; i < n; i++) {
95 memory += '\0';
96 }
97 return (char *)(memory.c_str() + pos);
98 }
99 void put_cstring(char *p) {
100 size_t n = strlen(p);
101 put(n);
102 put_bytes(p, n+1);
103 }
104 const char *get_cstring() {
105 size_t n = get_size();
106 const char *result = memory.c_str() + cursor;
107 cursor += n + 1;
108 return result;
109 }
111 size_t n = get_size();
112 cursor += n + 1;
113 }
114 void skip_bytes(size_t n) {
115 cursor += n;
116 }
117 std::string &to_string() {
118 return memory;
119 }
120 void set_last_ring(void *r);
122 return last_ring != NULL;
123 }
125 return last_ring;
126 }
127};
128
129std::string to_string(leftv val);
130leftv from_string(std::string &str);
131
132void init();
133
134};
135
136#endif
int i
Definition: cfEzgcd.cc:132
int p
Definition: cfModGcd.cc:4078
void * last_ring
Definition: lintree.h:29
const char * error
Definition: lintree.h:28
void put_bytes(char *p, size_t n)
Definition: lintree.h:88
int get_int()
Definition: lintree.h:68
std::string & to_string()
Definition: lintree.h:117
void skip_int()
Definition: lintree.h:77
void skip_bytes(size_t n)
Definition: lintree.h:114
void skip()
Definition: lintree.h:65
void rewind()
Definition: lintree.h:36
void put_cstring(char *p)
Definition: lintree.h:99
void * get_last_ring()
Definition: lintree.h:124
void clear()
Definition: lintree.h:37
size_t get_size()
Definition: lintree.h:71
char * reserve_bytes(size_t n)
Definition: lintree.h:91
void put(T data)
Definition: lintree.h:61
const char * get_bytes(size_t n)
Definition: lintree.h:80
void put_int(int code)
Definition: lintree.h:74
int has_last_ring()
Definition: lintree.h:121
std::string & memory
Definition: lintree.h:26
void mark_error(const char *s)
Definition: lintree.h:38
const char * get_cstring()
Definition: lintree.h:104
int has_error()
Definition: lintree.h:41
size_t cursor
Definition: lintree.h:27
const char * get_addr()
Definition: lintree.h:85
const char * error_msg()
Definition: lintree.h:44
void skip_cstring()
Definition: lintree.h:110
Class used for (list of) interpreter objects.
Definition: subexpr.h:83
return result
Definition: facAbsBiFact.cc:75
const CanonicalForm int s
Definition: facAbsFact.cc:51
STATIC_VAR jList * T
Definition: janet.cc:30
#define error(a)
Definition: mpr_numeric.cc:966
void init()
Definition: lintree.cc:864
vector< LinTreeDecodeFunc > decoders
Definition: lintree.cc:47
void(* LinTreeRefFunc)(LinTree &lintree, int by)
Definition: lintree.h:15
std::string to_string(leftv val)
Definition: lintree.cc:843
leftv(* LinTreeDecodeFunc)(LinTree &lintree)
Definition: lintree.h:14
vector< LinTreeRefFunc > refupdaters
Definition: lintree.cc:48
vector< LinTreeEncodeFunc > encoders
Definition: lintree.cc:46
void install(int typ, LinTreeEncodeFunc enc, LinTreeDecodeFunc dec, LinTreeRefFunc ref)
Definition: lintree.cc:51
leftv from_string(std::string &str)
Definition: lintree.cc:854
void(* LinTreeEncodeFunc)(LinTree &lintree, leftv val)
Definition: lintree.h:13
#define NULL
Definition: omList.c:12
sleftv * leftv
Definition: structs.h:57