rcolyer.net
RC Lib  Version 202403231100
Examples
1 // tolower.cpp - Take one string from the command line and make it lowercase.
2 
3 #include <RC/RC.h>
4 
5 // This provides using namespace for RC and std.
6 // It is recommended to use it for fast prototyping, but only in cpp files.
7 RC_USE
8 
9 // This wraps argc and argv as RC::Data1D<RC::RStr> args.
10 RC_MAIN {
11  if (args.size() < 2) {
12  cerr << args[0] << " <string>\n";
13  return -1;
14  }
15 
16  RC::RStr str = args[1];
17 
18  // For each character
19  for (char &c : str) {
20  // If it's between 'A' and 'Z' inclusive
21  if ('A' <= Betw(c) <= 'Z') {
22  // Make it lowercase.
23  c += 'a' - 'A';
24  }
25  }
26 
27  cout << str << endl;
28 
29  return 0;
30 }
31 
The main RC Library include file, which includes all other components.
#define RC_USE
Place this shorthand macro after including RC.h to use namespaces RC and std.
Definition: RC.h:50
#define RC_MAIN
This macro wraps int main so that command line arguments are placed into RC::Data1D<RC::RStr> args.
Definition: RC.h:61
A bounds-safe string class which provides an identical interface to std::string plus many convenience...
Definition: RStr.h:110
BetwCompare< T > Betw(const T &x)
Returns a comparator that can be used for range comparisons.
Definition: RCBits.h:69

1 // battery.cpp - Determine the battery state from the Linux power_supply files.
2 
3 #include "RC/RC.h"
4 
5 RC_USE
6 
7 const RStr dir_base = "/sys/class/power_supply/";
8 
9 // Read a line from a file.
10 RStr GetFile(const RStr& filename) {
11  // Concatenate the strings.
12  RStr pathname = dir_base + filename;
13 
14  // Open the file for reading.
15  FileRead br(pathname);
16 
17  // Get one line as an RStr
18  RStr retval;
19  br.Get(retval);
20 
21  return retval;
22 }
23 
24 RC_MAIN {
25  // Output whether or not the AC is plugged in.
26  // The RC::RStr::Get_bool checks for true, T, or a non-zero number.
27  cout << "AC is " << (GetFile("AC/online").Get_bool() ? "on" : "off") << endl;
28 
29  // Read the current, full, and design-peak energy levels.
30  // Unsigned integers are extracted from the RStr.
31  u64 current = GetFile("BAT0/energy_now").Get_u64();
32  u64 lastfull = GetFile("BAT0/energy_full").Get_u64();
33  u64 designfull = GetFile("BAT0/energy_full_design").Get_u64();
34 
35  // Calculate the percent full the battery is.
36  u32 perc = 100 * current / (f64) lastfull + 0.5;
37  // Calculate how close to ideal the battery is.
38  f64 quality = lastfull / (f64) designfull;
39 
40  cout << "Battery: " << perc << "%" << endl;
41  cout << "Quality: " << RStr(quality,FIXED,2) << endl;
42 
43  return 0;
44 }
uint64_t u64
64-bit unsigned integer.
Definition: Types.h:31
double f64
64-bit float.
Definition: Types.h:34
uint32_t u32
32-bit unsigned integer.
Definition: Types.h:29

1 // randomize_lines.cpp - Shuffle the lines from stdin.
2 
3 #include <RC/RC.h>
4 
5 RC_USE
6 
7 RC_MAIN {
8  size_t max_lines_out = -1; // Max size_t
9 
10  // If there are more arguments than the program name.
11  if (args.size() > 1) {
12  // Check to see if a number was given.
13  if (args[1].Is_u64()) {
14  // If so, get it.
15  max_lines_out = args[1].Get_u64();
16  }
17  else {
18  // If the argument was not a number, print the help.
19  cerr << args[0] << " [max_lines_out]\n";
20  return -1;
21  }
22  }
23 
24  // Open stdin as a FileRead
25  FileRead fr(stdin, false);
26 
27  // Get all the lines until end-of-file.
28  Data1D<RStr> lines;
29  fr.GetAll(lines);
30 
31  // Randomize the order of the lines.
32  lines.Shuffle();
33 
34  // Shrink the array if we should output fewer lines.
35  // Note this does not reallocate.
36  if (lines.size() > max_lines_out) {
37  lines.Resize(max_lines_out);
38  }
39 
40  // Output the remaining lines.
41  for (RStr line : lines) {
42  cout << line << endl;
43  }
44 
45  return 0;
46 }
47 

