Monday, May 4, 2015

Mountain Unicycling

Mountain Unicycling is the best sport I have every done. By far the most energy expended for the amount of time spent ratio. Check out this video if you are at all interested and want a challenge!


Thursday, April 30, 2015

Homeland Security wants less Encryption

The full text of the speech given by the Remarks by Secretary of Homeland Security Jeh Johnson at the RSA Conference 2015 can be found here. I think it is very telling where Homeland Security is from this small except:

"Now, finally, I have an ask: for your indulgence and your understanding on the subject of encryption. 
The Department of Homeland Security has both the cybersecurity mission and a law enforcement/counterterrorism mission for the American people. We have feet in both camps. I therefore believe I have a good perspective on this issue. 
The current course we are on, toward deeper and deeper encryption in response to the demands of the marketplace, is one that presents real challenges for those in law enforcement and national security.
Let me be clear: I understand the importance of what encryption brings to privacy. But, imagine the problems if, well after the advent of the telephone, the warrant authority of the government to investigate crime had extended only to the U.S. mail.
Our inability to access encrypted information poses public safety challenges. 
In fact, encryption is making it harder for your government to find criminal activity, and potential terrorist activity. 
We in government know that a solution to this dilemma must take full account of the privacy rights and expectations of the American public, the state of the technology, and the cybersecurity of American businesses."
First of all, uh, no. Second, are you kidding? The government has mountains of information in the form of metadata that they need absolutly no warrant for. They want back doors which make the encryption pointless. Maybe it's time some smart Silicon Valley engineers work for them rather than just VPs and Execs getting cushy jobs. Just sayin'. If other industries can do it like Monsonto with the FDA why not us?

Tuesday, April 28, 2015

Properties in C++ Part II



Last week I posed about making properties in C++. That code sample had some issues that I fine tuned with the code sample in this post. Here is the new class(s).

template <typename ObjectType, typename ValueType, ValueType (ObjectType::*getter)(void), void (ObjectType::*setter)(ValueType)>
class Property {
private:
    ObjectType* FObject;
    
public:
    Property() {
        FObject = NULL;
    }
    
    void SetInstance(ObjectType* Value) {
        FObject = Value;
    }
    
    // To set the value using the set method.
    ValueType operator =(const ValueType& Value) {
        assert(FObject != NULL);
        (FObject->*setter)(Value);
        return Value;
    }
    
    // The Property class is treated as the internal type.
    operator ValueType() {
        assert(FObject != NULL);
        return (FObject->*getter)();
    }
};

template <typename ObjectType, typename ValueType, ValueType (ObjectType::*getter)(void)>
class ReadProperty {
private:
    ObjectType* FObject;
    
public:
    ReadProperty() {
        FObject = NULL;
    }
    
    void SetInstance(ObjectType* Value) {
        FObject = Value;
    }
    
    // The Property class is treated as the internal type.
    operator ValueType() {
        assert(FObject != NULL);
        return (FObject->*getter)();
    }
};

template <typename ObjectType, typename ValueType, void (ObjectType::*setter)(ValueType)>
class WriteProperty {
private:
    ObjectType* FObject;
    
public:
    WriteProperty() {
        FObject = NULL;
    }
    
    void SetInstance(ObjectType* Value) {
        FObject = Value;
    }
    
    // To set the value using the set method.
    ValueType operator =(const ValueType& Value) {
        assert(FObject != NULL);
        (FObject->*setter)(Value);
        return Value;
    }
};

template <typename ObjectType, typename ValueType, ValueType (ObjectType::getter)(void), void (ObjectType::setter)(ValueType)>
class StaticProperty {
private:
    
public:
    StaticProperty() {
    }
    
    // To set the value using the set method.
    ValueType operator =(const ValueType& Value) {
        (*setter)(Value);
        return Value;
    }
    
    // The Property class is treated as the internal type which is the getter.
    operator ValueType() {
        return (*getter)();
    }
};

template <typename ObjectType, typename ValueType, ValueType (ObjectType::getter)(void)>
class StaticReadProperty {
private:
    
public:
    StaticReadProperty() {
    }
    
    // The Property class is treated as the internal type which is the getter.
    operator ValueType() {
        return (*getter)();
    }
};

template <typename ObjectType, typename ValueType, void (ObjectType::setter)(ValueType)>
class StaticWriteProperty {
private:
    
public:
    StaticWriteProperty() {
    }
    
    // To set the value using the set method.
    ValueType operator =(const ValueType& Value) {
        (*setter)(Value);
        return Value;
    }
};

As you can see there are several classes. I moved the read/write/readwrite enum from being a field of the property class to being a class. I also added static versions. Oh, why would one want a static property? Well, here are a couple examples of using these:

