feisty meow concerns codebase 2.140
crompish_pax.h
Go to the documentation of this file.
1#ifndef CROMPISH_PAX_GROUP
2#define CROMPISH_PAX_GROUP
3
4/*****************************************************************************\
5* *
6* Name : crompish packets for tester *
7* Author : Chris Koeritz *
8* *
9* Purpose: *
10* *
11* Some simple transactions that can be used for the CROMP tester. *
12* *
13*******************************************************************************
14* Copyright (c) 2002-$now By Author. This program is free software; you can *
15* redistribute it and/or modify it under the terms of the GNU General Public *
16* License as published by the Free Software Foundation; either version 2 of *
17* the License or (at your option) any later version. This is online at: *
18* http://www.fsf.org/copyleft/gpl.html *
19* Please send any updates to: fred@gruntose.com *
20\*****************************************************************************/
21
24#include <mathematics/chaos.h>
25#include <octopus/infoton.h>
28
29// uncomment to cause more checking that verifies the packed_size method
30// is reporting accurately.
31//#define CHECK_CROMPISH_PACKED_SIZE
32
33class bubble : public octopi::infoton
34{
35public:
36 bubble(int data_segment_size = 0, const structures::string_array &boundaries
37 = structures::string_array(), int color = 0)
38 // constructs a bubble within the "boundaries" that has "color" and a data
39 // segment size specified by "data_segment_size". the color definitions
40 // reside elsewhere.
41 : infoton(bubble_classing()), _data()
42 { reset(data_segment_size, boundaries, color); }
43
45 static basis::astring bubbs[2] = { "bubble", "rubble" };
46 static structures::string_array barray(2, bubbs);
47 return barray;
48 }
49
50 void reset(int data_segment_size, const structures::string_array &boundaries,
51 int color) {
52 _color = color;
53 _bounds = boundaries;
54 _data.reset(data_segment_size);
55 }
56
57 int data_length() const { return _data.length(); }
58
59 virtual clonable *clone() const { return octopi::cloner<bubble>(*this); }
60
61 basis::byte_array &data() { return _data; }
62
63 int non_data_overhead() const { return packed_size() - _data.length(); }
64
65 virtual void text_form(basis::base_string &to_show) const {
67 to_show.assign(basis::a_sprintf("classing=%s, seg size=%d, color=%d, bounds=%s",
68 bubs.text_form().s(), _data.length(), _bounds.text_form().s(), _color));
69 }
70
71 virtual void pack(basis::byte_array &packed_form) const {
72 FUNCDEF("pack")
73#ifdef CHECK_CROMPISH_PACKED_SIZE
74 int prior_len = packed_form.length();
75#endif
76 structures::attach(packed_form, _color);
77 _bounds.pack(packed_form);
78 structures::attach(packed_form, _data);
79#ifdef CHECK_CROMPISH_PACKED_SIZE
80 int predicted_size = packed_size();
81 int new_len = packed_form.length();
82 if (prior_len + predicted_size != new_len) {
83 loggers::deadly_error(class_name(), func, basis::a_sprintf("size predicted=%d but actually was %d", predicted_size, new_len - prior_len));
84 }
85#endif
86 }
87
88 int packed_size() const {
89 return _data.length() + 2 * sizeof(int) // packed byte array.
90 + sizeof(int) // packed color.
91 + _bounds.packed_size(); // packed string array.
93 }
94
95 virtual bool unpack(basis::byte_array &packed_form) {
96 if (!structures::detach(packed_form, _color)) return false;
97 if (!_bounds.unpack(packed_form)) return false;
98 if (!structures::detach(packed_form, _data)) return false;
99 return true;
100 }
101
102private:
104 int _color;
105 basis::byte_array _data;
106};
107
109
111{
112public:
113 bubbles_tentacle(bool backgrounded)
114 : octopi::tentacle_helper<bubble>(bubble().classifier(), backgrounded)
115 {}
116};
117
118#endif
119
a_sprintf is a specialization of astring that provides printf style support.
Definition astring.h:440
void reset(int number=0, const contents *initial_contents=NULL_POINTER)
Resizes this array and sets the contents from an array of contents.
Definition array.h:349
int length() const
Returns the current reported length of the allocated C array.
Definition array.h:115
Provides a dynamically resizable ASCII character string.
Definition astring.h:35
const char * s() const
synonym for observe. the 's' stands for "string", if that helps.
Definition astring.h:113
Defines the base class for all string processing objects in hoople.
Definition base_string.h:28
virtual base_string & assign(const base_string &s)=0
Sets the contents of this string to "s".
A very common template for a dynamic array of bytes.
Definition byte_array.h:36
virtual const char * class_name() const =0
Returns the bare name of this class as a constant character pointer.
int data_length() const
const structures::string_array & bubble_classing() const
virtual clonable * clone() const
must be provided to allow creation of a copy of this object.
bubble(int data_segment_size=0, const structures::string_array &boundaries=structures::string_array(), int color=0)
virtual bool unpack(basis::byte_array &packed_form)
restores an infoton from a packed form.
virtual void text_form(basis::base_string &to_show) const
requires derived infotons to be able to show their state as a string.
int packed_size() const
reports how large the infoton will be when packed.
basis::byte_array & data()
void reset(int data_segment_size, const structures::string_array &boundaries, int color)
int non_data_overhead() const
virtual void pack(basis::byte_array &packed_form) const
stuffs the data in the infoton into the "packed_form".
bubbles_tentacle(bool backgrounded)
An infoton is an individual request parcel with accompanying information.
Definition infoton.h:32
infoton(const structures::string_array &classifier)
creates an infoton with the "classifier".
Definition infoton.cpp:41
provides prefab implementations for parts of the tentacle object.
tentacle_helper(const structures::string_array &classifier, bool backgrounded, int motivational_rate=tentacle::DEFAULT_RATE)
An array of strings with some additional helpful methods.
virtual void pack(basis::byte_array &packed_form) const
Packs this string array into the "packed_form" byte array.
virtual bool unpack(basis::byte_array &packed_form)
Unpacks a string array from the "packed_form" byte array.
virtual int packed_size() const
Returns the number of bytes this string array would consume if packed.
basis::astring text_form() const
A synonym for the text_format() method.
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition enhance_cpp.h:54
void attach(byte_array &packed_form, const byte_array &to_attach)
Packs a byte_array "to_attach" into "packed_form".
bool detach(byte_array &packed_form, byte_array &to_detach)
Unpacks a byte_array "to_detach" from "packed_form".
Automates some common tasks for tentacle implementations. This template provides some default impleme...