1 // parse_and_sum.cpp - Parse and process some comma-separated values.
2 
3 #include <RC/RC.h>
4 
5 RC_USE
6 
7 RC_MAIN {
8  RStr numstr = "3.14, 5.7, 8, 12";
9 
10  // Split the comma-separated values into an array.
11  Data1D<RStr> str_arr = numstr.SplitCSV();
12 
13  // Sum the array of numbers
14  f64 sum = 0;
15  for (RStr x : str_arr) {
16  sum += x.Get_f64(); // Convert to a 64-bit float.
17  }
18 
19  // Outputs "3.14 + 5.7 + 8 + 12 = 28.84"
20  cout << RStr::Join(str_arr, " + ") << " = " << sum << endl;
21 
22  return 0;
23 }

1 // random_average.cpp - Generate and average 5000 numbers.
2 
3 #include <RC/RC.h>
4 
5 RC_USE
6 
7 RC_MAIN {
8  // Seeds the random number generator with the high precision clock.
9  RND rng;
10  // Initializes the timer.
11  Time timer;
12 
13  // Appends 5000 random 64-bit floats from [0,1) to vals.
14  Data1D<f64> vals;
15  for (u64 r=0; r<5000; r++) {
16  vals += rng.Get_f64();;
17  }
18 
19  // Calculates the average.
20  f64 avg=0;
21  for (size_t i=0; i<vals.size(); i++) {
22  avg += vals[i];
23  }
24  avg /= vals.size();
25 
26  // Reports how long it took, to a fraction of a second.
27  cout << "It took " << timer.SinceStart() << " seconds.\n";
28 
29  // A flushed stderr printout of "random_average.cpp:30, avg = 0.496605"
30  RC_DEBOUT(avg);
31 
32  // Reports if the avg was in an acceptable range.
33  cout << "The average was " << (0.48 < Betw(avg) < 0.52 ? "good\n" : "bad\n");
34 
35  return 0;
36 }
#define RC_DEBOUT(...)
Use RC_DEBOUT(var1, ..., varN) for a flushed stderr debug printout of variables.
Definition: Macros.h:167

1 // callback.cpp - Processes a callback with a Caller, and returns a Tuple.
2 
3 #include <RC/RC.h>
4 
5 RC_USE
6 
7 class Doer {
8  public:
9 
10  Tuple<u16, RStr> DoStuff(Caller<void, f32> callback) {
11  RND rng;
12  f32 val = rng.Get_f32();
13 
14  // Because of what was passed in, this will call w.CallBack, even
15  // though it knows nothing of the Waiter class.
16  callback(val);
17 
18  // Implicitly returns a Tuple.
19  return {rng.Get_u16(), "apples"};
20  }
21 };
22 
23 
24 class Waiter {
25  public:
26 
27  void CallIt(Doer &d) {
28  // Pass a Caller<void, f32> to DoStuff, and receive a Tuple back.
29  auto tup = d.DoStuff(MakeCaller(this, &Waiter::CallBack));
30 
31  // Alternatively one can construct the Caller like this.
32  auto tup2 = d.DoStuff({this, &Waiter::CallBack});
33 
34  // Prints a value like "It returned 51813 apples"
35  cout << "It returned " << tup.Get<0>() << " " << tup.Get<1>() << endl;
36  }
37 
38  // Receives a call back from DoStuff
39  void CallBack(f32 x) {
40  cout << "I got x == " << x << endl;
41  }
42 };
43 
44 
45 RC_MAIN {
46  Doer d;
47  Waiter w;
48 
49  w.CallIt(d);
50 
51  return 0;
52 }
53 
float f32
32-bit float.
Definition: Types.h:33
Caller< Ret, Params... > MakeCaller(C *obj, Ret(C::*func)(Params...))
Generates a Caller for the member function of the given object, with type inference.
Definition: Caller.h:241
email address
— (c) 2015