class Environment {
public:
    Environment() {
        PropertyTest.SetInstance(this);
    }
    
// Property
private:
    std::string FPropertyTest;
    
public:
    void SetPropertyTest(std::string Value) { FPropertyTest = Value; }
    std::string GetPropertyTest() { return FPropertyTest; }
    
    Property<Environmentstd::string, &Environment::GetPropertyTest, &Environment::SetPropertyTest> PropertyTest;

// Static Property
private:
    static std::string FTest;

public:
    static void SetTest(std::string Value) { FTest = Value; }
    static std::string GetTest() { return FTest; }
    
    static StaticProperty<Environmentstd::string, &Environment::GetTest, &Environment::SetTest> Test;
};

StaticProperty<Environmentstd::string, &Environment::GetTest, &Environment::SetTest> Environment::Test;
std::string Environment::FTest = "foo";


There are a few limitations still. For example, the dot (.) operator cannot be overridden. So doing:

printf("%s\n", Environment::Test.data());

isn't going to work. The arrow (->) operator can be overridden, so this behavior could be supported that way if one was so inclined. Seems a little odd. I could go either way.

Monday, April 20, 2015

Properties in C++

A long time ago when I was working on Borland C++ and Delphi was just a baby, we added properties as a language extension to C++. Nobody at the C++ committee liked the notion (not sure why, but Java didn't like it either), but I came up with a way of doing properties in C++ using templates. Here is the property class:

enum PropertyType {READ_ONLY, WRITE_ONLY, READ_WRITE};

template <typename Container, typename ValueType, PropertyType Type>
class Property {
private:
    Container* FObject;
    void (Container::*FSetter)(ValueType value);
    ValueType (Container::*FGetter)();
    
public:
    Property() {
        FObject = NULL;
        FSetter = NULL;
        FGetter = NULL;
    }
    
    void InitializeSetterGetter(Container* Value,
                                void (Container::*Setter)(ValueType Value),
                                ValueType (Container::*Getter)()) {
        assert(Type == READ_WRITE);
        FObject = Value;
        FSetter = Setter;
        FGetter = Getter;
    }

    void InitializeSetter(Container* Value,
                          void (Container::*Setter)(ValueType Value)) {
        assert(Type == WRITE_ONLY);
        FObject = Value;
        FSetter = Setter;
    }

    void InitializeGetter(Container* Value,
                          ValueType (Container::*Getter)()) {
        assert(Type == READ_ONLY);
        FObject = Value;
        FGetter = Getter;
    }
    
    // To set the value using the set method.
    ValueType operator =(const ValueType& Value) {
        assert(FObject != NULL);
        assert(FSetter != NULL);
        (FObject->*FSetter)(Value);
        return Value;
    }
    
    // The Property class is treated as the internal type.
    operator ValueType() {
        assert(FObject != NULL);
        assert(FGetter != NULL);
        return (FObject->*FGetter)();
    }

};

Now, to use this, call one of the Initializer* functions from within your classes constructor, provide getter and setter methods and instantiate the property as follows:

class TestProperty {
protected:
    std::string FMyProperty;
    
public:
    TestProperty() {
        MyProperty.InitializeGetter(this,
                                    &TestProperty::SetMyProperty
                                    &TestProperty::GetMyProperty);
    }

    std::string GetMyProperty() {
        return FMyProperty;
    }

    void SetMyProperty(std::string Value) {
        FMyProperty = Value
    }
    
    Property<Process, std::string, READ_WRITEMyProperty;
};

It's a bit more work than if you were to use a language that supports properties such as Delphi or C#, but it can be a very handy syntactic sugar.

Monday, March 30, 2015

Workout - Functional Patterns

The title says it all: "This is not CROSSFIT this is FUNCTIONAL PATTERNS". This is an extremely ergonomic and healthy way to workout. CrossFit is not safe. Seriously, it isn't. Most instructors are not knowledgable enough which is why CrossFit is so popular. It takes a few days to get a CrossFit certification. Lots of cling and jerky movements where as "Functional Patterns" is very round, front back side to side. Simply awesome!

Thursday, March 26, 2015

Exercises to do at your Desk

We now know that sitting all day isn't good for you. So here is a graphic with a bunch of desk-based exercises.


Wednesday, March 25, 2015

Back from my Blog Hiatus

I had a run in with a few viruses and bacteria over the last few months and haven't posed much (one post I think). So, I'm just now getting some time and feeling up to the task of adding more content to this blog.

Wednesday, February 18, 2015

C++ Ordered Map

The std::map class is not ordered. Coming from any other programming language with a nice standard library this seems like a serious hole in C++. Boost has one, but not everyone can use Boost. So, here is one a threw together. Sure it's missing things, but that's what you get for free. Enjoy!

#ifndef ORDEREDMAP_H
#define ORDEREDMAP_H

#include <map>
#include <vector>
#include <assert .h="">


template <typename _t1="" _t2="" typename="">
struct pair
{
    typedef _T1 first_type;
    typedef _T2 second_type;
    
    first_type first;
    second_type second;
    
    pair(first_type Value1, second_type Value2) {
        first = Value1;
        second = Value2;
    }
};

template <typename tkey="" tvalue="" typename="">
class OrderedMap {
public:
    typedef TKey key_type;
    typedef TValue mapped_type;
    typedef pair<key_type mapped_type=""> container_type;
    
private:
    typedef std::map<key_type container_type=""> map_type;
    typedef std::vector<container_type> list_type;
    
    map_type FMap;
    list_type FList;
    
    typename list_type::iterator FindListItem(const key_type Key) {
        typename list_type::iterator result = FList.end();
        
        for (typename list_type::iterator iterator = FList.begin(); iterator != FList.end(); iterator++) {
            container_type *item = *iterator;
            
            if (item->first == Key) {
                result = iterator;
                break;
            }
        }
        
        return result;
    }
    
public:
    OrderedMap() {
    }
    
    ~OrderedMap() {
        for (typename list_type::iterator iterator = FList.begin(); iterator != FList.end(); iterator++) {
            container_type *item = *iterator;
            delete item;
        }
    }
    
    void Append(key_type Key, mapped_type Value) {
        container_type *item = new container_type(Key, Value);
        item->first = Key;
        item->second = Value;
        
        FMap.insert(std::pair<key_type container_type="">(Key, item));
        FList.push_back(item);
    }
    
    void Insert(size_t Index, key_type Key, mapped_type Value) {
        container_type *item = new container_type(Key, Value);
        item->first = Key;
        item->second = Value;
        
        FMap.insert(std::pair<key_type container_type="">(Key, item));
        FList.insert(FList.begin() + Index, item);
    }
    
    void Remove(key_type Key) {
        typename list_type::iterator iterator = FindListItem(Key);
        
        if (iterator != FList.end()) {
            FMap.erase(Key);
            FList.erase(iterator);
        }
    }
    
    void Remove(size_t Index) {
        typename list_type::iterator iterator = FList.begin() + Index;
        
        if (iterator != FList.end()) {
            container_type* item = *iterator;
        
            if (item != NULL) {
                FMap.erase(item->first);
                FList.erase(iterator);
            }
        }
    }
    
    mapped_type &operator[](key_type Key) {
        container_type* item = FMap[Key];
        assert(item != NULL);
        
        if (item != NULL) {
            return item->second;
        }
        
        throw std::out_of_range("Index out of range");
    }
    
    mapped_type &operator[](size_t Index) {
        assert(Index >= 0 && Index < Count());
        container_type* item = FList[Index];
        assert(item != NULL);
        
        if (item != NULL) {
            return item->second;
        }
        
        throw std::out_of_range("Index out of range");
    }
    
    size_t Count() {
        return FList.size();
    }
};

#endif //ORDEREDMAP_H

Update: March, 25 2015 - The code was not escaped properly and didn't show up right. Now it should be better.

Thursday, January 8, 2015

New Years Resolution #1 - Be More Healthy by Not Drinking Soda

If you stop drinking soda you will loose 5lbs in the first month. Seriously. Don't switch to diet soda, drink water instead.

Search Google and you'll come up with thousands of links such as the first three that support what I'm saying:

The-Daily-Cost-Of-Your-Soda-Habit
Want to Lose Weight Fast? Cut Out Soda from Your Diet
Can You Lose Body Fat by Stopping Drinking Soda?

If you replace it with anything that contains calories such as lemon aid it won't help. Replace the soda with water or maybe tea. Obviously the occasional soda is fine, but once you cut it out and water becomes your new habit, you won't be able to finish an entire 12oz soda. I have friends who have done this and it has worked for them. Make this your number one New Year's resolution and you won't be disappointed.

Wednesday, December 31, 2014

New Years Resolutions

Every year millions of Americans declare New Year's resolutions. Here is a great article about how this whole tradition started:

The Surprising Reason We Make New Year's Resolutions

Since most people I know always have losing weight, staying fit and healthy and enjoying life on their list, I'm going to dedicate all my posts in January to things that I do (or wish I did) to satisfy a fit and healthy lifestyle. Everything will be published under a new label Lifestyle.

I'm also hoping to get my sister who is a physical therapist to write a few posts because she has a lot of extremely valuable detailed information from her education that most of us would only dream of knowing. Or maybe we wouldn't want to know because once you know you have to do things better!

So stay tuned and check back often because this should be fun!

Tuesday, December 30, 2014

iOS Paralax

To disable parallax in iOS 7 and 8, open Settings and navigate to General, Accessibility and turn the “Reduce Motion” option on.

I had this turned off prior to the latest iOS 8.* update.



Monday, December 29, 2014

How To Tie a Bow Tie With Bill Nye

Bow Ties are cool. Here's how to tie one.