Updated Apr 19th, 2013
Cpp

c++

Questions

What is the difference between getting &buffer and &buffer[0]?

How does serial read and write really work?

cu vs. tty?

How to use STD

Using Boost

search paths
header search paths
    /usr/local/Cellar/boost/1.52.0/include/
    non-recursive
library search path
    /usr/local/Cellar/boost/1.52.0/lib
    recursive
other linking flags
    -lboost_regex-mt
-L/usr/local/Cellar/boost/1.52.0/lib

=======

Libraries

Regex

include c++0x

Reference

int // 32 bits

short int // 16 bits

unsigned

  • unsigned int

Struct

struct product {
    int weight;
    float price;
};

product something;
something.weight;

Vector

.push_back

array

int something [5];
int something [5] = { 3, 4, 5}

extern

tell compiler this variable is declared elsewhere.

Vectors

Unlike arrays, storage is handled automatically, expanded and contracted as needed.

Stack

Last in first out (lifo)

List

Sequence container Ordered following a sequence. In order to get to a position, must iterate from beginning or end

Singleton

class Log {
public:
  static Log& Instance() {
    static Log theLog;
    return theLog;
}
  void Write(char const *logline);
  bool SaveTo(char const *filename);
private:
  Log();          // ctor is hidden
  Log(Log const&);      // copy ctor is hidden
  Log& operator=(Log const&);  // assign op is hidden

  static std::list<std::string> m_data;
};

[Read more][]
[Read more]: http://www.devarticles.com/c/a/Cplusplus/C-plus-plus-In-Theory-The-Singleton-Pattern-Part-I/4/#PR3d1CTSfUXK2E0r.99

Classes

class ExtendedClass {
    virtual void Start() = 0;
};  // DON'T FORGET SEMICOLON

class Test : public ExtendedClass {
    private: (is default)
    protected:
    public: 
    Test (int n, int y);
    void Start() {

    }
};

Test something (3, 5);

ExtendedClass[] my extendedclasses { new Test(), new Test2}

Enumerators

enum actions {nothing, walking, working}

Switch

switch(something) {
    case 1:
    break;
    default:
    break;
}

Pointers

&x

  • address of x
  • reference to x
  • get the memory address of x, don't care about what's actually there.
  • points at some value, gives us the street address of it.

*x

  • dereference x
  • goto the address x, get or set the value there
  • x MUST be a memory address
  • get value pointed at by x
  • go to the street address, get the value from
  • c->doThis() is equivlent to (*c).doThis();

char * x

  • declare pointer as pointing to a value with the type char
  • Has nothing to do with dereferencing!
  • HOWEVER, the actual pointer is going to be storing just the address. NOT the type of the value pointed to. All pointers take up the same amount of space.
  • You can change these pointers to point to memory addresses. int *tommy = &number;

offset operator

[] is a dereferencer that offsets by number in brackets!
p[4] == *(p+4);

arrays

  • array identifiers are equivlent to the address of the first element
  • array identifiers are constant pointers, can not be altered
  • [] is a dereference operator which offsets by the number in brackets! int numbers [30]; int * p; p = numbers; // this is valid *p = 10; //changes numbers[0] p++; *p = 20 // changes numbers[1] p = &numbers[2]; p = numbers + 3;

void pointers

void * ptr;

- can point to any hardware address, but can not dereference directly. - You can use if (x == sizeof(char)) to see if it's a char and then char * ptr = (char*) void_ptr; to cast it - can be used for generics

pointers to functions allows references functions to be sent as variables, dereferenced and ran int doSomething(int x, int y, int (* something)(int,int)) { return (* something)(x, y); doSomething(x,y, add); }

Reference

-   Don't have to dereference a reference, just pointers.
-   references are constant pointers, can not be reassigned!
-   must be initialized
-   can not increment through memory

int & something = x;
something = 3;

Templates

templates are generics

template <class identifier> function_declaration;

template <typename identifier> function_declaration;

template <class T> 
T GetMax (T a, T b) {}
    T result = (a>b)? a : b;
    return (result);
}

int k = GetMax<int>(1,2) 
long r = GetMax<int>(1,2) 

Other

#define PI 3.14159

const

can not be modified after declaration

return 0; // SUCCESS
return -1; // fail

std::list<std::string> data;

std::vector<Game_State*> mStateStack;

#include <iostream>

Serial

Byte

stores 8 bit unsigned int

uint8_t

Reset issue

http://stackoverflow.com/questions/11385915/serial-interface-initialisation-with-arduino-and-c

Oh my god, this is the most annoying thing in the world.

http://www.easysw.com/~mike/serial/serial.html

Comprehensive.

http://en.wikipedia.org/wiki/SerialPeripheralInterface_Bus

http://wiring.org.co/learning/tutorials/C++/

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1284666138/all

O_RDONLY - read only

O_WRONLY - write only

O_RDWR - read and write

O_NONBLOCK - non blocking

O_NOCTTY - don't make controlling

fd - common name for variable "file descriptor"

tcsetattr - set attributes

TCSANOW - TC set attributes now

termios - unix api for terminal i/o

c_cflags - control flags

c_lflags - local flags

readv?

Arduino

http://www.instructables.com/id/Sending-a-multi-byte-integer-to-Arduinos-serial/ stringOne = String(45, HEX);
// prints "2d", which is the hexadecimal version of decimal 45: Serial.println(stringOne);
String(255, BIN);

Example:

int fd;
ssize_t len;
unsigned char buf[8192]; // 8 kb (use a multiple of 512)
struct termios tio;

fd = open( "/dev/tty.usbmodem411", O_RDONLY | O_NOCTTY | O_NONBLOCK );
cfmakeraw(&tio);
cfsetispeed(&tio,B9600);
cfsetospeed(&tio,B9600);
tcsetattr(fd,TCSANOW,&tio);

for(;;) {
    len = read( fd, &buf[0], 8192 );
    std::cout << len;
    if( len > 0 ) write(1,buf,len);
    sleep(1);
}

so annoying that I can't just take the buffer and read it how I want...

Errors

Switch case is in protected scope

C++ is confused. Either don't initialize variables in a case, or wrap it in brackets in the case.

Engines

  • IndieLib C++ 2d -- direct3d (windows only)
  • sfml
  • sdl